add global config struct
This commit is contained in:
parent
cc429c44f9
commit
cd1e1a42f3
9 changed files with 251 additions and 214 deletions
84
api/api.go
84
api/api.go
|
|
@ -4,7 +4,7 @@ import (
|
|||
"caddydash/apic"
|
||||
"caddydash/config"
|
||||
"caddydash/db"
|
||||
"caddydash/user"
|
||||
"caddydash/gen"
|
||||
|
||||
"github.com/infinite-iroha/touka"
|
||||
)
|
||||
|
|
@ -21,40 +21,43 @@ func ApiGroup(v0 touka.IRouter, cdb *db.ConfigDB, cfg *config.Config) {
|
|||
})
|
||||
|
||||
// 配置参数相关
|
||||
api.GET("/config/file/:filename", GetConfig(cdb)) // 读取配置(与写入一致)
|
||||
api.PUT("/config/file/:filename", PutConfig(cdb, cfg)) // 写入配置
|
||||
api.DELETE("/config/file/:filename", DeleteConfig(cdb, cfg)) //删除配置
|
||||
cfgr := api.Group("/config")
|
||||
{
|
||||
cfgr.GET("/file/:filename", GetConfig(cdb)) // 读取配置(与写入一致)
|
||||
cfgr.PUT("/file/:filename", PutConfig(cdb, cfg)) // 写入配置
|
||||
cfgr.DELETE("/file/:filename", DeleteConfig(cdb, cfg)) //删除配置
|
||||
|
||||
api.GET("/config/files/params", FilesParams(cdb)) // 获取所有配置, 需进行decode
|
||||
api.GET("/config/files/templates", FilesTemplates(cdb)) // 获取所有模板
|
||||
api.GET("/config/files/rendered", FilesRendered(cdb)) // 获取所有渲染产物
|
||||
cfgr.GET("/files/params", FilesParams(cdb)) // 获取所有配置, 需进行decode
|
||||
cfgr.GET("/files/templates", FilesTemplates(cdb)) // 获取所有模板
|
||||
cfgr.GET("/files/rendered", FilesRendered(cdb)) // 获取所有渲染产物
|
||||
|
||||
api.GET("/config/templates", GetTemplates(cdb)) // 获取可用模板名称
|
||||
cfgr.GET("/templates", GetTemplates(cdb)) // 获取可用模板名称
|
||||
|
||||
api.GET("/config/headers-presets", func(c *touka.Context) {
|
||||
c.JSON(200, GetHeaderSetMetadataList())
|
||||
})
|
||||
cfgr.GET("/headers-presets", func(c *touka.Context) {
|
||||
c.JSON(200, GetHeaderSetMetadataList())
|
||||
})
|
||||
cfgr.GET("/headers-presets/:name", GetHeadersPreset())
|
||||
|
||||
api.GET("/config/headers-presets/:name", func(c *touka.Context) {
|
||||
presetName := c.Param("name")
|
||||
if presetName == "" {
|
||||
c.JSON(400, touka.H{"error": "presetName is required"})
|
||||
return
|
||||
glbr := api.Group("/global")
|
||||
{
|
||||
glbr.GET("/log/levels", func(c *touka.Context) {
|
||||
c.JSON(200, gen.LogLevelList)
|
||||
})
|
||||
glbr.GET("/tls/providers", func(c *touka.Context) {
|
||||
c.JSON(200, gen.ProviderList)
|
||||
})
|
||||
}
|
||||
preset, found := GetHeaderSetByID(presetName)
|
||||
if !found {
|
||||
c.JSON(404, touka.H{"error": "preset not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(200, preset)
|
||||
})
|
||||
}
|
||||
|
||||
// caddy实例相关
|
||||
api.POST("/caddy/stop", apic.StopCaddy()) // 无需payload
|
||||
api.POST("/caddy/run", apic.StartCaddy(cfg))
|
||||
api.POST("/caddy/restart", apic.RestartCaddy(cfg))
|
||||
api.GET("/caddy/status", apic.IsCaddyRunning())
|
||||
{
|
||||
api.POST("/caddy/stop", apic.StopCaddy()) // 无需payload
|
||||
api.POST("/caddy/run", apic.StartCaddy(cfg))
|
||||
api.POST("/caddy/restart", apic.RestartCaddy(cfg))
|
||||
api.GET("/caddy/status", apic.IsCaddyRunning())
|
||||
}
|
||||
|
||||
// 鉴权相关
|
||||
auth := api.Group("/auth")
|
||||
{
|
||||
auth.POST("/login", func(c *touka.Context) {
|
||||
|
|
@ -66,31 +69,8 @@ func ApiGroup(v0 touka.IRouter, cdb *db.ConfigDB, cfg *config.Config) {
|
|||
auth.GET("/logout", func(c *touka.Context) {
|
||||
AuthLogout(c)
|
||||
})
|
||||
auth.GET("/init", func(c *touka.Context) {
|
||||
// 返回是否init管理员
|
||||
isInit := user.IsAdminInit()
|
||||
if isInit {
|
||||
c.JSON(200, touka.H{"admin_init": true})
|
||||
} else {
|
||||
c.JSON(200, touka.H{"admin_init": false})
|
||||
}
|
||||
})
|
||||
auth.POST("/init", func(c *touka.Context) {
|
||||
username := c.PostForm("username")
|
||||
password := c.PostForm("password")
|
||||
// 验证是否为空
|
||||
if username == "" || password == "" {
|
||||
c.JSON(400, touka.H{"error": "username and password are required"})
|
||||
return
|
||||
}
|
||||
// 初始化管理员
|
||||
err := user.InitAdminUser(username, password, cdb)
|
||||
if err != nil {
|
||||
c.JSON(500, touka.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, touka.H{"message": "admin initialized"})
|
||||
})
|
||||
auth.GET("/init", AuthInitStatus())
|
||||
auth.POST("/init", AuthInitHandle(cdb))
|
||||
auth.POST("resetpwd", ResetPassword(cdb))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue