-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (155 loc) · 3.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"strings"
bootstrap "caddy-json-ui/bootstrap"
internal "caddy-json-ui/internal"
middleware "caddy-json-ui/middlewares"
types "caddy-json-ui/types"
utils "caddy-json-ui/utils"
"github.com/gin-gonic/gin"
)
func stashLatestCaddyConfig(jsonContent []byte) error {
err := os.WriteFile("./config/config.json", jsonContent, os.ModePerm)
if err != nil {
return err
}
return nil
}
func main() {
endpoint := os.Getenv("CADDY_API_ENDPOINT")
if endpoint == "" {
endpoint = bootstrap.CaddyAPIEndpoint
}
caddySrv := internal.NewCaddyServer(endpoint)
exist, existErr := utils.PathExists("./config/config.json")
if existErr != nil {
log.Print("Stat config dir failed")
log.Panic(existErr.Error())
return
}
if !exist {
err := os.Mkdir("./config", os.ModePerm)
if err != nil {
log.Print("Mkdir config dir failed")
log.Panic(err.Error())
return
} else {
err = os.WriteFile("./config/config.json", []byte("{\"admin\": {\"listen\": \"127.0.0.1:2019\"}}"), os.ModePerm)
if err != nil {
log.Print("Write config file failed")
log.Panic(err.Error())
return
}
}
}
gin.SetMode(gin.ReleaseMode)
apiEng := gin.New()
api := apiEng.Group("/api")
{
api.POST("/save", func(c *gin.Context) {
jsonByte, err := io.ReadAll(c.Request.Body)
if err != nil {
// Handle error
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"ok": 0,
"msg": err.Error(),
})
}
err = caddySrv.SetCaddyConfig(jsonByte)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"ok": 0,
"msg": err.Error(),
})
return
}
stashLatestCaddyConfig(jsonByte)
c.JSON(http.StatusOK, gin.H{
"ok": 1,
"msg": "Success",
})
})
api.GET("/load", func(c *gin.Context) {
data, err := caddySrv.GetCaddyConfig()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"ok": 0,
"msg": err.Error(),
})
return
}
c.JSON(200, gin.H{
"ok": 1,
"msg": "Success",
"data": string(data),
})
})
api.GET("/healthy", func(c *gin.Context) {
c.JSON(200, gin.H{
"ok": 1,
"msg": "healthy",
})
})
}
// load plugins' routes
pluginEng := gin.New()
pluginApi := pluginEng.Group("/plugins")
pluginConf := &types.PluginEnableConfig{}
_, existErr = utils.PathExists("./plugins.yml")
if existErr != nil {
log.Print("Stat plugins dir failed, the plugins route will be disabled")
pluginConf.Enable = false
} else {
err := utils.LoadYAML("./plugins.yml", pluginConf)
if err != nil {
log.Print("Stat plugins dir failed, the plugins route will be disabled")
pluginConf.Enable = false
}
}
if len(pluginConf.Plugins) > 0 {
for _, conf := range pluginConf.Plugins {
routeHandler, err := utils.LoadAndInvokeSomethingFromPlugin(conf.PluginPath)
if err != nil {
log.Printf("plugin %s is disabled due to load error: %s", conf.Name, err.Error())
} else {
// cannot be extract to outer types
handler, ok := routeHandler.(func(string) (func(*gin.Context), error))
if !ok {
fmt.Println(reflect.TypeOf(routeHandler))
log.Printf("plugin %s is disabled due to plugin file incorrect", conf.Name)
} else {
t, err := handler(conf.ConfigPath)
if err != nil {
log.Printf("plugin %s is disabled due to %s", conf.Name, err.Error())
} else {
pluginApi.Any(conf.Route, t)
}
}
}
}
}
pubEng := gin.New()
pubEng.Static("/", "./public")
r := gin.Default()
r.Use(middleware.MidWhitelistFilter(bootstrap.WhitelistPath))
r.Any("/*any", func(c *gin.Context) {
path := c.Param("any")
if strings.HasPrefix(path, "/api") {
apiEng.HandleContext(c)
} else if strings.HasPrefix(path, "/plugins") {
pluginEng.HandleContext(c)
} else {
pubEng.HandleContext(c)
}
})
e := r.Run(fmt.Sprintf(":%d", bootstrap.Port))
if e != nil {
log.Fatal(e)
}
}