mirror of
https://github.com/komodorio/helm-dashboard.git
synced 2026-03-24 11:48:04 +00:00
Start working on web server (#2)
* Start working on web server * Some restructure * Serving static works * Fix linter complaint * Enable local dev * Static files served fine * Tidy
This commit is contained in:
55
pkg/dashboard/api.go
Normal file
55
pkg/dashboard/api.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
//go:embed static/*
|
||||
var staticFS embed.FS
|
||||
|
||||
func newRouter(abortWeb ControlChan, data DataLayer) *gin.Engine {
|
||||
api := gin.Default()
|
||||
fs := http.FS(staticFS)
|
||||
|
||||
// local dev speed-up
|
||||
localDevPath := "pkg/dashboard/static"
|
||||
if _, err := os.Stat(localDevPath); err == nil {
|
||||
log.Warnf("Using local development path to serve static files")
|
||||
|
||||
// the root page
|
||||
api.GET("/", func(c *gin.Context) {
|
||||
c.File(path.Join(localDevPath, "index.html"))
|
||||
})
|
||||
|
||||
// serve a directory called static
|
||||
api.GET("/static/*filepath", func(c *gin.Context) {
|
||||
c.File(path.Join(localDevPath, c.Param("filepath")))
|
||||
})
|
||||
} else {
|
||||
// the root page
|
||||
api.GET("/", func(c *gin.Context) {
|
||||
c.FileFromFS("/static/", fs)
|
||||
})
|
||||
|
||||
// serve a directory called static
|
||||
api.GET("/static/*filepath", func(c *gin.Context) {
|
||||
c.FileFromFS(c.Request.URL.Path, fs)
|
||||
})
|
||||
}
|
||||
|
||||
// server shutdown handler
|
||||
api.DELETE("/", func(c *gin.Context) {
|
||||
abortWeb <- struct{}{}
|
||||
})
|
||||
|
||||
api.GET("/api", func(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, data.ListInstalled())
|
||||
})
|
||||
|
||||
return api
|
||||
}
|
||||
Reference in New Issue
Block a user