working on login && auth

This commit is contained in:
Torkel Ödegaard
2014-08-12 15:07:20 +02:00
parent 56d449b2ce
commit e7ce371ee8
5 changed files with 55 additions and 26 deletions
+2 -19
View File
@@ -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")
+1 -1
View File
@@ -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
+46
View File
@@ -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)
}
}
+5 -5
View File
@@ -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}
}
+1 -1
Submodule grafana updated: c690d4aae8...bfe1ef0733