diff --git a/backend/api/api.go b/backend/api/api.go index 3d592a035c3..ee2f1133f54 100644 --- a/backend/api/api.go +++ b/backend/api/api.go @@ -55,9 +55,8 @@ func (self *HttpServer) ListenAndServe() { } // register default route - self.router.GET("/", self.AuthMiddleware(), self.index) - self.router.GET("/login/*_", self.index) - self.router.GET("/dashboard/*_", self.index) + self.router.GET("/", self.authMiddleware(), self.index) + self.router.GET("/dashboard/*_", self.authMiddleware(), self.index) self.router.Run(":" + self.port) } @@ -66,22 +65,6 @@ func (self *HttpServer) index(c *gin.Context) { c.HTML(200, "index.html", &indexViewModel{title: "hello from go"}) } -func (self *HttpServer) login(c *gin.Context) { - c.HTML(200, "login.html", &indexViewModel{title: "hello from go"}) -} - -func (self *HttpServer) AuthMiddleware() gin.HandlerFunc { - return func(c *gin.Context) { - session, _ := sessionStore.Get(c.Request, "grafana-session") - // if session.Values["login"] == nil { - // c.Writer.Header().Set("Location", "/login/login#login") - // c.Abort(302) - // } - session.Values["asd"] = 1 - session.Save(c.Request, c.Writer) - } -} - func CacheHeadersMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Add("Cache-Control", "max-age=0, public, must-revalidate, proxy-revalidate") diff --git a/backend/api/api_dashboard.go b/backend/api/api_dashboard.go index 7c1f9614233..08fb5c9f63e 100644 --- a/backend/api/api_dashboard.go +++ b/backend/api/api_dashboard.go @@ -41,7 +41,7 @@ func (self *HttpServer) postDashboard(c *gin.Context) { var command saveDashboardCommand if c.EnsureBody(&command) { - err := self.store.Save(&models.Dashboard{Data: command.dashboard}) + err := self.store.Save(&models.Dashboard{Data: command.Dashboard}) if err == nil { c.JSON(200, gin.H{"status": "saved"}) return diff --git a/backend/api/api_login.go b/backend/api/api_login.go new file mode 100644 index 00000000000..47a8c6ef5c9 --- /dev/null +++ b/backend/api/api_login.go @@ -0,0 +1,46 @@ +package api + +import "github.com/gin-gonic/gin" + +func init() { + addRoutes(func(self *HttpServer) { + self.router.GET("/login/*_", self.index) + self.router.POST("/login", self.loginPost) + }) +} + +type loginJsonModel struct { + Email string `json:"email" binding:"required"` + Password string `json:"password" binding:"required"` + Remember bool `json:"remember"` +} + +func (self *HttpServer) loginPost(c *gin.Context) { + var loginModel loginJsonModel + + if c.EnsureBody(&loginModel) { + if loginModel.Email == "manu" && loginModel.Password == "123" { + + session, _ := sessionStore.Get(c.Request, "grafana-session") + session.Values["login"] = true + session.Save(c.Request, c.Writer) + + c.JSON(200, gin.H{"status": "you are logged in"}) + } else { + c.JSON(401, gin.H{"status": "unauthorized"}) + } + } +} + +func (self *HttpServer) authMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + session, _ := sessionStore.Get(c.Request, "grafana-session") + + if c.Request.URL.Path != "/login" && session.Values["login"] == nil { + c.Writer.Header().Set("Location", "/login") + c.Abort(302) + } + + session.Save(c.Request, c.Writer) + } +} diff --git a/backend/api/api_models.go b/backend/api/api_models.go index 9d57a6b5fa2..f53d8cd0f4b 100644 --- a/backend/api/api_models.go +++ b/backend/api/api_models.go @@ -1,13 +1,13 @@ package api type saveDashboardCommand struct { - id string `json:"id"` - title string `json:"title"` - dashboard map[string]interface{} + Id string `json:"id"` + Title string `json:"title"` + Dashboard map[string]interface{} } type errorResponse struct { - message string `json:"message"` + Message string `json:"message"` } type indexViewModel struct { @@ -15,5 +15,5 @@ type indexViewModel struct { } func newErrorResponse(message string) *errorResponse { - return &errorResponse{message: message} + return &errorResponse{Message: message} } diff --git a/grafana b/grafana index c690d4aae84..bfe1ef07330 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit c690d4aae84fc39d7c4bf9a0a820a71f69059b78 +Subproject commit bfe1ef07330fcfcdbc3aa9ddd4eef827ee48f316