Init: bootstrap go module
This commit is contained in:
26
backend/docs/docs.go
Normal file
26
backend/docs/docs.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Code generated by swaggo/swag. DO NOT EDIT.
|
||||
|
||||
package docs
|
||||
|
||||
import "github.com/swaggo/swag/v2"
|
||||
|
||||
const docTemplate = `{
|
||||
"schemes": {{ marshal .Schemes }},"swagger":"2.0","info":{"description":"{{escape .Description}}","title":"{{.Title}}","contact":{},"license":{"name":"0BSD"},"version":"{{.Version}}"},"host":"{{.Host}}","basePath":"{{.BasePath}}","paths":{}}`
|
||||
|
||||
// SwaggerInfo holds exported Swagger Info so clients can modify it
|
||||
var SwaggerInfo = &swag.Spec{
|
||||
Version: "2.0",
|
||||
Host: "",
|
||||
BasePath: "/",
|
||||
Schemes: []string{},
|
||||
Title: "NASAOJ v3",
|
||||
Description: "",
|
||||
InfoInstanceName: "swagger",
|
||||
SwaggerTemplate: docTemplate,
|
||||
LeftDelim: "{{",
|
||||
RightDelim: "}}",
|
||||
}
|
||||
|
||||
func init() {
|
||||
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
|
||||
}
|
||||
1
backend/docs/swagger.json
Normal file
1
backend/docs/swagger.json
Normal file
@@ -0,0 +1 @@
|
||||
{"swagger":"2.0","info":{"title":"NASAOJ v3","contact":{},"license":{"name":"0BSD"},"version":"2.0"},"basePath":"/","paths":{}}
|
||||
9
backend/docs/swagger.yaml
Normal file
9
backend/docs/swagger.yaml
Normal file
@@ -0,0 +1,9 @@
|
||||
basePath: /
|
||||
info:
|
||||
contact: {}
|
||||
license:
|
||||
name: 0BSD
|
||||
title: NASAOJ v3
|
||||
version: "2.0"
|
||||
paths: {}
|
||||
swagger: "2.0"
|
||||
1
backend/handlers/auth/getLogin.go
Normal file
1
backend/handlers/auth/getLogin.go
Normal file
@@ -0,0 +1 @@
|
||||
package auth
|
||||
16
backend/middlewares/accessLog.go
Normal file
16
backend/middlewares/accessLog.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/tracing"
|
||||
"github.com/uptrace/bunrouter"
|
||||
)
|
||||
|
||||
func 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)
|
||||
}
|
||||
}
|
||||
48
backend/middlewares/bunrouterOtel.go
Normal file
48
backend/middlewares/bunrouterOtel.go
Normal file
@@ -0,0 +1,48 @@
|
||||
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 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)
|
||||
}
|
||||
}
|
||||
88
backend/middlewares/errorHandler.go
Normal file
88
backend/middlewares/errorHandler.go
Normal file
@@ -0,0 +1,88 @@
|
||||
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:"-"`
|
||||
|
||||
TraceID string `json:"traceId"`
|
||||
}
|
||||
|
||||
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 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)
|
||||
httpErr.TraceID = span.SpanContext().TraceID().String()
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user