注册代码
This commit is contained in:
parent
109f284a11
commit
ed7ed6cd12
@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/xmdhs/authlib-skin/model"
|
"github.com/xmdhs/authlib-skin/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleError(ctx context.Context, w http.ResponseWriter, msg string, code int, httpcode int) {
|
func handleError(ctx context.Context, w http.ResponseWriter, msg string, code model.APIStatus, httpcode int) {
|
||||||
w.WriteHeader(httpcode)
|
w.WriteHeader(httpcode)
|
||||||
b, err := json.Marshal(model.API[any]{Code: code, Msg: msg, Data: nil})
|
b, err := json.Marshal(model.API[any]{Code: code, Msg: msg, Data: nil})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,25 +1,35 @@
|
|||||||
package handle
|
package handle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/snowflake"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"github.com/xmdhs/authlib-skin/config"
|
||||||
"github.com/xmdhs/authlib-skin/db/mysql"
|
"github.com/xmdhs/authlib-skin/db/mysql"
|
||||||
"github.com/xmdhs/authlib-skin/model"
|
"github.com/xmdhs/authlib-skin/model"
|
||||||
|
"github.com/xmdhs/authlib-skin/service"
|
||||||
"github.com/xmdhs/authlib-skin/utils"
|
"github.com/xmdhs/authlib-skin/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Reg(l *slog.Logger, q mysql.Querier, v *validator.Validate) httprouter.Handle {
|
func Reg(l *slog.Logger, q mysql.Querier, v *validator.Validate, db *sql.DB, snow *snowflake.Node, c config.Config) httprouter.Handle {
|
||||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
u, err := utils.DeCodeBody[model.User](r.Body, v)
|
u, err := utils.DeCodeBody[model.User](r.Body, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.InfoContext(ctx, err.Error())
|
l.InfoContext(ctx, err.Error())
|
||||||
|
handleError(ctx, w, err.Error(), model.ErrInput, 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = service.Reg(ctx, u, q, db, snow, c)
|
||||||
|
if err != nil {
|
||||||
|
l.WarnContext(ctx, err.Error())
|
||||||
|
handleError(ctx, w, err.Error(), model.ErrService, 500)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
_ = u
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
type APIStatus int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
OK = 0
|
OK APIStatus = iota
|
||||||
|
ErrInput
|
||||||
|
ErrService
|
||||||
)
|
)
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
type API[T any] struct {
|
type API[T any] struct {
|
||||||
Code int `json:"code"`
|
Code APIStatus `json:"code"`
|
||||||
Data T `json:"data"`
|
Data T `json:"data"`
|
||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user