Browse Source

Keep handlers organized in their own package. Render template functionality is now in the newly introduced view package.

Wirecog 7 years ago
parent
commit
1f58dae263
4 changed files with 50 additions and 30 deletions
  1. 14
    0
      handlers/gopherjs.go
  2. 12
    0
      handlers/index.go
  3. 7
    30
      isogoapp.go
  4. 17
    0
      view/render.go

+ 14
- 0
handlers/gopherjs.go View File

1
+package handlers
2
+
3
+import (
4
+	"net/http"
5
+	"os"
6
+)
7
+
8
+func GopherjsScriptHandler(w http.ResponseWriter, r *http.Request) {
9
+	http.ServeFile(w, r, os.Getenv("ISOGO_APP_ROOT")+"/client/client.js")
10
+}
11
+
12
+func GopherjsScriptMapHandler(w http.ResponseWriter, r *http.Request) {
13
+	http.ServeFile(w, r, os.Getenv("ISOGO_APP_ROOT")+"/client/client.js.map")
14
+}

+ 12
- 0
handlers/index.go View File

1
+package handlers
2
+
3
+import (
4
+	"net/http"
5
+	"os"
6
+
7
+	"github.com/isomorphicgo/isogoapp/view"
8
+)
9
+
10
+func IndexHandler(w http.ResponseWriter, r *http.Request) {
11
+	view.RenderTemplate(w, os.Getenv("ISOGO_APP_ROOT")+"/templates/index.html", nil)
12
+}

+ 7
- 30
isogoapp.go View File

2
 
2
 
3
 import (
3
 import (
4
 	"fmt"
4
 	"fmt"
5
-	"html/template"
6
-	"log"
7
 	"net/http"
5
 	"net/http"
8
 	"os"
6
 	"os"
9
 
7
 
10
 	"github.com/gorilla/mux"
8
 	"github.com/gorilla/mux"
9
+	"github.com/isomorphicgo/isogoapp/handlers"
11
 )
10
 )
12
 
11
 
13
-var webappRoot string = os.Getenv("ISOGO_APP_ROOT")
14
-
15
-func indexHandler(w http.ResponseWriter, r *http.Request) {
16
-	renderTemplate(w, webappRoot+"/templates/index.html", nil)
17
-}
18
-
19
-// Template rendering function
20
-func renderTemplate(w http.ResponseWriter, templateFile string, templateData interface{}) {
21
-
22
-	t, err := template.ParseFiles(templateFile)
23
-	if err != nil {
24
-		log.Fatal("Error encountered while parsing the template: ", err)
25
-	}
26
-	t.Execute(w, templateData)
27
-}
28
-
29
-func gopherjsScriptHandler(w http.ResponseWriter, r *http.Request) {
30
-	http.ServeFile(w, r, webappRoot+"/client/client.js")
31
-}
32
-
33
-func gopherjsScriptMapHandler(w http.ResponseWriter, r *http.Request) {
34
-	http.ServeFile(w, r, webappRoot+"/client/client.js.map")
35
-}
12
+var WebAppRoot string = os.Getenv("ISOGO_APP_ROOT")
36
 
13
 
37
 func main() {
14
 func main() {
38
 
15
 
39
-	if webappRoot == "" {
16
+	if WebAppRoot == "" {
40
 		fmt.Println("The ISOGO_APP_ROOT environment variable must be set before the web server instance can be started.")
17
 		fmt.Println("The ISOGO_APP_ROOT environment variable must be set before the web server instance can be started.")
41
 		os.Exit(1)
18
 		os.Exit(1)
42
 	}
19
 	}
43
 
20
 
44
-	fs := http.FileServer(http.Dir(webappRoot + "/static"))
21
+	fs := http.FileServer(http.Dir(WebAppRoot + "/static"))
45
 	r := mux.NewRouter()
22
 	r := mux.NewRouter()
46
-	r.HandleFunc("/", indexHandler)
47
-	r.HandleFunc("/js/client.js", gopherjsScriptHandler)
48
-	r.HandleFunc("/js/client.js.map", gopherjsScriptMapHandler)
23
+	r.HandleFunc("/", handlers.IndexHandler)
24
+	r.HandleFunc("/js/client.js", handlers.GopherjsScriptHandler)
25
+	r.HandleFunc("/js/client.js.map", handlers.GopherjsScriptMapHandler)
49
 	http.Handle("/", r)
26
 	http.Handle("/", r)
50
 	http.Handle("/static/", http.StripPrefix("/static", fs))
27
 	http.Handle("/static/", http.StripPrefix("/static", fs))
51
 	http.ListenAndServe(":8080", nil)
28
 	http.ListenAndServe(":8080", nil)

+ 17
- 0
view/render.go View File

1
+package view
2
+
3
+import (
4
+	"html/template"
5
+	"log"
6
+	"net/http"
7
+)
8
+
9
+// Template rendering function
10
+func RenderTemplate(w http.ResponseWriter, templateFile string, templateData interface{}) {
11
+
12
+	t, err := template.ParseFiles(templateFile)
13
+	if err != nil {
14
+		log.Fatal("Error encountered while parsing the template: ", err)
15
+	}
16
+	t.Execute(w, templateData)
17
+}