2018-09-19 06:09:03 +00:00
|
|
|
// Highly experimental plugin for caching rendered pages for guests
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-04-20 12:49:48 +00:00
|
|
|
//"log"
|
2019-04-20 04:55:22 +00:00
|
|
|
"bytes"
|
2018-09-19 06:09:03 +00:00
|
|
|
"sync/atomic"
|
2019-04-20 04:55:22 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-09-19 06:09:03 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
|
|
|
"github.com/Azareal/Gosora/routes"
|
2018-09-19 06:09:03 +00:00
|
|
|
)
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
var hyperspace *Hyperspace
|
2018-09-19 06:09:03 +00:00
|
|
|
|
|
|
|
func init() {
|
2019-04-20 04:55:22 +00:00
|
|
|
c.Plugins.Add(&c.Plugin{UName: "hyperdrive", Name: "Hyperdrive", Author: "Azareal", Init: initHdrive, Deactivate: deactivateHdrive})
|
2018-09-19 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
func initHdrive(plugin *c.Plugin) error {
|
|
|
|
hyperspace = newHyperspace()
|
|
|
|
plugin.AddHook("tasks_tick_topic_list",tickHdrive)
|
2019-04-20 12:49:48 +00:00
|
|
|
plugin.AddHook("tasks_tick_widget_wol",tickHdriveWol)
|
2019-04-20 04:55:22 +00:00
|
|
|
plugin.AddHook("route_topic_list_start",jumpHdrive)
|
2018-09-19 06:09:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
func deactivateHdrive(plugin *c.Plugin) {
|
|
|
|
plugin.RemoveHook("tasks_tick_topic_list",tickHdrive)
|
2019-04-20 12:49:48 +00:00
|
|
|
plugin.RemoveHook("tasks_tick_widget_wol",tickHdriveWol)
|
2019-04-20 04:55:22 +00:00
|
|
|
plugin.RemoveHook("route_topic_list_start",jumpHdrive)
|
|
|
|
hyperspace = nil
|
2018-09-19 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
type Hyperspace struct {
|
2018-09-19 06:09:03 +00:00
|
|
|
topicList atomic.Value
|
2019-04-27 06:32:26 +00:00
|
|
|
gzipTopicList atomic.Value
|
2018-09-19 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
func newHyperspace() *Hyperspace {
|
|
|
|
pageCache := new(Hyperspace)
|
2018-09-19 06:09:03 +00:00
|
|
|
pageCache.topicList.Store([]byte(""))
|
|
|
|
return pageCache
|
|
|
|
}
|
2019-04-20 04:55:22 +00:00
|
|
|
|
2019-04-20 12:49:48 +00:00
|
|
|
func tickHdriveWol(args ...interface{}) (skip bool, rerr c.RouteError) {
|
|
|
|
c.DebugLog("docking at wol")
|
|
|
|
return tickHdrive(args)
|
|
|
|
}
|
|
|
|
|
2019-04-27 10:22:39 +00:00
|
|
|
// TODO: Find a better way of doing this
|
|
|
|
func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
|
|
|
|
c.DebugLog("Refueling...")
|
|
|
|
|
|
|
|
// Avoid accidentally caching already cached content
|
|
|
|
hyperspace.topicList.Store([]byte(""))
|
|
|
|
hyperspace.gzipTopicList.Store([]byte(""))
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2019-04-20 04:55:22 +00:00
|
|
|
req := httptest.NewRequest("get", "/topics/", bytes.NewReader(nil))
|
|
|
|
user := c.GuestUser
|
|
|
|
|
2019-04-27 10:22:39 +00:00
|
|
|
head, rerr := c.UserCheck(w, req, &user)
|
|
|
|
if rerr != nil {
|
2019-04-20 04:55:22 +00:00
|
|
|
return true, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
rerr = routes.TopicList(w, req, user, head)
|
|
|
|
if rerr != nil {
|
|
|
|
return true, rerr
|
|
|
|
}
|
|
|
|
if w.Code != 200 {
|
2019-04-27 10:22:39 +00:00
|
|
|
c.LogWarning(errors.New("not 200 for topic list in hyperdrive"))
|
2019-04-27 06:32:26 +00:00
|
|
|
return false, nil
|
2019-04-20 04:55:22 +00:00
|
|
|
}
|
2019-04-27 06:32:26 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(w.Result().Body)
|
|
|
|
hyperspace.topicList.Store(buf.Bytes())
|
|
|
|
|
2019-04-27 10:22:39 +00:00
|
|
|
gbuf, err := c.CompressBytesGzip(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
c.LogWarning(err)
|
|
|
|
return false, nil
|
2019-04-27 06:32:26 +00:00
|
|
|
}
|
2019-04-27 10:22:39 +00:00
|
|
|
hyperspace.gzipTopicList.Store(gbuf)
|
2019-04-27 06:32:26 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func jumpHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
|
2019-04-27 06:32:26 +00:00
|
|
|
var tList []byte
|
|
|
|
w := args[0].(http.ResponseWriter)
|
|
|
|
_, ok := w.(c.GzipResponseWriter)
|
|
|
|
if ok {
|
|
|
|
tList = hyperspace.gzipTopicList.Load().([]byte)
|
|
|
|
} else {
|
|
|
|
tList = hyperspace.topicList.Load().([]byte)
|
|
|
|
}
|
2019-04-20 04:55:22 +00:00
|
|
|
if len(tList) == 0 {
|
2019-04-20 12:49:48 +00:00
|
|
|
c.DebugLog("no topiclist in hyperspace")
|
2019-04-20 04:55:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid intercepting user requests as we only have guests in cache right now
|
|
|
|
user := args[2].(*c.User)
|
|
|
|
if user.ID != 0 {
|
2019-04-20 12:49:48 +00:00
|
|
|
c.DebugLog("not guest")
|
2019-04-20 04:55:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid intercepting search requests and filters as we don't have those in cache
|
|
|
|
r := args[1].(*http.Request)
|
2019-04-20 12:49:48 +00:00
|
|
|
//c.DebugLog("r.URL.Path:",r.URL.Path)
|
|
|
|
//c.DebugLog("r.URL.RawQuery:",r.URL.RawQuery)
|
2019-04-20 04:55:22 +00:00
|
|
|
if r.URL.RawQuery != "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-04-27 10:22:39 +00:00
|
|
|
if r.FormValue("js") == "1" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-04-20 12:49:48 +00:00
|
|
|
//c.DebugLog
|
|
|
|
c.DebugLog("Successful jump")
|
2019-04-20 04:55:22 +00:00
|
|
|
|
|
|
|
header := args[3].(*c.Header)
|
|
|
|
routes.FootHeaders(w, header)
|
|
|
|
w.Write(tList)
|
2019-04-27 10:22:39 +00:00
|
|
|
if ok {
|
|
|
|
w.Header().Set("X-I","1")
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
return true, nil
|
|
|
|
}
|