-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (98 loc) · 2.3 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
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
apis "github.com/alvan/opsul/api/index"
"github.com/alvan/opsul/app"
"github.com/gin-gonic/gin"
)
var (
conf = flag.String("conf", "etc/opsul.json", "Configuration file")
host = flag.String("host", "", "Server host")
port = flag.Int("port", 9900, "Server port")
)
func serv() *gin.Engine {
engine := gin.Default()
if app.Store.Files.Use {
func(path string) {
engine.StaticFile("/", path)
list, _ := os.ReadDir(path)
for _, file := range list {
if !strings.HasPrefix(file.Name(), ".") {
if file.IsDir() {
engine.Static("/"+file.Name(), filepath.Join(path, file.Name()))
} else {
engine.StaticFile("/"+file.Name(), filepath.Join(path, file.Name()))
}
}
}
}(app.Store.Files.Dir)
}
if app.Store.Webui.Use {
engine.SetFuncMap(template.FuncMap{
"join": strings.Join,
"json": json.Marshal,
})
engine.LoadHTMLFiles(func(path string) (list []string) {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".tmpl" {
list = append(list, path)
}
return nil
})
return
}(app.Store.Webui.Dir)...)
engine.Group("/web", gin.BasicAuth(app.Store.AuthBasicUsers())).GET("/*path", func(ctx *gin.Context) {
user := app.Store.FindUserByName(ctx.GetString(gin.AuthUserKey))
path := ctx.Param("path")
if path == "/" {
path = "/index"
}
path = "/web" + path
ctx.HTML(http.StatusOK, "/web/index", gin.H{
"store": app.Store,
"state": gin.H{
"path": path,
"user": user,
},
})
})
}
apis.Index(engine)
return engine
}
func main() {
flag.Parse()
if *conf == "" {
flag.PrintDefaults()
return
}
if err := app.Store.Load(*conf); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := app.Tmpfs.Init(app.Store.Tmpfs.Dir, app.Store.Tmpfs.Pre); err != nil {
fmt.Println(err)
os.Exit(1)
}
sig := make(chan os.Signal, 2)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
<-sig
app.Tmpfs.Done()
os.Exit(1)
}()
if app.Store.Https.Use {
serv().RunTLS(fmt.Sprintf("%s:%d", *host, *port), app.Store.Https.Crt, app.Store.Https.Key)
} else {
serv().Run(fmt.Sprintf("%s:%d", *host, *port))
}
}