Compare commits
No commits in common. "main" and "v0.2.14" have entirely different histories.
6 changed files with 10 additions and 217 deletions
10
README.md
10
README.md
|
@ -1,10 +0,0 @@
|
||||||
# godoc
|
|
||||||
|
|
||||||
Installera verktyget godoc:
|
|
||||||
```
|
|
||||||
go install golang.org/x/tools/cmd/godoc@latest
|
|
||||||
```
|
|
||||||
|
|
||||||
Kör godoc i katalogen för webservice-repot.
|
|
||||||
|
|
||||||
Gå till http://localhost:6060/
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
// Standard
|
// Standard
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -48,10 +49,9 @@ func New(filename string) (config Config, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.Session.DaysValid == 0 {
|
||||||
|
err = errors.New("Configuration: session.daysvalid needs to be higher than 0.")
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) ParseApplicationConfig(v any) {
|
|
||||||
yStr, _ := yaml.Marshal(config.Application)
|
|
||||||
yaml.Unmarshal(yStr, v)
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
package HTMLTemplate
|
|
||||||
|
|
||||||
type Page interface {
|
|
||||||
GetVersion() string
|
|
||||||
GetLayout() string
|
|
||||||
GetPage() string
|
|
||||||
GetData() any
|
|
||||||
}
|
|
||||||
|
|
||||||
type SimplePage struct {
|
|
||||||
Version string
|
|
||||||
Layout string
|
|
||||||
Page string
|
|
||||||
Data any
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SimplePage) GetVersion() string {
|
|
||||||
return s.Version
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SimplePage) GetLayout() string {
|
|
||||||
return s.Layout
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SimplePage) GetPage() string {
|
|
||||||
return s.Page
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SimplePage) GetData() any {
|
|
||||||
return s.Data
|
|
||||||
}
|
|
|
@ -1,158 +0,0 @@
|
||||||
package HTMLTemplate
|
|
||||||
|
|
||||||
import (
|
|
||||||
// External
|
|
||||||
werr "git.gibonuddevalla.se/go/wrappederror"
|
|
||||||
|
|
||||||
// Standard
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Engine struct {
|
|
||||||
parsedTemplates map[string]*template.Template
|
|
||||||
viewFS fs.FS
|
|
||||||
staticEmbeddedFS http.Handler
|
|
||||||
staticLocalFS http.Handler
|
|
||||||
componentFilenames []string
|
|
||||||
DevMode bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEngine(viewFS, staticFS fs.FS, devmode bool) (e Engine, err error) { // {{{
|
|
||||||
e.parsedTemplates = make(map[string]*template.Template)
|
|
||||||
e.viewFS = viewFS
|
|
||||||
e.DevMode = devmode
|
|
||||||
|
|
||||||
e.componentFilenames, err = e.getComponentFilenames()
|
|
||||||
|
|
||||||
// Set up fileservers for static resources.
|
|
||||||
// The embedded FS is using the embedded files intented for production use.
|
|
||||||
// The local FS is for development of Javascript to avoid server rebuild (devmode).
|
|
||||||
var staticSubFS fs.FS
|
|
||||||
staticSubFS, err = fs.Sub(staticFS, "static")
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
e.staticEmbeddedFS = http.FileServer(http.FS(staticSubFS))
|
|
||||||
e.staticLocalFS = http.FileServer(http.Dir("static"))
|
|
||||||
|
|
||||||
return
|
|
||||||
} // }}}
|
|
||||||
|
|
||||||
func (e *Engine) getComponentFilenames() (files []string, err error) { // {{{
|
|
||||||
files = []string{}
|
|
||||||
if err := fs.WalkDir(e.viewFS, "views/components", func(path string, d fs.DirEntry, err error) error {
|
|
||||||
if d == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if d.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
files = append(files, path)
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return files, nil
|
|
||||||
} // }}}
|
|
||||||
|
|
||||||
func (e *Engine) ReloadTemplates() { // {{{
|
|
||||||
e.parsedTemplates = make(map[string]*template.Template)
|
|
||||||
} // }}}
|
|
||||||
|
|
||||||
func (e *Engine) StaticResource(w http.ResponseWriter, r *http.Request) { // {{{
|
|
||||||
var err error
|
|
||||||
|
|
||||||
// URLs with pattern /(css|images)/v1.0.0/foobar are stripped of the version.
|
|
||||||
// To get rid of problems with cached content in browser on a new version release,
|
|
||||||
// while also not disabling cache altogether.
|
|
||||||
if r.URL.Path == "/favicon.ico" {
|
|
||||||
e.staticEmbeddedFS.ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
rxp := regexp.MustCompile("^/(css|images|js|fonts)/v[0-9]+/(.*)$")
|
|
||||||
if comp := rxp.FindStringSubmatch(r.URL.Path); comp != nil {
|
|
||||||
w.Header().Add("Pragma", "public")
|
|
||||||
w.Header().Add("Cache-Control", "max-age=604800")
|
|
||||||
|
|
||||||
r.URL.Path = fmt.Sprintf("/%s/%s", comp[1], comp[2])
|
|
||||||
if e.DevMode {
|
|
||||||
p := fmt.Sprintf("static/%s/%s", comp[1], comp[2])
|
|
||||||
_, err = os.Stat(p)
|
|
||||||
if err == nil {
|
|
||||||
e.staticLocalFS.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
e.staticEmbeddedFS.ServeHTTP(w, r)
|
|
||||||
} // }}}
|
|
||||||
func (e *Engine) getPage(layout, page string) (tmpl *template.Template, err error) { // {{{
|
|
||||||
layoutFilename := fmt.Sprintf("views/layouts/%s.gotmpl", layout)
|
|
||||||
pageFilename := fmt.Sprintf("views/pages/%s.gotmpl", page)
|
|
||||||
|
|
||||||
if tmpl, found := e.parsedTemplates[page]; found {
|
|
||||||
return tmpl, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
funcMap := template.FuncMap{
|
|
||||||
/*
|
|
||||||
"format_time": func(t time.Time) template.HTML {
|
|
||||||
return template.HTML(
|
|
||||||
t.In(smonConfig.Timezone()).Format(`<span class="date">2006-01-02</span> <span class="time">15:04:05<span class="seconds">:05</span></span>`),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
filenames := []string{layoutFilename, pageFilename}
|
|
||||||
filenames = append(filenames, e.componentFilenames...)
|
|
||||||
|
|
||||||
if e.DevMode {
|
|
||||||
tmpl, err = template.New(layout+".gotmpl").Funcs(funcMap).ParseFS(os.DirFS("."), filenames...)
|
|
||||||
} else {
|
|
||||||
tmpl, err = template.New(layout+".gotmpl").Funcs(funcMap).ParseFS(e.viewFS, filenames...)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
err = werr.Wrap(err).Log()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
e.parsedTemplates[page] = tmpl
|
|
||||||
return
|
|
||||||
} // }}}
|
|
||||||
func (e *Engine) Render(p Page, w http.ResponseWriter, r *http.Request) (err error) { // {{{
|
|
||||||
if e.DevMode {
|
|
||||||
e.ReloadTemplates()
|
|
||||||
}
|
|
||||||
|
|
||||||
var tmpl *template.Template
|
|
||||||
tmpl, err = e.getPage(p.GetLayout(), p.GetPage())
|
|
||||||
if err != nil {
|
|
||||||
err = werr.Wrap(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
data := map[string]any{
|
|
||||||
"VERSION": p.GetVersion(),
|
|
||||||
"LAYOUT": p.GetLayout(),
|
|
||||||
"PAGE": p.GetPage(),
|
|
||||||
"ERROR": r.URL.Query().Get("_err"),
|
|
||||||
"Data": p.GetData(),
|
|
||||||
}
|
|
||||||
|
|
||||||
err = tmpl.Execute(w, data)
|
|
||||||
if err != nil {
|
|
||||||
err = werr.Wrap(err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
} // }}}
|
|
||||||
|
|
||||||
// vim: foldmethod=marker
|
|
16
pkg.go
16
pkg.go
|
@ -64,7 +64,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "v0.2.17"
|
const VERSION = "v0.2.14"
|
||||||
|
|
||||||
type HttpHandler func(http.ResponseWriter, *http.Request)
|
type HttpHandler func(http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
|
@ -300,19 +300,11 @@ func (service *Service) StaticHandler(w http.ResponseWriter, r *http.Request, se
|
||||||
|
|
||||||
rxp := regexp.MustCompile("^/(css|images|js|fonts)/v[0-9]+/(.*)$")
|
rxp := regexp.MustCompile("^/(css|images|js|fonts)/v[0-9]+/(.*)$")
|
||||||
if comp := rxp.FindStringSubmatch(r.URL.Path); comp != nil {
|
if comp := rxp.FindStringSubmatch(r.URL.Path); comp != nil {
|
||||||
w.Header().Add("Pragma", "public")
|
|
||||||
w.Header().Add("Cache-Control", "max-age=604800")
|
|
||||||
|
|
||||||
r.URL.Path = fmt.Sprintf("/%s/%s", comp[1], comp[2])
|
r.URL.Path = fmt.Sprintf("/%s/%s", comp[1], comp[2])
|
||||||
p := fmt.Sprintf(service.staticDirectory+"/%s/%s", comp[1], comp[2])
|
p := fmt.Sprintf(service.staticDirectory+"/%s/%s", comp[1], comp[2])
|
||||||
|
_, err = os.Stat(p)
|
||||||
if service.useStaticDirectory {
|
if err == nil {
|
||||||
_, err = os.Stat(p)
|
service.staticLocalFileserver.ServeHTTP(w, r)
|
||||||
if err == nil {
|
|
||||||
service.staticLocalFileserver.ServeHTTP(w, r)
|
|
||||||
} else {
|
|
||||||
service.staticEmbeddedFileserver.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
service.staticEmbeddedFileserver.ServeHTTP(w, r)
|
service.staticEmbeddedFileserver.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,7 +117,7 @@ func (cm *ConnectionManager) ReadLoop(wsConn *WsConnection) { // {{{
|
||||||
cm.logger.Debug("websocket", "op", "read", "data", data)
|
cm.logger.Debug("websocket", "op", "read", "data", data)
|
||||||
|
|
||||||
for _, handler := range cm.readHandlers {
|
for _, handler := range cm.readHandlers {
|
||||||
go handler(cm, wsConn, data)
|
handler(cm, wsConn, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
|
|
Loading…
Add table
Reference in a new issue