first commit

This commit is contained in:
2025-03-21 11:05:42 +08:00
commit 7dffc94035
1717 changed files with 724764 additions and 0 deletions

29
cmd/cmd.go Normal file
View File

@@ -0,0 +1,29 @@
package cmd
import (
"log"
"reflect"
"runtime"
)
type server interface {
ListenAndServe() error
}
func mustInit(fn func() error) {
err := fn()
if err != nil {
ptr := reflect.ValueOf(fn).Pointer()
fi := runtime.FuncForPC(ptr)
log.Fatalf("%s failed: %v", fi.Name(), err)
}
}
func mustInitAny[T any](s T, fn func(s T) error) {
err := fn(s)
if err != nil {
ptr := reflect.ValueOf(fn).Pointer()
fi := runtime.FuncForPC(ptr)
log.Fatalf("%s(T) failed: %v", fi.Name(), err)
}
}

57
cmd/manage.go Normal file
View File

@@ -0,0 +1,57 @@
package cmd
import (
"context"
"fmt"
"log"
"management/internal/config"
"management/internal/pkg/logger"
"management/internal/pkg/redis"
"management/internal/pkg/session"
"management/internal/pkg/snowflake"
"management/internal/tpl"
dbinit "management/internal/db/init"
db "management/internal/db/sqlc"
router "management/internal/router/manage"
"github.com/spf13/cobra"
)
var manageCmd = &cobra.Command{
Use: "manage",
Short: "Start management server",
Long: `A Service to management manage`,
Run: func(cmd *cobra.Command, args []string) {
err := runManage(cmd.Context())
if err != nil {
log.Fatalf("run manage failed: %v", err)
}
},
}
func init() {
manageCmd.Flags().StringVarP(&configPath, "config", "c", "", "Custom config file path")
rootCmd.AddCommand(manageCmd)
}
func runManage(ctx context.Context) error {
mustInitAny(configPath, config.Init)
logger.Init()
mustInitAny(ctx, db.NewStore)
// 初始化数据
dbinit.InitSeed()
mustInit(redis.Init)
session.Init()
mustInit(snowflake.Init)
// mustInit(token.NewPasetoMaker)
// mustInit(tencentoss.Init)
mustInit(tpl.Init)
address := fmt.Sprintf("%s:%d", config.File.App.Host, config.File.App.Port)
log.Printf("Starting manage server on %s", address)
server := InitServer(address, router.NewRouter())
return server.ListenAndServe()
}

25
cmd/root.go Normal file
View File

@@ -0,0 +1,25 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var configPath string
var rootCmd = &cobra.Command{
Use: "testpaper",
Short: "Start testpaper server",
Long: `testpaper service`,
Run: func(cmd *cobra.Command, args []string) {
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

19
cmd/server_other.go Normal file
View File

@@ -0,0 +1,19 @@
//go:build !windows
// +build !windows
package cmd
import (
"net/http"
"time"
"github.com/fvbock/endless"
)
func InitServer(address string, router http.Handler) server {
s := endless.NewServer(address, router)
s.ReadHeaderTimeout = 20 * time.Second
s.WriteTimeout = 20 * time.Second
s.MaxHeaderBytes = 1 << 20
return s
}

19
cmd/server_win.go Normal file
View File

@@ -0,0 +1,19 @@
//go:build windows
// +build windows
package cmd
import (
"net/http"
"time"
)
func InitServer(address string, router http.Handler) server {
return &http.Server{
Addr: address,
Handler: router,
ReadTimeout: 20 * time.Second,
WriteTimeout: 20 * time.Second,
MaxHeaderBytes: 1 << 20,
}
}