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

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

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
 
2
 
3
 import (
3
 import (
4
 	"fmt"
4
 	"fmt"
5
+	"isogoblog/common"
6
+	"isomorphicgo/isokit"
5
 	"net/http"
7
 	"net/http"
6
 	"os"
8
 	"os"
7
 
9
 
18
 		os.Exit(1)
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
 	r := mux.NewRouter()
27
 	r := mux.NewRouter()
23
 	r.HandleFunc("/", handlers.IndexHandler)
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
 	http.Handle("/", r)
34
 	http.Handle("/", r)
27
 	http.Handle("/static/", http.StripPrefix("/static", fs))
35
 	http.Handle("/static/", http.StripPrefix("/static", fs))
28
 	http.ListenAndServe(":8080", nil)
36
 	http.ListenAndServe(":8080", nil)