rewrite using net/http
tronnet/workflows: Static Analysis -- Golang / static-go (push) Failing after 5s
tronnet/workflows: Static Analysis -- Golang / static-go (push) Failing after 5s
This commit is contained in:
1 parent
db8ed2e830
commit
98790dbe3b
9 files changed
+270
-189
No files matched your search
+25
-28
@@ -4,12 +4,11 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"proxmoxaas-dashboard/app/common"
|
||||
"proxmoxaas-dashboard/app/routes"
|
||||
"strings"
|
||||
"web" // go will complain here until the first build
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tdewolff/minify/v2"
|
||||
)
|
||||
|
||||
@@ -19,44 +18,42 @@ func Run() {
|
||||
|
||||
common.Global = common.GetConfig(*configPath)
|
||||
|
||||
// setup gin
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
router := gin.Default()
|
||||
// setup http router
|
||||
router := http.NewServeMux()
|
||||
|
||||
// setup static resources
|
||||
m := common.InitMinify()
|
||||
ServeStatic(router, m)
|
||||
html := common.MinifyStatic(m, web.Templates)
|
||||
common.TMPL = common.LoadHTMLToGin(router, html)
|
||||
common.TMPL = common.LoadHTML(html)
|
||||
|
||||
// dynamic routes for pages and page fragments
|
||||
router.GET("/", routes.HandleGETIndex)
|
||||
router.GET("/index", routes.HandleGETIndex)
|
||||
router.GET("/index/instances", routes.HandleGETInstancesFragment)
|
||||
router.GET("/account", routes.HandleGETAccount)
|
||||
router.GET("/config", routes.HandleGETConfig)
|
||||
router.GET("/config/volumes", routes.HandleGETConfigVolumesFragment)
|
||||
router.GET("/config/nets", routes.HandleGETConfigNetsFragment)
|
||||
router.GET("/config/devices", routes.HandleGETConfigDevicesFragment)
|
||||
router.GET("/config/boot", routes.HandleGETConfigBootFragment)
|
||||
router.GET("/backups", routes.HandleGETBackups)
|
||||
router.GET("/backups/backups", routes.HandleGETBackupsFragment)
|
||||
router.GET("/login", routes.HandleGETLogin)
|
||||
router.GET("/settings", routes.HandleGETSettings)
|
||||
router.HandleFunc("GET /", routes.HandleGETIndex)
|
||||
router.HandleFunc("GET /index", routes.HandleGETIndex)
|
||||
router.HandleFunc("GET /index/instances", routes.HandleGETInstancesFragment)
|
||||
router.HandleFunc("GET /account", routes.HandleGETAccount)
|
||||
router.HandleFunc("GET /config", routes.HandleGETConfig)
|
||||
router.HandleFunc("GET /config/volumes", routes.HandleGETConfigVolumesFragment)
|
||||
router.HandleFunc("GET /config/nets", routes.HandleGETConfigNetsFragment)
|
||||
router.HandleFunc("GET /config/devices", routes.HandleGETConfigDevicesFragment)
|
||||
router.HandleFunc("GET /config/boot", routes.HandleGETConfigBootFragment)
|
||||
router.HandleFunc("GET /backups", routes.HandleGETBackups)
|
||||
router.HandleFunc("GET /backups/backups", routes.HandleGETBackupsFragment)
|
||||
router.HandleFunc("GET /login", routes.HandleGETLogin)
|
||||
router.HandleFunc("GET /settings", routes.HandleGETSettings)
|
||||
|
||||
// run on all interfaces with port
|
||||
log.Fatal("[ERR ] starting gin router: ", router.Run(fmt.Sprintf("0.0.0.0:%d", common.Global.Port)))
|
||||
log.Fatal("[ERR ] starting gin router: ", http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", common.Global.Port), router))
|
||||
}
|
||||
|
||||
// setup static resources under web (css, images, modules, scripts)
|
||||
func ServeStatic(router *gin.Engine, m *minify.M) {
|
||||
func ServeStatic(router *http.ServeMux, m *minify.M) {
|
||||
static := common.MinifyStatic(m, web.Static)
|
||||
router.GET("/websrc/*path", func(c *gin.Context) {
|
||||
path, _ := c.Params.Get("path")
|
||||
trimed_path := strings.Replace(path, "/", "", 1) // replace leading "/" captured by *path
|
||||
data := static[trimed_path]
|
||||
fmt.Println(path)
|
||||
c.Header("Content-Encoding", data.MimeType.Encoding)
|
||||
c.Data(200, data.MimeType.Type, []byte(data.Data))
|
||||
router.HandleFunc("GET /websrc/{path...}", func(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.PathValue("path")
|
||||
data := static[path]
|
||||
w.Header().Set("Content-Encoding", data.MimeType.Encoding)
|
||||
w.Header().Set("Content-Type", data.MimeType.Type)
|
||||
w.Write([]byte(data.Data))
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user