Rename context functions

This commit is contained in:
perp 2024-06-04 16:00:53 +01:00
parent 510d71ef33
commit 29f0900068
4 changed files with 28 additions and 26 deletions

View File

@ -48,7 +48,7 @@ func New(cfg *config.Config, db *database.Database) *Context {
} }
// Return a Gin 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) { return func(gctx *gin.Context) {
context := Context{ context := Context{
Context: gctx, Context: gctx,
@ -60,15 +60,17 @@ func (ctx *Context) API(handler func(ctx *Context)) func(*gin.Context) {
} }
// Return an Error // Return an Error
func (ctx *Context) Error(err string) models.Error { func (ctx *Context) Error(code int, err string) {
return models.Error{ error := &models.Error{
Type: err, Type: err,
} }
ctx.JSON(code, error)
} }
// Return a Token // Return a Token
func (ctx *Context) Token(token string) models.Token { func (ctx *Context) Token(token string) {
return models.Token{ token_ := &models.Token{
Token: token, Token: token,
} }
ctx.JSON(200, token_)
} }

View File

@ -29,7 +29,7 @@ import (
// @param register body v1.Register true "alice" "supersecretpassword" // @param register body v1.Register true "alice" "supersecretpassword"
// @success 200 {object} models.Token // @success 200 {object} models.Token
// @failure 400 {object} models.Error "MissingBody | InvalidUsername | InvalidPassword" // @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] // @router /v1/account/login [post]
func Login(ctx *context.Context) { func Login(ctx *context.Context) {
// Store body // Store body
@ -38,36 +38,36 @@ func Login(ctx *context.Context) {
// Bind JSON // Bind JSON
err := ctx.BindJSON(&body) err := ctx.BindJSON(&body)
if err != nil { if err != nil {
ctx.JSON(400, ctx.Error("MissingBody")) ctx.Error(400, "MissingBody")
return return
} }
// Select account by username // Select account by username
account, err := ctx.Db.Account.SelectByUsername(body.Username) account, err := ctx.Db.Account.SelectByUsername(body.Username)
if err != nil { if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError")) ctx.Error(500, "DatabaseError")
return return
} }
// Compare username // Compare username
if account.Username != body.Username { if account.Username != body.Username {
ctx.JSON(400, ctx.Error("InvalidUsername")) ctx.Error(400, "InvalidUsername")
return return
} }
// Compare password // Compare password
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(body.Password)) err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(body.Password))
if err != nil { if err != nil {
ctx.JSON(400, ctx.Error("InvalidPassword")) ctx.Error(400, "InvalidPassword")
return return
} }
// Generate token // Generate token
token, err := jwt.Encode(account.ID) token, err := jwt.Encode(account.ID)
if err != nil { if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError")) ctx.Error(500, "InternalServerError")
return return
} }
ctx.JSON(200, ctx.Token(token)) ctx.Token(token)
} }

View File

@ -30,12 +30,12 @@ import (
// @success 200 {object} models.Token // @success 200 {object} models.Token
// @failure 400 {object} models.Error "MissingBody | UsernameTaken" // @failure 400 {object} models.Error "MissingBody | UsernameTaken"
// @failure 403 {object} models.Error "RegistrationDisabled" // @failure 403 {object} models.Error "RegistrationDisabled"
// @failure 500 {object} models.Error "InternalServerError" // @failure 500 {object} models.Error "DatabaseError | InternalServerError"
// @router /v1/account/register [post] // @router /v1/account/register [post]
func Register(ctx *context.Context) { func Register(ctx *context.Context) {
// Check if registration is disabled // Check if registration is disabled
if ctx.Config.Auth.Disabled { if ctx.Config.Auth.Disabled {
ctx.JSON(403, ctx.Error("RegistrationDisabled")) ctx.Error(403, "RegistrationDisabled")
return return
} }
@ -45,50 +45,50 @@ func Register(ctx *context.Context) {
// Bind JSON // Bind JSON
err := ctx.BindJSON(&body) err := ctx.BindJSON(&body)
if err != nil { if err != nil {
ctx.JSON(400, ctx.Error("MissingBody")) ctx.Error(400, "MissingBody")
return return
} }
// Select account by username // Select account by username
account, err := ctx.Db.Account.SelectByUsername(body.Username) account, err := ctx.Db.Account.SelectByUsername(body.Username)
if err != nil { if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError")) ctx.Error(500, "DatabaseError")
return return
} }
// Compare username // Compare username
if account.Username == body.Username { if account.Username == body.Username {
ctx.JSON(400, ctx.Error("UsernameTaken")) ctx.Error(400, "UsernameTaken")
return return
} }
// Hash password // Hash password
password, err := bcrypt.GenerateFromPassword([]byte(body.Password), ctx.Config.Auth.Cost) password, err := bcrypt.GenerateFromPassword([]byte(body.Password), ctx.Config.Auth.Cost)
if err != nil { if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError")) ctx.Error(500, "InternalServerError")
return return
} }
// Insert account // Insert account
err = ctx.Db.Account.Insert(body.Username, string(password)) err = ctx.Db.Account.Insert(body.Username, string(password))
if err != nil { if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError")) ctx.Error(500, "DatabaseError")
return return
} }
// Select account by username // Select account by username
account, err = ctx.Db.Account.SelectByUsername(body.Username) account, err = ctx.Db.Account.SelectByUsername(body.Username)
if err != nil { if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError")) ctx.Error(500, "DatabaseError")
return return
} }
// Generate token // Generate token
token, err := jwt.Encode(account.ID) token, err := jwt.Encode(account.ID)
if err != nil { if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError")) ctx.Error(500, "InternalServerError")
return return
} }
ctx.JSON(200, ctx.Token(token)) ctx.Token(token)
} }

View File

@ -34,13 +34,13 @@ func Register(ctx *context.Context) {
v1 := ctx.Group("v1") v1 := ctx.Group("v1")
{ {
v1.GET("scalar", ctx.API(Spec)) v1.GET("scalar", ctx.Route(Spec))
v1.GET("docs", ctx.API(Spec)) v1.GET("docs", ctx.Route(Spec))
} }
{ {
a := v1.Group("account") a := v1.Group("account")
a.POST("login", ctx.API(account.Login)) a.POST("login", ctx.Route(account.Login))
a.POST("register", ctx.API(account.Register)) a.POST("register", ctx.Route(account.Register))
} }
} }