-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathui.go
77 lines (63 loc) · 1.89 KB
/
ui.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
package ui
import (
"dhtc/config"
"github.com/gin-gonic/contrib/renders/multitemplate"
"github.com/gin-gonic/gin"
"github.com/leekchan/gtf"
"github.com/ostafen/clover/v2"
"html/template"
"io/fs"
"net/http"
"strings"
)
type Controller struct {
Database *clover.DB
Configuration *config.Configuration
}
func loadTemplates() multitemplate.Render {
renderer := multitemplate.New()
viewDirectory, _ := templates.ReadDir("templates/view")
includeDirectory, _ := templates.ReadDir("templates/include")
var includeFiles []string
for _, includeFile := range includeDirectory {
includeFiles = append(includeFiles, "templates/include/"+includeFile.Name())
}
for _, viewPath := range viewDirectory {
if viewPath.IsDir() {
continue
}
viewFileName := viewPath.Name()
viewName := strings.TrimSuffix(viewFileName, ".html")
tpl := template.Must(gtf.New(viewName).ParseFS(templates, append(includeFiles, "templates/view/"+viewFileName)...))
renderer.Add(viewName, tpl)
}
return renderer
}
func RunWebServer(configuration *config.Configuration, database *clover.DB) {
// gin.SetMode(gin.ReleaseMode)
srv := gin.Default()
srv.HTMLRender = loadTemplates()
_ = srv.SetTrustedProxies(nil)
uiCtrl := Controller{
Database: database,
Configuration: configuration,
}
srv.GET("", uiCtrl.Dashboard)
srv.GET("/dashboard", uiCtrl.Dashboard)
srv.GET("/search", uiCtrl.SearchGet)
srv.POST("/search", uiCtrl.SearchPost)
srv.GET("/discover", uiCtrl.DiscoverGet)
srv.POST("/discover", uiCtrl.DiscoverPost)
srv.GET("/watches", uiCtrl.WatchGet)
srv.POST("/watches", uiCtrl.WatchPost)
srv.GET("/blacklist", uiCtrl.BlacklistGet)
srv.POST("/blacklist", uiCtrl.BlacklistPost)
css, _ := fs.Sub(static, "static/css")
js, _ := fs.Sub(static, "static/js")
srv.StaticFS("/css", http.FS(css))
srv.StaticFS("/js", http.FS(js))
err := srv.Run(configuration.Address)
if err != nil {
return
}
}