Draft: feat login

This commit is contained in:
2025-09-05 03:59:25 +08:00
parent 6d7074198f
commit b190a50e90
19 changed files with 589 additions and 83 deletions

View File

@@ -4,6 +4,7 @@ import (
"strings"
"gitea.konchin.com/ytshih/inp2025/game/cmd/serve"
"gitea.konchin.com/ytshih/inp2025/game/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -12,10 +13,12 @@ var RootCmd = &cobra.Command{
Use: "game",
Short: "Game for Intro. to Network Programming 2025",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.SetEnvKeyReplacer(strings.NewReplacer("_", "-"))
viper.BindPFlags(cmd.PersistentFlags())
viper.BindPFlags(cmd.Flags())
utils.ReadConfig(ctx)
},
}

View File

@@ -1,23 +1,31 @@
package serve
import (
"database/sql"
"log"
"net/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
httpSwagger "github.com/swaggo/http-swagger"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/sqliteshim"
"github.com/uptrace/bun/extra/bunotel"
"github.com/uptrace/bunrouter"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
_ "gitea.konchin.com/ytshih/inp2025/game/backend/docs"
"gitea.konchin.com/ytshih/inp2025/game/backend/handlers/auth"
"gitea.konchin.com/ytshih/inp2025/game/backend/middlewares"
"gitea.konchin.com/ytshih/inp2025/game/implements"
"gitea.konchin.com/ytshih/inp2025/game/tracing"
"gitea.konchin.com/ytshih/inp2025/game/utils"
)
// @title NASAOJ v3
// @version 2.0
// @title Intro. to Network Programming Game
// @version 0.0.1-alpha
// @license.name 0BSD
// @BasePath /
var backendCmd = &cobra.Command{
@@ -27,21 +35,49 @@ var backendCmd = &cobra.Command{
ctx := cmd.Context()
tracing.InitTracer("game backend")
tracing.Logger.Ctx(ctx).
Debug("connect to sql",
zap.String("sql.url",
"file:"+viper.GetString("sqlite-file")+"?cache=shared&mode=rwc"))
sqldb, err := sql.Open(sqliteshim.ShimName,
"file:"+viper.GetString("sqlite-file")+"?cache=shared&mode=rwc")
if err != nil {
tracing.Logger.Ctx(ctx).
Panic("failed to init sqlite",
zap.Error(err))
panic(err)
}
bunDB := bun.NewDB(sqldb, sqlitedialect.New())
bunDB.AddQueryHook(bunotel.NewQueryHook(bunotel.WithDBName("sqlite")))
if err := utils.InitDB(ctx, bunDB); err != nil {
tracing.Logger.Ctx(ctx).
Panic("failed to init db schema",
zap.Error(err))
panic(err)
}
db := implements.NewBunDatabase(bunDB)
authHandlers := auth.NewHandlers(db)
router := bunrouter.New()
if viper.GetBool("swagger") {
router.GET("/swagger/*any",
bunrouter.HTTPHandlerFunc(
httpSwagger.Handler()))
}
backend := router.NewGroup("").
Use(middlewares.ErrorHandler).
Use(middlewares.BunrouterOtel).
Use(middlewares.AccessLog)
tracing.Logger.Ctx(ctx).
Debug("nmsl",
zap.Bool("swagger", viper.GetBool("swagger")))
if viper.GetBool("swagger") {
backend.GET("/swagger/*any",
bunrouter.HTTPHandlerFunc(
httpSwagger.Handler()))
}
authGroup := backend.NewGroup("/auth")
authGroup.GET("/login",
authHandlers.GetLogin)
authGroup.POST("/register",
authHandlers.PostRegister)
// api := backend.NewGroup("/api")
tracing.Logger.Ctx(ctx).
Info("http server up",
@@ -57,4 +93,6 @@ func init() {
String("port", "8080", "Port to listen on")
backendCmd.Flags().
Bool("swagger", false, "Enable swagger")
backendCmd.Flags().
String("sqlite-file", ":memory:", "File for sqlite")
}