Draft: feat login
This commit is contained in:
@@ -7,7 +7,9 @@ import (
|
||||
"github.com/uptrace/bunrouter"
|
||||
)
|
||||
|
||||
func AccessLog(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
|
||||
func (self *Handlers) AccessLog(
|
||||
next bunrouter.HandlerFunc,
|
||||
) bunrouter.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
tracing.Logger.Ctx(req.Context()).
|
||||
Info(req.Method + " " + req.Params().Route())
|
||||
|
||||
56
backend/middlewares/auth.go
Normal file
56
backend/middlewares/auth.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/models"
|
||||
"gitea.konchin.com/ytshih/inp2025/game/tracing"
|
||||
"gitea.konchin.com/ytshih/inp2025/game/types"
|
||||
"github.com/uptrace/bunrouter"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (self *Handlers) Auth(
|
||||
next bunrouter.HandlerFunc,
|
||||
) bunrouter.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
ctx := req.Context()
|
||||
|
||||
username, password, ok := req.BasicAuth()
|
||||
if !ok {
|
||||
return HTTPError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Message: "basic auth wrong format",
|
||||
}
|
||||
}
|
||||
|
||||
dbUser, err := self.db.GetUser(ctx, username)
|
||||
if err != nil {
|
||||
return HTTPError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Message: "username not exist",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
|
||||
if dbUser.Password != password {
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Debug("password input",
|
||||
zap.String("input.password", password),
|
||||
zap.String("dbuser.password", dbUser.Password))
|
||||
return HTTPError{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Message: "password incorrect",
|
||||
}
|
||||
}
|
||||
|
||||
user := models.User{
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, types.User(""), user)
|
||||
return next(w, req.WithContext(ctx))
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,9 @@ import (
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func BunrouterOtel(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
|
||||
func (self *Handlers) BunrouterOtel(
|
||||
next bunrouter.HandlerFunc,
|
||||
) bunrouter.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
span := trace.SpanFromContext(req.Context())
|
||||
if !span.IsRecording() {
|
||||
|
||||
@@ -15,8 +15,6 @@ type HTTPError struct {
|
||||
StatusCode int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
OriginError error `json:"-"`
|
||||
|
||||
TraceID string `json:"traceId"`
|
||||
}
|
||||
|
||||
func (e HTTPError) Error() string {
|
||||
@@ -31,7 +29,9 @@ func NewHTTPError(err error) HTTPError {
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorHandler(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
|
||||
func (self *Handlers) ErrorHandler(
|
||||
next bunrouter.HandlerFunc,
|
||||
) bunrouter.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
err := next(w, req)
|
||||
ctx := req.Context()
|
||||
@@ -53,7 +53,6 @@ func ErrorHandler(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
|
||||
|
||||
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
|
||||
span := trace.SpanFromContext(ctx)
|
||||
httpErr.TraceID = span.SpanContext().TraceID().String()
|
||||
span.SetAttributes(
|
||||
attribute.Int("http.response.status_code", httpErr.StatusCode),
|
||||
)
|
||||
|
||||
11
backend/middlewares/handler.go
Normal file
11
backend/middlewares/handler.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package middlewares
|
||||
|
||||
import "gitea.konchin.com/ytshih/inp2025/game/interfaces"
|
||||
|
||||
type Handlers struct {
|
||||
db interfaces.Database
|
||||
}
|
||||
|
||||
func NewHandlers(db interfaces.Database) *Handlers {
|
||||
return &Handlers{db: db}
|
||||
}
|
||||
Reference in New Issue
Block a user