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

72 lines
1.5 KiB

package main
import (
"devops/build"
"devops/entities"
"devops/routes"
"html/template"
"io"
"log"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"zestack.dev/env"
"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
var db *gorm.DB
// 初始化环境变量
if err = env.Init(); err != nil {
log.Fatalln(err)
}
// 初始化数据库
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 {
log.Fatalln(err)
} else if err = entities.Init(db); err != nil {
log.Fatalln(err)
}
// 启动构建程序
if err = build.Init(db); err != nil {
log.Fatalln(err)
}
// 初始化 http
s := slim.New()
s.Debug = true
s.Renderer = &Template{templates: template.Must(template.ParseGlob("views/*.gohtml"))}
s.ResetRouterCreator(func(s *slim.Slim) slim.Router {
return slim.NewRouter(slim.RouterConfig{
RoutingTrailingSlash: true,
})
})
s.Use(slim.Logging())
s.Use(slim.Recovery())
s.Use(slim.Static("public"))
// 注册路由
if err = routes.Init(db, s); err != nil {
log.Fatalln(err)
}
// 启动网络服务
log.Fatalln(s.Start(":5000"))
}