-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (73 loc) · 1.89 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
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
"github.com/gin-gonic/gin"
"github.com/jwmwalrus/bnp/onerror"
"github.com/jwmwalrus/felice-n-franz/internal/base"
"github.com/jwmwalrus/felice-n-franz/internal/kafkian"
rtc "github.com/jwmwalrus/rtcycler"
"github.com/jwmwalrus/walkie"
)
//go:embed web/html/index.html
var indexHTML string
//go:embed public
var staticContent embed.FS
func main() {
rtc.Load(rtc.RTCycler{
AppDirName: base.AppDirName,
AppName: base.AppName,
Config: &base.UserConf,
})
cfg := base.Validate()
gl := walkie.New()
gin.DefaultWriter = gl.Writer()
r := gin.Default()
r.Use(gl.ToFile())
r.Use(gin.Recovery())
route(r)
fmt.Printf("\n Running %v on %v\n", base.AppName, cfg.GetURL())
r.Run(cfg.GetPort())
}
func route(r *gin.Engine) *gin.Engine {
sc, err := fs.Sub(staticContent, "public")
onerror.Panic(err)
r.StaticFS("/static", http.FS(sc))
t, _ := template.New("index.html").Parse(indexHTML)
r.SetHTMLTemplate(t)
r.GET("/", index)
r.GET("/envs/:envname", getEnv)
r.GET("/ws", webSocket)
return r
}
func index(c *gin.Context) {
c.HTML(
http.StatusOK,
"index.html",
gin.H{
"title": base.AppName,
"version": base.AppVersion.String(),
"envs": base.Conf.GetEnvsList(),
"replayTypes": kafkian.GetReplayTypesList(),
"headerPlaceholder": `e.g.: [{"key": "Content-Type","value": "application/json"}]`,
"payloadPlaceholder": `e.g.: {"year": 1984, "enemy": "Eastasia"}`,
},
)
}
func getEnv(c *gin.Context) {
envName := c.Param("envname")
config := base.Conf.GetEnvConfig(envName)
c.JSON(http.StatusOK, gin.H{
"name": config.Name,
"headerPrefix": config.HeaderPrefix,
"groups": config.Groups,
"topics": config.Topics,
"schemas": config.Schemas,
})
}
func webSocket(c *gin.Context) {
kafkian.HandleWS(c.Writer, c.Request)
}