Browse Source

Refactored handler code.

Wirecog 6 years ago
parent
commit
f69e8adabc
3 changed files with 42 additions and 3 deletions
  1. 7
    0
      common/common.go
  2. 24
    0
      handlers/templatebundle.go
  3. 11
    3
      isogoapp.go

+ 7
- 0
common/common.go View File

@@ -0,0 +1,7 @@
1
+package common
2
+
3
+import "isomorphicgo/isokit"
4
+
5
+type Env struct {
6
+	TemplateSet *isokit.TemplateSet
7
+}

+ 24
- 0
handlers/templatebundle.go View File

@@ -0,0 +1,24 @@
1
+package handlers
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/gob"
6
+	"isogoblog/common"
7
+	"log"
8
+	"net/http"
9
+)
10
+
11
+func TemplateBundleHandler(env *common.Env) http.Handler {
12
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13
+		var templateContentItemsBuffer bytes.Buffer
14
+		enc := gob.NewEncoder(&templateContentItemsBuffer)
15
+		m := env.TemplateSet.Bundle().Items()
16
+		err := enc.Encode(&m)
17
+		if err != nil {
18
+			log.Print("encoding err: ", err)
19
+		}
20
+		w.Header().Set("Content-Type", "application/octet-stream")
21
+		w.Write(templateContentItemsBuffer.Bytes())
22
+	})
23
+
24
+}

+ 11
- 3
isogoapp.go View File

@@ -2,6 +2,8 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"isogoblog/common"
6
+	"isomorphicgo/isokit"
5 7
 	"net/http"
6 8
 	"os"
7 9
 
@@ -18,11 +20,17 @@ func main() {
18 20
 		os.Exit(1)
19 21
 	}
20 22
 
21
-	fs := http.FileServer(http.Dir(WebAppRoot + "/static"))
23
+	env := common.Env{}
24
+	ts := isokit.NewTemplateSet()
25
+	ts.GatherTemplates()
26
+	env.TemplateSet = ts
22 27
 	r := mux.NewRouter()
23 28
 	r.HandleFunc("/", handlers.IndexHandler)
24
-	r.HandleFunc("/js/client.js", handlers.GopherjsScriptHandler)
25
-	r.HandleFunc("/js/client.js.map", handlers.GopherjsScriptMapHandler)
29
+	r.HandleFunc("/js/client.js", isokit.GopherjsScriptHandler)
30
+	r.HandleFunc("/js/client.js.map", isokit.GopherjsScriptMapHandler)
31
+	r.Handle("/template-bundle", handlers.TemplateBundleHandler(&env))
32
+
33
+	fs := http.FileServer(http.Dir(WebAppRoot + "/static"))
26 34
 	http.Handle("/", r)
27 35
 	http.Handle("/static/", http.StripPrefix("/static", fs))
28 36
 	http.ListenAndServe(":8080", nil)