145 lines
3.3 KiB
Go
Raw Normal View History

2016-10-21 16:25:35 +00:00
package main
import (
2016-10-23 04:15:47 +00:00
"encoding/json"
2016-10-22 05:34:33 +00:00
"fmt"
"io"
2016-10-22 17:03:16 +00:00
"io/ioutil"
"runtime"
2016-10-21 16:25:35 +00:00
"strconv"
2016-10-22 05:34:33 +00:00
"strings"
2016-10-23 04:15:47 +00:00
"time"
2016-10-21 16:25:35 +00:00
2016-10-22 05:34:33 +00:00
"github.com/Joker/jade"
2016-10-23 04:15:47 +00:00
"github.com/OneOfOne/xxhash"
2016-10-21 16:25:35 +00:00
"github.com/aerojs/aero"
2016-10-22 17:03:16 +00:00
"github.com/blitzprog/arn"
2016-10-21 16:25:35 +00:00
"github.com/buaazp/fasthttprouter"
2016-10-23 04:15:47 +00:00
cache "github.com/patrickmn/go-cache"
2016-10-22 17:03:16 +00:00
"github.com/robertkrimen/otto"
2016-10-21 16:25:35 +00:00
"github.com/valyala/fasthttp"
2016-10-22 05:34:33 +00:00
"github.com/valyala/fasttemplate"
2016-10-21 16:25:35 +00:00
)
2016-10-22 17:03:16 +00:00
const (
gzipThreshold = 1450
contentTypeHeader = "content-type"
contentType = "text/html;charset=utf-8"
contentEncodingHeader = "content-encoding"
contentEncoding = "gzip"
hello = "Hello World"
)
2016-10-21 16:25:35 +00:00
2016-10-23 04:15:47 +00:00
var jsonToResponse *cache.Cache
2016-10-22 17:03:16 +00:00
func worker(script *otto.Script, jobs <-chan map[string]interface{}, results chan<- string) {
vm := otto.New()
for properties := range jobs {
2016-10-23 04:15:47 +00:00
h := xxhash.NewS64(0)
for _, value := range properties {
jsonBytes, err := json.Marshal(value)
if err == nil {
h.Write(jsonBytes)
// fmt.Println(string(buffer))
}
}
hash := strconv.FormatUint(h.Sum64(), 10)
cachedResponse, found := jsonToResponse.Get(hash)
if found {
results <- cachedResponse.(string)
} else {
for key, value := range properties {
vm.Set(key, value)
}
result, _ := vm.Run(script)
code, _ := result.ToString()
results <- code
jsonToResponse.Set(hash, code, cache.DefaultExpiration)
2016-10-22 17:03:16 +00:00
}
}
}
func main() {
2016-10-23 04:15:47 +00:00
jsonToResponse = cache.New(5*time.Minute, 1*time.Minute)
2016-10-21 16:25:35 +00:00
app := aero.New()
2016-10-22 05:34:33 +00:00
jade.PrettyOutput = false
code, _ := jade.ParseFile("pages/anime/anime.pug")
code = strings.TrimSpace(code)
code = strings.Replace(code, "{{ ", "{{", -1)
code = strings.Replace(code, " }}", "}}", -1)
2016-10-22 17:03:16 +00:00
code = strings.Replace(code, "\n", " ", -1)
t, _ := fasttemplate.NewTemplate(code, "{{", "}}")
jsCode := "html = '" + t.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
if tag == "end" {
return w.Write([]byte("'; }\nhtml += '"))
}
if strings.HasPrefix(tag, "if ") {
return w.Write([]byte("';\nif(" + tag[3:] + ") { html += '"))
}
if tag == "else" {
return w.Write([]byte("';\nelse { html += '"))
}
return w.Write([]byte("';\nhtml += (" + tag + ");\nhtml += '"))
}) + "';"
jsCode = strings.Replace(jsCode, "html += '';", "", -1)
// fmt.Println(code)
// fmt.Println(jsCode)
jsCompiler := otto.New()
script, err := jsCompiler.Compile("pages/anime/anime.pug", jsCode)
if err != nil {
panic(err)
}
example, _ := ioutil.ReadFile("security/frontpage.html")
jobs := make(chan map[string]interface{}, 4096)
results := make(chan string, 4096)
for w := 1; w <= runtime.NumCPU(); w++ {
go worker(script, jobs, results)
}
2016-10-21 16:25:35 +00:00
app.Get("/", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
2016-10-22 17:03:16 +00:00
aero.RespondBytes(ctx, example)
})
app.Get("/hello", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
aero.Respond(ctx, "Hello World")
2016-10-21 16:25:35 +00:00
})
app.Get("/anime/:id", func(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
id, _ := strconv.Atoi(params.ByName("id"))
anime, err := arn.GetAnime(id)
if err != nil {
2016-10-22 17:03:16 +00:00
aero.Respond(ctx, "Anime not found")
2016-10-21 16:25:35 +00:00
return
}
2016-10-23 04:15:47 +00:00
templateParams := make(map[string]interface{})
templateParams["anime"] = anime
jobs <- templateParams
2016-10-22 17:03:16 +00:00
aero.Respond(ctx, <-results)
2016-10-21 16:25:35 +00:00
})
2016-10-23 04:15:47 +00:00
fmt.Println("Starting server on http://localhost:4000/")
2016-10-22 05:34:33 +00:00
2016-10-21 16:25:35 +00:00
app.Run()
}