dashboard: Use gin-gonic instead of httprouter directly
This commit is contained in:
parent
8cb7bf8b96
commit
635e67c224
5 changed files with 193 additions and 215 deletions
60
dashboard/app.go
Normal file
60
dashboard/app.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
router *gin.Engine
|
||||
srv *http.Server
|
||||
bind string
|
||||
}
|
||||
|
||||
func NewApp(baseURL string, bind string) App {
|
||||
gin.ForceConsoleColor()
|
||||
router := gin.Default()
|
||||
|
||||
var baserouter *gin.RouterGroup
|
||||
if len(baseURL) > 1 {
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.Redirect(http.StatusFound, baseURL)
|
||||
})
|
||||
|
||||
baserouter = router.Group(baseURL)
|
||||
} else {
|
||||
baserouter = router.Group("")
|
||||
}
|
||||
declareStaticRoutes(baserouter, baseURL)
|
||||
|
||||
app := App{
|
||||
router: router,
|
||||
bind: bind,
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func (app *App) Start() {
|
||||
app.srv = &http.Server{
|
||||
Addr: app.bind,
|
||||
Handler: app.router,
|
||||
}
|
||||
|
||||
log.Printf("Ready, listening on %s\n", app.bind)
|
||||
if err := app.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("listen: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *App) Stop() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := app.srv.Shutdown(ctx); err != nil {
|
||||
log.Fatal("Server Shutdown:", err)
|
||||
}
|
||||
}
|
Reference in a new issue