diff --git a/internal/context/context.go b/internal/context/context.go index 6821171..9f0dc6c 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -48,7 +48,7 @@ func New(cfg *config.Config, db *database.Database) *Context { } // Return a Gin Context -func (ctx *Context) API(handler func(ctx *Context)) func(*gin.Context) { +func (ctx *Context) Route(handler func(ctx *Context)) func(*gin.Context) { return func(gctx *gin.Context) { context := Context{ Context: gctx, @@ -60,15 +60,17 @@ func (ctx *Context) API(handler func(ctx *Context)) func(*gin.Context) { } // Return an Error -func (ctx *Context) Error(err string) models.Error { - return models.Error{ +func (ctx *Context) Error(code int, err string) { + error := &models.Error{ Type: err, } + ctx.JSON(code, error) } // Return a Token -func (ctx *Context) Token(token string) models.Token { - return models.Token{ +func (ctx *Context) Token(token string) { + token_ := &models.Token{ Token: token, } + ctx.JSON(200, token_) } diff --git a/internal/router/api/v1/account/login.go b/internal/router/api/v1/account/login.go index 0ef0f66..aa3a738 100644 --- a/internal/router/api/v1/account/login.go +++ b/internal/router/api/v1/account/login.go @@ -29,7 +29,7 @@ import ( // @param register body v1.Register true "alice" "supersecretpassword" // @success 200 {object} models.Token // @failure 400 {object} models.Error "MissingBody | InvalidUsername | InvalidPassword" -// @failure 500 {object} models.Error "InternalServerError" +// @failure 500 {object} models.Error "DatabaseError | InternalServerError" // @router /v1/account/login [post] func Login(ctx *context.Context) { // Store body @@ -38,36 +38,36 @@ func Login(ctx *context.Context) { // Bind JSON err := ctx.BindJSON(&body) if err != nil { - ctx.JSON(400, ctx.Error("MissingBody")) + ctx.Error(400, "MissingBody") return } // Select account by username account, err := ctx.Db.Account.SelectByUsername(body.Username) if err != nil { - ctx.JSON(500, ctx.Error("InternalServerError")) + ctx.Error(500, "DatabaseError") return } // Compare username if account.Username != body.Username { - ctx.JSON(400, ctx.Error("InvalidUsername")) + ctx.Error(400, "InvalidUsername") return } // Compare password err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(body.Password)) if err != nil { - ctx.JSON(400, ctx.Error("InvalidPassword")) + ctx.Error(400, "InvalidPassword") return } // Generate token token, err := jwt.Encode(account.ID) if err != nil { - ctx.JSON(500, ctx.Error("InternalServerError")) + ctx.Error(500, "InternalServerError") return } - ctx.JSON(200, ctx.Token(token)) + ctx.Token(token) } diff --git a/internal/router/api/v1/account/register.go b/internal/router/api/v1/account/register.go index cfe91d7..a0bf3a6 100644 --- a/internal/router/api/v1/account/register.go +++ b/internal/router/api/v1/account/register.go @@ -30,12 +30,12 @@ import ( // @success 200 {object} models.Token // @failure 400 {object} models.Error "MissingBody | UsernameTaken" // @failure 403 {object} models.Error "RegistrationDisabled" -// @failure 500 {object} models.Error "InternalServerError" +// @failure 500 {object} models.Error "DatabaseError | InternalServerError" // @router /v1/account/register [post] func Register(ctx *context.Context) { // Check if registration is disabled if ctx.Config.Auth.Disabled { - ctx.JSON(403, ctx.Error("RegistrationDisabled")) + ctx.Error(403, "RegistrationDisabled") return } @@ -45,50 +45,50 @@ func Register(ctx *context.Context) { // Bind JSON err := ctx.BindJSON(&body) if err != nil { - ctx.JSON(400, ctx.Error("MissingBody")) + ctx.Error(400, "MissingBody") return } // Select account by username account, err := ctx.Db.Account.SelectByUsername(body.Username) if err != nil { - ctx.JSON(500, ctx.Error("InternalServerError")) + ctx.Error(500, "DatabaseError") return } // Compare username if account.Username == body.Username { - ctx.JSON(400, ctx.Error("UsernameTaken")) + ctx.Error(400, "UsernameTaken") return } // Hash password password, err := bcrypt.GenerateFromPassword([]byte(body.Password), ctx.Config.Auth.Cost) if err != nil { - ctx.JSON(500, ctx.Error("InternalServerError")) + ctx.Error(500, "InternalServerError") return } // Insert account err = ctx.Db.Account.Insert(body.Username, string(password)) if err != nil { - ctx.JSON(500, ctx.Error("InternalServerError")) + ctx.Error(500, "DatabaseError") return } // Select account by username account, err = ctx.Db.Account.SelectByUsername(body.Username) if err != nil { - ctx.JSON(500, ctx.Error("InternalServerError")) + ctx.Error(500, "DatabaseError") return } // Generate token token, err := jwt.Encode(account.ID) if err != nil { - ctx.JSON(500, ctx.Error("InternalServerError")) + ctx.Error(500, "InternalServerError") return } - ctx.JSON(200, ctx.Token(token)) + ctx.Token(token) } diff --git a/internal/router/api/v1/v1.go b/internal/router/api/v1/v1.go index cc922c0..0d11dfc 100644 --- a/internal/router/api/v1/v1.go +++ b/internal/router/api/v1/v1.go @@ -34,13 +34,13 @@ func Register(ctx *context.Context) { v1 := ctx.Group("v1") { - v1.GET("scalar", ctx.API(Spec)) - v1.GET("docs", ctx.API(Spec)) + v1.GET("scalar", ctx.Route(Spec)) + v1.GET("docs", ctx.Route(Spec)) } { a := v1.Group("account") - a.POST("login", ctx.API(account.Login)) - a.POST("register", ctx.API(account.Register)) + a.POST("login", ctx.Route(account.Login)) + a.POST("register", ctx.Route(account.Register)) } }