Init: bootstrap
This commit is contained in:
24
cmds/root.go
Normal file
24
cmds/root.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "root",
|
||||
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(serveCmd)
|
||||
}
|
||||
115
cmds/serve.go
Normal file
115
cmds/serve.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"gitea.konchin.com/go2025/backend/tracing"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
|
||||
_ "gitea.konchin.com/go2025/backend/docs"
|
||||
)
|
||||
|
||||
// @title Golang 2025 Final Project
|
||||
// @version 0.0.1
|
||||
// @termsOfService http://swagger.io/terms
|
||||
//
|
||||
// @license.name 0BSD
|
||||
// @basePath /
|
||||
var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx := cmd.Context()
|
||||
|
||||
// Initialize tracing
|
||||
appname := "go2025-backend"
|
||||
tracing.InitTracer(appname)
|
||||
if viper.GetString("uptrace-dsn") != "" {
|
||||
tracing.InitUptrace(appname)
|
||||
defer tracing.DeferUptrace(ctx)
|
||||
}
|
||||
|
||||
// Initialize DB instance
|
||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(
|
||||
viper.GetString("pg-connection-string"))))
|
||||
bunDB := bun.NewDB(sqldb, pgdialect.New())
|
||||
bunDB.AddQueryHook(bunotel.NewQueryHook(bunotel.WithDBName("backend")))
|
||||
|
||||
// Initialize MinIO instance
|
||||
mc, err := minio.New(viper.GetString("minio-host"), &minio.Options{
|
||||
Creds: credentials.NewStaticV4(
|
||||
viper.GetString("minio-accesskey"),
|
||||
viper.GetString("minio-secretkey"),
|
||||
"",
|
||||
),
|
||||
Secure: viper.GetBool("minio-usessl"),
|
||||
})
|
||||
if err != nil {
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Error("failed to create minio client",
|
||||
zap.Error(err))
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := utils.InitMinIO(ctx, mc); err != nil {
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Error("failed to minio init",
|
||||
zap.Error(err))
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Initialize custom interfaces
|
||||
db := implements.NewBunDatabase(bunDB)
|
||||
s3 := implements.NewMinIOObjectStorage(mc)
|
||||
|
||||
// Initialize backend router
|
||||
router := bunrouter.New()
|
||||
backend := router.NewGroup("").
|
||||
Use(middlewares.ErrorHandler).
|
||||
Use(middlewares.BunrouterOtel).
|
||||
Use(middlewares.AccessLog).
|
||||
Use(middlewares.CORSHandler)
|
||||
|
||||
if viper.GetBool("swagger") {
|
||||
backend.GET("/swagger/*any",
|
||||
bunrouter.HTTPHandlerFunc(
|
||||
httpSwagger.Handler()))
|
||||
}
|
||||
|
||||
log.Println(http.ListenAndServe(
|
||||
":"+viper.GetString("port"),
|
||||
otelhttp.NewHandler(router, "")))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
serveCmd.Flags().
|
||||
String("port", "8080", "Port to listen on")
|
||||
|
||||
serveCmd.Flags().
|
||||
Bool("zap-production", true, "Toggle production log format")
|
||||
serveCmd.Flags().
|
||||
Bool("swagger", false, "Toggle swagger")
|
||||
|
||||
serveCmd.Flags().
|
||||
String("pg-connection-string",
|
||||
"postgres://go2025:go2025@pg:5432/go2025?sslmode=disable",
|
||||
"Postgres connection string")
|
||||
|
||||
backendCmd.Flags().
|
||||
String("minio-host", "minio:9000", "MinIO host[:port]")
|
||||
backendCmd.Flags().
|
||||
String("minio-bucket", "go2025", "MinIO bucket")
|
||||
backendCmd.Flags().
|
||||
String("minio-accesskey", "poop", "MinIO accesskey")
|
||||
backendCmd.Flags().
|
||||
String("minio-secretkey", "poop", "MinIO secretkey")
|
||||
backendCmd.Flags().
|
||||
Bool("minio-usessl", false, "MinIO usessl")
|
||||
|
||||
serveCmd.Flags().
|
||||
String("uptrace-dsn", "", "Uptrace DSN (disabled by default)")
|
||||
}
|
||||
Reference in New Issue
Block a user