api.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package api
  2. import (
  3. "html/template"
  4. log "github.com/alecthomas/log4go"
  5. "github.com/gin-gonic/gin"
  6. "github.com/gorilla/sessions"
  7. "github.com/torkelo/grafana-pro/backend/components"
  8. "github.com/torkelo/grafana-pro/backend/stores"
  9. )
  10. type HttpServer struct {
  11. port string
  12. shutdown chan bool
  13. store stores.Store
  14. renderer *components.PhantomRenderer
  15. router *gin.Engine
  16. }
  17. var sessionStore = sessions.NewCookieStore([]byte("something-very-secret"))
  18. // var hashKey = []byte("very-secret")
  19. // var blockKey = []byte("a-lot-secret")
  20. // var s = securecookie.New(hashKey, blockKey)
  21. func NewHttpServer(port string, store stores.Store) *HttpServer {
  22. self := &HttpServer{}
  23. self.port = port
  24. self.store = store
  25. self.renderer = &components.PhantomRenderer{ImagesDir: "data/png", PhantomDir: "_vendor/phantomjs"}
  26. return self
  27. }
  28. func (self *HttpServer) ListenAndServe() {
  29. log.Info("Starting Http Listener on port %v", self.port)
  30. defer func() { self.shutdown <- true }()
  31. self.router = gin.Default()
  32. self.router.Use(CacheHeadersMiddleware())
  33. self.router.Static("/public", "./public")
  34. self.router.Static("/app", "./public/app")
  35. self.router.Static("/img", "./public/img")
  36. // register & parse templates
  37. templates := template.New("templates")
  38. templates.Delims("[[", "]]")
  39. templates.ParseFiles("./views/index.html")
  40. self.router.SetHTMLTemplate(templates)
  41. for _, fn := range routeHandlers {
  42. fn(self)
  43. }
  44. // register default route
  45. self.router.GET("/", self.AuthMiddleware(), self.index)
  46. self.router.GET("/login/*_", self.index)
  47. self.router.GET("/dashboard/*_", self.index)
  48. self.router.Run(":" + self.port)
  49. }
  50. func (self *HttpServer) index(c *gin.Context) {
  51. c.HTML(200, "index.html", &indexViewModel{title: "hello from go"})
  52. }
  53. func (self *HttpServer) login(c *gin.Context) {
  54. c.HTML(200, "login.html", &indexViewModel{title: "hello from go"})
  55. }
  56. func (self *HttpServer) AuthMiddleware() gin.HandlerFunc {
  57. return func(c *gin.Context) {
  58. session, _ := sessionStore.Get(c.Request, "grafana-session")
  59. // if session.Values["login"] == nil {
  60. // c.Writer.Header().Set("Location", "/login/login#login")
  61. // c.Abort(302)
  62. // }
  63. session.Values["asd"] = 1
  64. session.Save(c.Request, c.Writer)
  65. }
  66. }
  67. func CacheHeadersMiddleware() gin.HandlerFunc {
  68. return func(c *gin.Context) {
  69. c.Writer.Header().Add("Cache-Control", "max-age=0, public, must-revalidate, proxy-revalidate")
  70. }
  71. }
  72. // Api Handler Registration
  73. var routeHandlers = make([]routeHandlerRegisterFn, 0)
  74. type routeHandlerRegisterFn func(self *HttpServer)
  75. func addRoutes(fn routeHandlerRegisterFn) {
  76. routeHandlers = append(routeHandlers, fn)
  77. }