Draft: big refactor

This commit is contained in:
2025-09-16 16:03:27 +08:00
parent f527230f1e
commit 7ee2163a95
33 changed files with 338 additions and 155 deletions

View File

@@ -0,0 +1,18 @@
package middlewares
import (
"net/http"
"gitea.konchin.com/ytshih/inp2025/game/tracing"
"github.com/uptrace/bunrouter"
)
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())
return next(w, req)
}
}

View 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))
}
}

View File

@@ -0,0 +1,50 @@
package middlewares
import (
"net/http"
"gitea.konchin.com/ytshih/inp2025/game/utils"
"github.com/uptrace/bunrouter"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
)
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() {
return next(w, req)
}
params := req.Params()
span.SetName(req.Method + " " + params.Route())
paramSlice := params.Slice()
attrs := make([]attribute.KeyValue, 0, 3+len(paramSlice))
attrs = append(attrs, semconv.HTTPRouteKey.String(req.Route()))
if req.URL != nil {
attrs = append(attrs, semconv.HTTPTargetKey.String(req.URL.RequestURI()))
} else {
// This should never occur if the request was generated by the net/http
// package. Fail gracefully, if it does though.
attrs = append(attrs, semconv.HTTPTargetKey.String(req.RequestURI))
}
attrs = append(attrs, semconv.HTTPClientIPKey.String(
utils.GetRemoteAddr(req)))
for _, param := range paramSlice {
attrs = append(attrs, attribute.String("http.route.param."+param.Key,
param.Value))
}
span.SetAttributes(attrs...)
return next(w, req)
}
}

View File

@@ -0,0 +1,87 @@
package middlewares
import (
"net/http"
"gitea.konchin.com/ytshih/inp2025/game/tracing"
"github.com/uptrace/bunrouter"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
type HTTPError struct {
StatusCode int `json:"code"`
Message string `json:"message"`
OriginError error `json:"-"`
}
func (e HTTPError) Error() string {
return e.Message
}
func NewHTTPError(err error) HTTPError {
return HTTPError{
StatusCode: http.StatusInternalServerError,
Message: "Internal server error with unknown reason",
OriginError: err,
}
}
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()
var httpErr HTTPError
switch err := err.(type) {
case nil:
return nil
case HTTPError:
httpErr = err
default:
tracing.Logger.Ctx(ctx).
Error("unhandled error",
zap.Error(err))
httpErr = NewHTTPError(err)
}
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
span := trace.SpanFromContext(ctx)
span.SetAttributes(
attribute.Int("http.response.status_code", httpErr.StatusCode),
)
if 500 <= httpErr.StatusCode && httpErr.StatusCode <= 599 {
span.SetStatus(codes.Error, err.Error())
if httpErr.OriginError == nil {
tracing.Logger.Ctx(ctx).
Error(httpErr.Message)
} else {
tracing.Logger.Ctx(ctx).
Error(httpErr.Message,
zap.Error(httpErr.OriginError))
}
} else {
span.SetStatus(codes.Ok, "")
if httpErr.OriginError == nil {
tracing.Logger.Ctx(ctx).
Warn(httpErr.Message)
} else {
tracing.Logger.Ctx(ctx).
Warn(httpErr.Message,
zap.Error(httpErr.OriginError))
}
}
w.WriteHeader(httpErr.StatusCode)
_ = bunrouter.JSON(w, httpErr)
return err
}
}

View 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}
}