Improved JS execution
This commit is contained in:
parent
d5e658029b
commit
2386b4cb92
104
main.go
104
main.go
@ -3,33 +3,95 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"io/ioutil"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Joker/jade"
|
"github.com/Joker/jade"
|
||||||
"github.com/aerojs/aero"
|
"github.com/aerojs/aero"
|
||||||
"github.com/animenotifier/arn"
|
"github.com/blitzprog/arn"
|
||||||
"github.com/buaazp/fasthttprouter"
|
"github.com/buaazp/fasthttprouter"
|
||||||
|
"github.com/robertkrimen/otto"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
"github.com/valyala/fasttemplate"
|
"github.com/valyala/fasttemplate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
const (
|
||||||
arn.Init()
|
gzipThreshold = 1450
|
||||||
|
contentTypeHeader = "content-type"
|
||||||
|
contentType = "text/html;charset=utf-8"
|
||||||
|
contentEncodingHeader = "content-encoding"
|
||||||
|
contentEncoding = "gzip"
|
||||||
|
hello = "Hello World"
|
||||||
|
)
|
||||||
|
|
||||||
|
func worker(script *otto.Script, jobs <-chan map[string]interface{}, results chan<- string) {
|
||||||
|
vm := otto.New()
|
||||||
|
|
||||||
|
for properties := range jobs {
|
||||||
|
for key, value := range properties {
|
||||||
|
vm.Set(key, value)
|
||||||
|
}
|
||||||
|
result, _ := vm.Run(script)
|
||||||
|
code, _ := result.ToString()
|
||||||
|
results <- code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
app := aero.New()
|
app := aero.New()
|
||||||
jade.PrettyOutput = false
|
jade.PrettyOutput = false
|
||||||
code, _ := jade.ParseFile("pages/anime/anime.pug")
|
code, _ := jade.ParseFile("pages/anime/anime.pug")
|
||||||
code = strings.TrimSpace(code)
|
code = strings.TrimSpace(code)
|
||||||
code = strings.Replace(code, "{{ ", "{{", -1)
|
code = strings.Replace(code, "{{ ", "{{", -1)
|
||||||
code = strings.Replace(code, " }}", "}}", -1)
|
code = strings.Replace(code, " }}", "}}", -1)
|
||||||
fmt.Println(code)
|
code = strings.Replace(code, "\n", " ", -1)
|
||||||
t := fasttemplate.New(code, "{{", "}}")
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
app.Get("/", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
|
app.Get("/", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
|
||||||
ctx.Response.Header.Set("content-type", "text/html;charset=utf-8")
|
aero.RespondBytes(ctx, example)
|
||||||
ctx.SetBodyString("Hello World")
|
})
|
||||||
|
|
||||||
|
app.Get("/hello", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
|
||||||
|
aero.Respond(ctx, "Hello World")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/anime/:id", func(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
|
app.Get("/anime/:id", func(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
|
||||||
@ -37,29 +99,21 @@ func main() {
|
|||||||
anime, err := arn.GetAnime(id)
|
anime, err := arn.GetAnime(id)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.WriteString("Anime not found")
|
aero.Respond(ctx, "Anime not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Response.Header.Set("content-type", "text/html;charset=utf-8")
|
myMap := make(map[string]interface{})
|
||||||
|
myMap["anime"] = anime
|
||||||
|
jobs <- myMap
|
||||||
|
|
||||||
writer := ctx.Response.BodyWriter()
|
aero.Respond(ctx, <-results)
|
||||||
t.ExecuteFunc(writer, func(w io.Writer, tag string) (int, error) {
|
|
||||||
val := reflect.ValueOf(*anime)
|
|
||||||
parts := strings.Split(tag, ".")
|
|
||||||
|
|
||||||
for _, part := range parts {
|
// if runErr != nil {
|
||||||
val = val.FieldByName(part)
|
// panic(runErr)
|
||||||
}
|
// }
|
||||||
|
|
||||||
switch val.Kind() {
|
// aero.Respond(ctx, result.String())
|
||||||
case reflect.Int:
|
|
||||||
num := strconv.FormatInt(val.Int(), 10)
|
|
||||||
return w.Write([]byte(num))
|
|
||||||
default:
|
|
||||||
return w.Write([]byte(val.String()))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println("Starting server on http://localhost:5000/")
|
fmt.Println("Starting server on http://localhost:5000/")
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user