api_register.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package api
  2. import (
  3. log "github.com/alecthomas/log4go"
  4. "github.com/gin-gonic/gin"
  5. "github.com/torkelo/grafana-pro/pkg/models"
  6. )
  7. func init() {
  8. addRoutes(func(self *HttpServer) {
  9. self.router.GET("/register/*_", self.index)
  10. self.router.POST("/api/register/user", self.registerUserPost)
  11. })
  12. }
  13. type registerAccountJsonModel struct {
  14. Email string `json:"email" binding:"required"`
  15. Password string `json:"password" binding:"required"`
  16. Password2 bool `json:"remember2"`
  17. }
  18. func (self *HttpServer) registerUserPost(c *gin.Context) {
  19. var registerModel registerAccountJsonModel
  20. if !c.EnsureBody(&registerModel) {
  21. c.JSON(400, gin.H{"status": "bad request"})
  22. return
  23. }
  24. account := models.Account{
  25. UserName: registerModel.Email,
  26. Login: registerModel.Email,
  27. Email: registerModel.Email,
  28. Password: registerModel.Password,
  29. }
  30. err := self.store.CreateAccount(&account)
  31. if err != nil {
  32. log.Error("Failed to create user account, email: %v, error: %v", registerModel.Email, err)
  33. c.JSON(500, gin.H{"status": "failed to create account"})
  34. return
  35. }
  36. c.JSON(200, gin.H{"status": "ok"})
  37. }