Browse Source

Code refactoring

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

+ 14
- 0
handlers/gopherjs.go View File

@@ -0,0 +1,14 @@
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

@@ -0,0 +1,12 @@
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
+}

+ 10
- 7
isogoapp.go View File

@@ -2,14 +2,14 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"html/template"
6
-	"log"
7 5
 	"net/http"
8 6
 	"os"
9 7
 
10 8
 	"github.com/gorilla/mux"
9
+	"github.com/isomorphicgo/isogoapp/handlers"
11 10
 )
12 11
 
12
+<<<<<<< HEAD
13 13
 var webappRoot string = os.Getenv("ISOGO_APP_ROOT")
14 14
 
15 15
 func indexHandler(w http.ResponseWriter, r *http.Request) {
@@ -33,19 +33,22 @@ func gopherjsScriptHandler(w http.ResponseWriter, r *http.Request) {
33 33
 func gopherjsScriptMapHandler(w http.ResponseWriter, r *http.Request) {
34 34
 	http.ServeFile(w, r, webappRoot+"/client/client.js.map")
35 35
 }
36
+=======
37
+var WebAppRoot string = os.Getenv("ISOGO_APP_ROOT")
38
+>>>>>>> 1f58dae263ab74226133033f55a2a449b05b4683
36 39
 
37 40
 func main() {
38 41
 
39
-	if webappRoot == "" {
42
+	if WebAppRoot == "" {
40 43
 		fmt.Println("The ISOGO_APP_ROOT environment variable must be set before the web server instance can be started.")
41 44
 		os.Exit(1)
42 45
 	}
43 46
 
44
-	fs := http.FileServer(http.Dir(webappRoot + "/static"))
47
+	fs := http.FileServer(http.Dir(WebAppRoot + "/static"))
45 48
 	r := mux.NewRouter()
46
-	r.HandleFunc("/", indexHandler)
47
-	r.HandleFunc("/js/client.js", gopherjsScriptHandler)
48
-	r.HandleFunc("/js/client.js.map", gopherjsScriptMapHandler)
49
+	r.HandleFunc("/", handlers.IndexHandler)
50
+	r.HandleFunc("/js/client.js", handlers.GopherjsScriptHandler)
51
+	r.HandleFunc("/js/client.js.map", handlers.GopherjsScriptMapHandler)
49 52
 	http.Handle("/", r)
50 53
 	http.Handle("/static/", http.StripPrefix("/static", fs))
51 54
 	http.ListenAndServe(":8080", nil)

+ 17
- 0
view/render.go View File

@@ -0,0 +1,17 @@
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
+}