From 2386b4cb92dabc6fc518f11372468dac6868c8ed Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 23 Oct 2016 02:03:16 +0900 Subject: [PATCH] Improved JS execution --- main.go | 104 ++++++++++++++++++++++++++++++++---------- pages/anime/anime.pug | 18 ++++++-- 2 files changed, 92 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index 9604031d..0d3bbcb7 100644 --- a/main.go +++ b/main.go @@ -3,33 +3,95 @@ package main import ( "fmt" "io" - "reflect" + "io/ioutil" + "runtime" "strconv" "strings" "github.com/Joker/jade" "github.com/aerojs/aero" - "github.com/animenotifier/arn" + "github.com/blitzprog/arn" "github.com/buaazp/fasthttprouter" + "github.com/robertkrimen/otto" "github.com/valyala/fasthttp" "github.com/valyala/fasttemplate" ) -func main() { - arn.Init() +const ( + 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() jade.PrettyOutput = false code, _ := jade.ParseFile("pages/anime/anime.pug") code = strings.TrimSpace(code) code = strings.Replace(code, "{{ ", "{{", -1) code = strings.Replace(code, " }}", "}}", -1) - fmt.Println(code) - t := fasttemplate.New(code, "{{", "}}") + 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) + } app.Get("/", func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) { - ctx.Response.Header.Set("content-type", "text/html;charset=utf-8") - ctx.SetBodyString("Hello World") + aero.RespondBytes(ctx, example) + }) + + 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) { @@ -37,29 +99,21 @@ func main() { anime, err := arn.GetAnime(id) if err != nil { - ctx.WriteString("Anime not found") + aero.Respond(ctx, "Anime not found") 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() - t.ExecuteFunc(writer, func(w io.Writer, tag string) (int, error) { - val := reflect.ValueOf(*anime) - parts := strings.Split(tag, ".") + aero.Respond(ctx, <-results) - for _, part := range parts { - val = val.FieldByName(part) - } + // if runErr != nil { + // panic(runErr) + // } - switch val.Kind() { - case reflect.Int: - num := strconv.FormatInt(val.Int(), 10) - return w.Write([]byte(num)) - default: - return w.Write([]byte(val.String())) - } - }) + // aero.Respond(ctx, result.String()) }) fmt.Println("Starting server on http://localhost:5000/") diff --git a/pages/anime/anime.pug b/pages/anime/anime.pug index 46bb4b27..01e3d40d 100644 --- a/pages/anime/anime.pug +++ b/pages/anime/anime.pug @@ -1,6 +1,14 @@ -h1= Title.Romaji -p= Description -if ID - p= ID +h1= anime.Title.Romaji +p= anime.Description + +p + | The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. + | The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. + | The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. + +p The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. The story takes place in a world filled with monsters and supernatural forces. The series focuses on four girls, each with their own unique weapon and powers, who come together as a team at Beacon Academy in a place called Vale where they are trained to battle these forces alongside other similar teams. Prior to the events of the series, mankind waged a battle of survival against the shadowy creatures of the Grimm until they discovered the power of Dust, which allowed them to fight back the monsters. Dust is used to power magical abilities in the series. + +if anime.ID + p= anime.ID else - p Undefined. \ No newline at end of file + p No ID. \ No newline at end of file