Fix rice build

This commit is contained in:
Khanh Ngo 2020-05-21 15:22:08 +07:00
parent d428bfe6f4
commit 8ea0ee3ca5
No known key found for this signature in database
GPG key ID: D5FAA6A16150E49E
3 changed files with 15 additions and 18 deletions

View file

@ -23,4 +23,5 @@ docker-compose*
# App data & bin
db
wireguard-ui
assets
wireguard-ui

12
main.go
View file

@ -2,13 +2,12 @@ package main
import (
"fmt"
rice "github.com/GeertJohan/go.rice"
"github.com/labstack/echo/v4"
"net/http"
"github.com/GeertJohan/go.rice"
"github.com/ngoduykhanh/wireguard-ui/handler"
"github.com/ngoduykhanh/wireguard-ui/router"
"github.com/ngoduykhanh/wireguard-ui/util"
"net/http"
)
func main() {
@ -18,11 +17,14 @@ func main() {
fmt.Print("Cannot init database: ", err)
}
// the file server for rice. "assets" is the folder where the files come from.
// create rice box for embedded template
tmplBox := rice.MustFindBox("templates")
// rice file server for assets. "assets" is the folder where the files come from.
assetHandler := http.FileServer(rice.MustFindBox("assets").HTTPBox())
// register routes
app := router.New()
app := router.New(tmplBox)
app.GET("/", handler.WireGuardClients())
app.GET("/login", handler.LoginPage())

View file

@ -33,38 +33,32 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c
}
// New function
func New() *echo.Echo {
func New(tmplBox *rice.Box) *echo.Echo {
e := echo.New()
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
// create go rice box for embedded template
templateBox, err := rice.FindBox("../templates")
if err != nil {
log.Fatal(err)
}
// read html template file to string
tmplBaseString, err := templateBox.String("base.html")
tmplBaseString, err := tmplBox.String("base.html")
if err != nil {
log.Fatal(err)
}
tmplLoginString, err := templateBox.String("login.html")
tmplLoginString, err := tmplBox.String("login.html")
if err != nil {
log.Fatal(err)
}
tmplClientsString, err := templateBox.String("clients.html")
tmplClientsString, err := tmplBox.String("clients.html")
if err != nil {
log.Fatal(err)
}
tmplServerString, err := templateBox.String("server.html")
tmplServerString, err := tmplBox.String("server.html")
if err != nil {
log.Fatal(err)
}
tmplGlobalSettingsString, err := templateBox.String("global_settings.html")
tmplGlobalSettingsString, err := tmplBox.String("global_settings.html")
if err != nil {
log.Fatal(err)
}