init project
Some checks failed
CI / build (push) Failing after 8m49s

This commit is contained in:
2025-11-13 16:53:08 +08:00
commit 937a3cea60
25 changed files with 1858 additions and 0 deletions

57
cmd/cobra.go Normal file
View File

@@ -0,0 +1,57 @@
package cmd
import (
"errors"
"fmt"
"os"
"github.com/mss-boot-io/mss-boot/pkg"
"github.com/spf13/cobra"
"service-http/cmd/migrate"
"service-http/cmd/server"
)
/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/10/31 16:37:31
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/10/31 16:37:31
*/
var rootCmd = &cobra.Command{
Use: "service-http",
Short: "service-http",
SilenceUsage: true,
Long: `service-http is a background management system developed by the mss-boot framework`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
tip()
return errors.New(pkg.Red("requires at least one arg"))
}
return nil
},
PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
Run: func(cmd *cobra.Command, args []string) {
tip()
},
}
func tip() {
usageStr := `欢迎使用 ` + pkg.Green(`service-http 0.0.1`) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
usageStr1 := `也可以参考 https://doc.mss-boot-io.top 的相关内容`
fmt.Printf("%s\n", usageStr)
fmt.Printf("%s\n", usageStr1)
}
func init() {
rootCmd.AddCommand(server.StartCmd)
rootCmd.AddCommand(migrate.StartCmd)
}
// Execute : apply commands
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(-1)
}
}

71
cmd/migrate/migrate.go Normal file
View File

@@ -0,0 +1,71 @@
package migrate
import (
"log/slog"
"path/filepath"
"github.com/mss-boot-io/mss-boot/pkg/config/gormdb"
"github.com/mss-boot-io/mss-boot/pkg/migration"
"github.com/mss-boot-io/mss-boot/pkg/migration/models"
"github.com/spf13/cobra"
_ "service-http/cmd/migrate/migration/custom"
_ "service-http/cmd/migrate/migration/system"
"service-http/config"
)
/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/10/31 16:37:31
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/10/31 16:37:31
*/
var (
generate bool
username string
password string
system bool
StartCmd = &cobra.Command{
Use: "migrate",
Short: "Initialize the database",
Example: "mss-boot-admin migrate",
RunE: func(cmd *cobra.Command, args []string) error {
return Run()
},
}
)
func init() {
StartCmd.PersistentFlags().BoolVarP(&system, "system", "s",
false, "generate system migration file")
StartCmd.PersistentFlags().BoolVarP(&generate, "generate", "g",
false, "generate migration file")
StartCmd.PersistentFlags().StringVarP(&username, "username", "u",
"admin", "system super administrator login username")
StartCmd.PersistentFlags().StringVarP(&password, "password", "p",
"admin", "system super administrator login password")
}
func Run() error {
if !generate {
slog.Info("start init")
config.Cfg.Init()
return migrate()
}
slog.Info(`generate migration file`)
return migration.GenFile(system, filepath.Join("cmd", "migrate", "migration"))
}
func migrate() error {
db := gormdb.DB
err := db.AutoMigrate(&models.Migration{})
if err != nil {
slog.Error("auto migrate error", slog.Any("err", err))
return err
}
migration.Migrate.SetModel(&models.Migration{})
migration.Migrate.SetDb(db)
migration.Migrate.Migrate()
return err
}

View File

@@ -0,0 +1,12 @@
package custom
/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/10/31 16:37:31
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/10/31 16:37:31
*/
// fixme: developer's custom migration script
func init() {
}

View File

@@ -0,0 +1,28 @@
package system
import (
"runtime"
"github.com/mss-boot-io/mss-boot/pkg/migration"
migrationModel "github.com/mss-boot-io/mss-boot/pkg/migration/models"
"gorm.io/gorm"
)
func init() {
_, fileName, _, _ := runtime.Caller(0)
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1691804837583Tables)
}
func _1691804837583Tables(db *gorm.DB, version string) error {
return db.Transaction(func(tx *gorm.DB) error {
err := tx.Migrator().AutoMigrate()
if err != nil {
return err
}
return tx.Create(&migrationModel.Migration{
Version: version,
}).Error
})
}

View File

@@ -0,0 +1,12 @@
package system
/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/10/31 16:37:31
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/10/31 16:37:31
*/
// fixme: developer's custom migration script
func init() {
}

58
cmd/server/server.go Normal file
View File

@@ -0,0 +1,58 @@
package server
import (
"context"
"github.com/gin-gonic/gin"
"github.com/mss-boot-io/mss-boot/core/server"
"github.com/mss-boot-io/mss-boot/core/server/listener"
"github.com/spf13/cobra"
"service-http/config"
"service-http/router"
)
/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/10/31 16:37:31
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/10/31 16:37:31
*/
var (
StartCmd = &cobra.Command{
Use: "server",
Short: "start server",
Long: "start service-http server",
Example: "service-http server",
PreRunE: func(cmd *cobra.Command, args []string) error {
return setup()
},
RunE: func(cmd *cobra.Command, args []string) error {
return run()
},
}
)
func setup() error {
// setup config
config.Cfg.Init()
r := gin.Default()
router.Init(r.Group("/"))
runnable := []server.Runnable{
config.Cfg.Server.Init(
listener.WithName("service-http"),
listener.WithHandler(r)),
}
server.Manage.Add(runnable...)
return nil
}
func run() error {
ctx := context.Background()
return server.Manage.Start(ctx)
}