A basic, barebones web app, intented to be used as a starting point for developing an Isomorphic Go application.

isogoapp.go 1.3KB

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