init
This commit is contained in:
commit
b10790c212
40 changed files with 4149 additions and 0 deletions
145
main.go
Normal file
145
main.go
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"caddydash/api"
|
||||
"caddydash/apic"
|
||||
"caddydash/config"
|
||||
"caddydash/db"
|
||||
"caddydash/gen"
|
||||
"caddydash/user"
|
||||
"crypto/rand"
|
||||
"encoding/gob"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/fenthope/record"
|
||||
"github.com/fenthope/sessions"
|
||||
"github.com/fenthope/sessions/cookie"
|
||||
"github.com/infinite-iroha/touka"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
cfgfile string
|
||||
cdb *db.ConfigDB
|
||||
sessionKey []byte
|
||||
)
|
||||
|
||||
func init() {
|
||||
parseFlags()
|
||||
loadConfig()
|
||||
loadDatabase(cfg.DB.Filepath)
|
||||
loadtmpltoDB(cfg.Tmpl.Path, cdb)
|
||||
loadAdminStatus(cdb)
|
||||
initSessionKey()
|
||||
}
|
||||
|
||||
func parseFlags() {
|
||||
//posix
|
||||
flag.StringVar(&cfgfile, "c", "./config.toml", "Path to the configuration file")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func loadConfig() {
|
||||
var err error
|
||||
cfg, err = config.LoadConfig(cfgfile)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to load config: %v\n", err)
|
||||
// 如果配置文件加载失败,也显示帮助信息并退出
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
if cfg != nil && cfg.Server.Debug { // 确保 cfg 不为 nil
|
||||
fmt.Println("Config File Path: ", cfgfile)
|
||||
fmt.Printf("Loaded config: %v\n", cfg)
|
||||
}
|
||||
fmt.Printf("Loaded config: %v\n", cfg)
|
||||
}
|
||||
|
||||
func loadDatabase(filepath string) {
|
||||
var err error
|
||||
cdb, err = db.InitDB(filepath)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to initialize database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func loadAdminStatus(cdb *db.ConfigDB) {
|
||||
err := user.InitAdminUserStatus(cdb)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to initialize admin user status: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func loadtmpltoDB(path string, cdb *db.ConfigDB) {
|
||||
err := gen.ReadTmplToDB(path, cdb)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to load templates: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = gen.Add80SiteConfig(cfg, cdb)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to add :80 site config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func initSessionKey() {
|
||||
// crypto 生成随机
|
||||
sessionKey = make([]byte, 32)
|
||||
_, err := rand.Read(sessionKey)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to generate session key: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer cdb.CloseDB()
|
||||
|
||||
r := touka.Default()
|
||||
r.Use(record.Middleware())
|
||||
|
||||
store := cookie.NewStore(sessionKey)
|
||||
store.Options(sessions.Options{
|
||||
Path: "/",
|
||||
MaxAge: 10800, // 3 hours
|
||||
HttpOnly: true,
|
||||
})
|
||||
r.Use(sessions.Sessions("mysession", store))
|
||||
// 应用 session 中间件
|
||||
r.Use(api.SessionMiddleware(cdb))
|
||||
|
||||
v0 := r.Group("/v0")
|
||||
api.ApiGroup(v0, cdb, cfg)
|
||||
|
||||
gob.Register(map[string]interface{}{})
|
||||
gob.Register(time.Time{})
|
||||
gob.Register(gen.CaddyUniConfig{})
|
||||
|
||||
frontendFS := os.DirFS("frontend")
|
||||
r.SetUnMatchFS(http.FS(frontendFS))
|
||||
|
||||
// 打印定义的路由
|
||||
fmt.Println("Registered Routes:")
|
||||
for _, info := range r.GetRouterInfo() {
|
||||
fmt.Printf(" Method: %-7s Path: %-25s Handler: %-40s Group: %s\n", info.Method, info.Path, info.Handler, info.Group)
|
||||
}
|
||||
|
||||
go func() {
|
||||
err := apic.RunCaddy(cfg)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to start caddy: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||
r.Run(addr)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue