Init: bootstrap go module

This commit is contained in:
2025-09-04 11:01:05 +08:00
commit 6d7074198f
20 changed files with 658 additions and 0 deletions

26
cmd/root.go Normal file
View File

@@ -0,0 +1,26 @@
package cmd
import (
"strings"
"gitea.konchin.com/ytshih/inp2025/game/cmd/serve"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var RootCmd = &cobra.Command{
Use: "game",
Short: "Game for Intro. to Network Programming 2025",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.BindPFlags(cmd.PersistentFlags())
viper.BindPFlags(cmd.Flags())
},
}
func init() {
cobra.EnableTraverseRunHooks = true
RootCmd.AddCommand(serve.RootCmd)
}

60
cmd/serve/backend.go Normal file
View File

@@ -0,0 +1,60 @@
package serve
import (
"log"
"net/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
httpSwagger "github.com/swaggo/http-swagger"
"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/middlewares"
"gitea.konchin.com/ytshih/inp2025/game/tracing"
)
// @title NASAOJ v3
// @version 2.0
// @license.name 0BSD
// @BasePath /
var backendCmd = &cobra.Command{
Use: "backend",
Short: "Game backend",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
tracing.InitTracer("game backend")
router := bunrouter.New()
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()))
}
tracing.Logger.Ctx(ctx).
Info("http server up",
zap.String("http.port", viper.GetString("port")))
log.Println(http.ListenAndServe(":"+viper.GetString("port"),
otelhttp.NewHandler(router, "")))
},
}
func init() {
backendCmd.Flags().
String("port", "8080", "Port to listen on")
backendCmd.Flags().
Bool("swagger", false, "Enable swagger")
}

11
cmd/serve/root.go Normal file
View File

@@ -0,0 +1,11 @@
package serve
import "github.com/spf13/cobra"
var RootCmd = &cobra.Command{
Use: "serve",
}
func init() {
RootCmd.AddCommand(backendCmd)
}