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

isogoapp.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "github.com/gorilla/mux"
  7. "github.com/isomorphicgo/isogoapp/handlers"
  8. )
  9. <<<<<<< HEAD
  10. var webappRoot string = os.Getenv("ISOGO_APP_ROOT")
  11. func indexHandler(w http.ResponseWriter, r *http.Request) {
  12. renderTemplate(w, webappRoot+"/templates/index.tmpl", 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. =======
  29. var WebAppRoot string = os.Getenv("ISOGO_APP_ROOT")
  30. >>>>>>> 1f58dae263ab74226133033f55a2a449b05b4683
  31. func main() {
  32. if WebAppRoot == "" {
  33. fmt.Println("The ISOGO_APP_ROOT environment variable must be set before the web server instance can be started.")
  34. os.Exit(1)
  35. }
  36. fs := http.FileServer(http.Dir(WebAppRoot + "/static"))
  37. r := mux.NewRouter()
  38. r.HandleFunc("/", handlers.IndexHandler)
  39. r.HandleFunc("/js/client.js", handlers.GopherjsScriptHandler)
  40. r.HandleFunc("/js/client.js.map", handlers.GopherjsScriptMapHandler)
  41. http.Handle("/", r)
  42. http.Handle("/static/", http.StripPrefix("/static", fs))
  43. http.ListenAndServe(":8080", nil)
  44. }