mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-07 00:46:58 +03:00
Initial commit
This commit is contained in:
commit
6cb8527c35
12 changed files with 741 additions and 0 deletions
49
router/router.go
Normal file
49
router/router.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"text/template"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/labstack/gommon/log"
|
||||
)
|
||||
|
||||
// TemplateRegistry is a custom html/template renderer for Echo framework
|
||||
type TemplateRegistry struct {
|
||||
templates map[string]*template.Template
|
||||
}
|
||||
|
||||
// Render e.Renderer interface
|
||||
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
tmpl, ok := t.templates[name]
|
||||
if !ok {
|
||||
err := errors.New("Template not found -> " + name)
|
||||
return err
|
||||
}
|
||||
return tmpl.ExecuteTemplate(w, "base.html", data)
|
||||
}
|
||||
|
||||
// New function
|
||||
func New() *echo.Echo {
|
||||
e := echo.New()
|
||||
templates := make(map[string]*template.Template)
|
||||
templates["home.html"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html"))
|
||||
|
||||
e.Logger.SetLevel(log.DEBUG)
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: []string{"*"},
|
||||
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization},
|
||||
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
|
||||
}))
|
||||
e.Validator = NewValidator()
|
||||
e.Static("/static", "assets")
|
||||
e.Renderer = &TemplateRegistry{
|
||||
templates: templates,
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
20
router/validator.go
Normal file
20
router/validator.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package router
|
||||
|
||||
import "gopkg.in/go-playground/validator.v9"
|
||||
|
||||
// NewValidator func
|
||||
func NewValidator() *Validator {
|
||||
return &Validator{
|
||||
validator: validator.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// Validator struct
|
||||
type Validator struct {
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
// Validate func
|
||||
func (v *Validator) Validate(i interface{}) error {
|
||||
return v.validator.Struct(i)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue