diff --git a/.idea/webResources.xml b/.idea/webResources.xml
new file mode 100644
index 0000000..4a3d8bd
--- /dev/null
+++ b/.idea/webResources.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/application/application.go b/application/application.go
index 9a344b8..60d791d 100644
--- a/application/application.go
+++ b/application/application.go
@@ -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,
diff --git a/config/config.go b/config/config.go
index 8e2f7ed..f7f3599 100644
--- a/config/config.go
+++ b/config/config.go
@@ -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"`
}
diff --git a/ports/rest/ui.go b/ports/rest/ui.go
new file mode 100644
index 0000000..e6b33f5
--- /dev/null
+++ b/ports/rest/ui.go
@@ -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/")
+}
diff --git a/ui/embed.go b/ui/embed.go
new file mode 100644
index 0000000..15d3fe2
--- /dev/null
+++ b/ui/embed.go
@@ -0,0 +1,8 @@
+package ui
+
+import (
+ "embed"
+)
+
+//go:embed static/*
+var StaticFiles embed.FS
diff --git a/ui/static/index.html b/ui/static/index.html
new file mode 100644
index 0000000..5017bfb
--- /dev/null
+++ b/ui/static/index.html
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+ Document
+
+
+
+Hello World!
+
+
+
diff --git a/ui/static/style.css b/ui/static/style.css
new file mode 100644
index 0000000..8b7c591
--- /dev/null
+++ b/ui/static/style.css
@@ -0,0 +1,3 @@
+h1 {
+ background-color: azure;
+}