web ui: basic logic

This commit is contained in:
2023-04-04 16:20:47 +03:00
parent 790eece361
commit 2a8b94136f
7 changed files with 101 additions and 1 deletions

14
.idea/webResources.xml generated Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="WebResourcesPaths">
<contentEntries>
<entry url="file://$PROJECT_DIR$">
<entryData>
<resourceRoots>
<path value="file://$PROJECT_DIR$/ui" />
</resourceRoots>
</entryData>
</entry>
</contentEntries>
</component>
</project>

View File

@@ -73,9 +73,25 @@ func NewApplication(cfg config.Config) (Application, error) {
return Application{}, fmt.Errorf("new rest server: %w", err)
}
var httpHandler http.Handler = server
if cfg.UI.Enabled {
ui := rest.NewUI(cfg.UI)
httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ui.IsUIRequest(r) {
ui.ServeHTTP(w, r)
return
}
server.ServeHTTP(w, r)
})
}
httpServer := http.Server{
Addr: cfg.API.Address,
Handler: server,
Handler: httpHandler,
ReadTimeout: time.Second * 15,
ReadHeaderTimeout: time.Second * 5,
IdleTimeout: time.Second * 30,

View File

@@ -28,6 +28,7 @@ type Config struct {
DB DB `env:",prefix=DB_"`
Logging Logging `env:",prefix=LOGGING_"`
API API `env:",prefix=API_"`
UI UI `env:",prefix=UI_"`
PDF PDF `env:",prefix=PDF_"`
}
@@ -42,9 +43,15 @@ type PDF struct {
}
type API struct {
Prefix string `env:"PREFIX,default=/"`
Address string `env:"ADDRESS,default=0.0.0.0:5001"`
}
type UI struct {
Enabled bool `env:"ENABLED,default=true"`
Prefix string `env:"PREFIX,default=/"`
}
type DB struct {
Path string `env:"PATH,default=./db"`
}

38
ports/rest/ui.go Normal file
View File

@@ -0,0 +1,38 @@
package rest
import (
"io/fs"
"net/http"
"strings"
"github.com/derfenix/webarchive/config"
"github.com/derfenix/webarchive/ui"
)
func NewUI(cfg config.UI) *UI {
return &UI{prefix: cfg.Prefix}
}
type UI struct {
prefix string
}
func (u *UI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
serveRoot, err := fs.Sub(ui.StaticFiles, "static")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if strings.HasPrefix(r.URL.Path, u.prefix) {
r.URL.Path = "/" + strings.TrimPrefix(r.URL.Path, u.prefix)
}
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/static")
http.FileServer(http.FS(serveRoot)).ServeHTTP(w, r)
}
func (u *UI) IsUIRequest(r *http.Request) bool {
return r.URL.Path == u.prefix || strings.HasPrefix(r.URL.Path, "/static/")
}

8
ui/embed.go Normal file
View File

@@ -0,0 +1,8 @@
package ui
import (
"embed"
)
//go:embed static/*
var StaticFiles embed.FS

14
ui/static/index.html Normal file
View File

@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

3
ui/static/style.css Normal file
View File

@@ -0,0 +1,3 @@
h1 {
background-color: azure;
}