You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.6 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gorilla/securecookie"
  5. "github.com/gorilla/sessions"
  6. "os"
  7. "log"
  8. "git.wegmueller.it/fruitsteps/framework/auth"
  9. "github.com/gorilla/schema"
  10. "encoding/base64"
  11. "github.com/gorilla/mux"
  12. "git.wegmueller.it/fruitsteps/framework/controller"
  13. "github.com/justinas/alice"
  14. "git.wegmueller.it/fruitsteps/framework/xrsf"
  15. )
  16. var schemaDec = schema.NewDecoder()
  17. var (
  18. mainCont = MainController{}
  19. newTileCont = NewTileController{}
  20. tileDetailCont = TileDetailController{}
  21. listTileCont = TileListController{}
  22. )
  23. func main() {
  24. // Initialize Sessions and Cookies
  25. // Typically gorilla securecookie and sessions packages require
  26. // highly random secret keys that are not divulged to the public.
  27. //
  28. // In this example we use keys generated one time (if these keys ever become
  29. // compromised the gorilla libraries allow for key rotation, see gorilla docs)
  30. // The keys are 64-bytes as recommended for HMAC keys as per the gorilla docs.
  31. //
  32. // These values MUST be changed for any new project as these keys are already "compromised"
  33. // as they're in the public domain, if you do not change these your application will have a fairly
  34. // wide-opened security hole. You can generate your own with the code below, or using whatever method
  35. // you prefer:
  36. //
  37. // func main() {
  38. // fmt.Println(base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(64)))
  39. // }
  40. //
  41. // We store them in base64 in the example to make it easy if we wanted to move them later to
  42. // a configuration environment var or file.
  43. cookieStoreKey, _ := base64.StdEncoding.DecodeString(`NpEPi8pEjKVjLGJ6kYCS+VTCzi6BUuDzU0wrwXyf5uDPArtlofn2AG6aTMiPmN3C909rsEWMNqJqhIVPGP3Exg==`)
  44. sessionStoreKey, _ := base64.StdEncoding.DecodeString(`AbfYwmmt8UCwUuhd9qvfNA9UCuN1cVcKJN1ofbiky6xCyyBj20whe40rJa3Su0WOWLWcPpO1taqJdsEI/65+JA==`)
  45. auth.CookieStore = securecookie.New(cookieStoreKey, nil)
  46. auth.SetSessionStore(sessions.NewCookieStore(sessionStoreKey))
  47. // Initialize ab.
  48. //auth.SetupAuthboss()
  49. //Load Templates to cache
  50. controller.Init("./templates/*.html", true, "", nil)
  51. // Set up our router
  52. schemaDec.IgnoreUnknownKeys(true)
  53. //Routes
  54. mux_router := mux.NewRouter()
  55. controller.SetUpRouting(mux_router, []controller.ControllerInterface{
  56. &mainCont,
  57. &newTileCont,
  58. &tileDetailCont,
  59. &listTileCont,
  60. })
  61. mux_router.NotFoundHandler = http.HandlerFunc(controller.NotFound)
  62. // Set up our middleware chain
  63. stack := alice.New(auth.Logger, xrsf.Nosurfing).Then(mux_router)
  64. // Start the server
  65. port := os.Getenv("PORT")
  66. if len(port) == 0 {
  67. port = "8080"
  68. }
  69. log.Println(http.ListenAndServe("localhost:"+port, stack))
  70. }