You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devops/main.go

73 lines
1.5 KiB

3 months ago
package main
import (
3 months ago
"devops/build"
"devops/entities"
"devops/routes"
3 months ago
"html/template"
"io"
"log"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
3 months ago
"gorm.io/gorm/schema"
"zestack.dev/env"
3 months ago
"zestack.dev/slim"
)
type Template struct {
templates *template.Template
}
func (t *Template) Render(c slim.Context, w io.Writer, name string, data any) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
var err error
3 months ago
var db *gorm.DB
// 初始化环境变量
if err = env.Init(); err != nil {
3 months ago
log.Fatalln(err)
}
3 months ago
// 初始化数据库
if db, err = gorm.Open(sqlite.Open(env.String("DB_DSN", "devops.db")), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: env.String("DB_TABLE_PREFIX", "do_"),
},
TranslateError: env.Bool("DB_TRANSLATE_ERROR", true),
}); err != nil {
3 months ago
log.Fatalln(err)
3 months ago
} else if err = entities.Init(db); err != nil {
3 months ago
log.Fatalln(err)
}
3 months ago
// 启动构建程序
if err = build.Init(db); err != nil {
log.Fatalln(err)
}
3 months ago
3 months ago
// 初始化 http
3 months ago
s := slim.New()
s.Debug = true
s.Renderer = &Template{templates: template.Must(template.ParseGlob("views/*.gohtml"))}
3 months ago
s.ResetRouterCreator(func(s *slim.Slim) slim.Router {
return slim.NewRouter(slim.RouterConfig{
RoutingTrailingSlash: true,
})
})
3 months ago
s.Use(slim.Logging())
s.Use(slim.Recovery())
s.Use(slim.Static("public"))
3 months ago
// 注册路由
if err = routes.Init(db, s); err != nil {
log.Fatalln(err)
}
3 months ago
3 months ago
// 启动网络服务
3 months ago
log.Fatalln(s.Start(":5000"))
}