add account routes

This commit is contained in:
celogeek 2022-01-30 14:50:53 +01:00
parent 0ccfceb5b3
commit 770b5e14d0
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
4 changed files with 62 additions and 0 deletions

1
go.mod
View File

@ -25,5 +25,6 @@ require (
github.com/ugorji/go/codec v1.1.7 // indirect github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect
) )

2
go.sum
View File

@ -57,6 +57,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 h1:EFLtLCwd8tGN+r/ePz3cvRtdsfYNhDEdt/vp6qsT+0A=
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -0,0 +1,53 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"gopkg.in/validator.v2"
)
func (s *Service) Login(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{
"status": "todo",
})
}
func (s *Service) Logout(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{
"status": "todo",
})
}
type SignupRequest struct {
Login string `validate:"min=3,max=40,regexp=^[a-zA-Z0-9]*$"`
Password string `validate:"min=8,max=40"`
}
func (s *Service) Signup(c *gin.Context) {
var account *SignupRequest
if c.Request.ContentLength == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "missing body",
})
return
}
if err := c.ShouldBindJSON(&account); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
if err := validator.Validate(account); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}

View File

@ -48,6 +48,12 @@ func New(logger *log.Logger, config *ServiceConfig) *Service {
func (s *Service) SetupRoutes() { func (s *Service) SetupRoutes() {
s.Gin.Use(gin.Logger()) s.Gin.Use(gin.Logger())
s.Gin.Use(s.Recovery) s.Gin.Use(s.Recovery)
ac := s.Gin.Group("/account")
ac.POST("/signup", s.Signup)
ac.POST("/login", s.Login)
ac.POST("/logout", s.Logout)
s.Gin.NoRoute(func(c *gin.Context) { s.Gin.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{ c.JSON(http.StatusNotFound, gin.H{
"error": "this route doesn't exists", "error": "this route doesn't exists",