mirror of
https://github.com/h44z/wg-portal.git
synced 2025-09-15 07:11:15 +00:00
V2 alpha - initial version (#172)
Initial alpha codebase for version 2 of WireGuard Portal. This version is considered unstable and incomplete (for example, no public REST API)! Use with care! Fixes/Implements the following issues: - OAuth support #154, #1 - New Web UI with internationalisation support #98, #107, #89, #62 - Postgres Support #49 - Improved Email handling #47, #119 - DNS Search Domain support #46 - Bugfixes #94, #48 --------- Co-authored-by: Fabian Wechselberger <wechselbergerf@hotmail.com>
This commit is contained in:
68
internal/app/configfile/template.go
Normal file
68
internal/app/configfile/template.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package configfile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"text/template"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
//go:embed tpl_files/*
|
||||
var TemplateFiles embed.FS
|
||||
|
||||
type TemplateHandler struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
func newTemplateHandler() (*TemplateHandler, error) {
|
||||
tplFuncs := template.FuncMap{
|
||||
"CidrsToString": domain.CidrsToString,
|
||||
}
|
||||
|
||||
templateCache, err := template.New("WireGuard").Funcs(tplFuncs).ParseFS(TemplateFiles, "tpl_files/*.tpl")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
handler := &TemplateHandler{
|
||||
templates: templateCache,
|
||||
}
|
||||
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
func (c TemplateHandler) GetInterfaceConfig(cfg *domain.Interface, peers []domain.Peer) (io.Reader, error) {
|
||||
var tplBuff bytes.Buffer
|
||||
|
||||
err := c.templates.ExecuteTemplate(&tplBuff, "wg_interface.tpl", map[string]interface{}{
|
||||
"Interface": cfg,
|
||||
"Peers": peers,
|
||||
"Portal": map[string]interface{}{
|
||||
"Version": "unknown",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute interface template for %s: %w", cfg.Identifier, err)
|
||||
}
|
||||
|
||||
return &tplBuff, nil
|
||||
}
|
||||
|
||||
func (c TemplateHandler) GetPeerConfig(peer *domain.Peer) (io.Reader, error) {
|
||||
var tplBuff bytes.Buffer
|
||||
|
||||
err := c.templates.ExecuteTemplate(&tplBuff, "wg_peer.tpl", map[string]interface{}{
|
||||
"Peer": peer,
|
||||
"Portal": map[string]interface{}{
|
||||
"Version": "unknown",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute peer template for %s: %w", peer.Identifier, err)
|
||||
}
|
||||
|
||||
return &tplBuff, nil
|
||||
}
|
Reference in New Issue
Block a user