add static override

This commit is contained in:
1e99 2024-12-16 22:31:50 +01:00
parent 71e28f94ca
commit d1d5f93486
3 changed files with 30 additions and 16 deletions

View file

@ -69,6 +69,9 @@ Configuration is done using environment variables.
- `ram`: Passwords are stored in RAM. - `ram`: Passwords are stored in RAM.
- `dir`: Passwords are stored in a directory. The directory is specified using `PASSED_STORE_DIR_PATH`, which defaults to `passwords`. PassED will create the directory for you. - `dir`: Passwords are stored in a directory. The directory is specified using `PASSED_STORE_DIR_PATH`, which defaults to `passwords`. PassED will create the directory for you.
- `PASSED_STORE_CLEAR_INTERVAL`: Time that should pass between clearing expired passwords in seconds, defaults to `30`. - `PASSED_STORE_CLEAR_INTERVAL`: Time that should pass between clearing expired passwords in seconds, defaults to `30`.
- `PASSED_STATIC_TYPE`: Directory from which static files are served, defaults to `embed`.
- `embed`: Files are served from the files located within the binary. This is recommended.
- `dir`: Files are served from a directory. The directory is specified using `PASSED_STATIC_DIR_PATH`, which defaults to `static`. PassED will not create the directory for you.
## Translators ## Translators
- RoryBD - RoryBD

28
main.go
View file

@ -3,6 +3,7 @@ package main
import ( import (
"embed" "embed"
"errors" "errors"
"io/fs"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -22,6 +23,11 @@ func run() error {
return err return err
} }
staticFS, err := newStaticFS()
if err != nil {
return err
}
var address string var address string
var logRequests bool var logRequests bool
var maxPasswordLength int var maxPasswordLength int
@ -32,7 +38,7 @@ func run() error {
mux := http.NewServeMux() mux := http.NewServeMux()
handler := http.Handler(mux) handler := http.Handler(mux)
mux.Handle("GET /", routes.ServeFiles(embedFS, "static")) mux.Handle("GET /", http.FileServerFS(staticFS))
mux.Handle("POST /api/password", routes.CreatePassword(store, maxPasswordLength)) mux.Handle("POST /api/password", routes.CreatePassword(store, maxPasswordLength))
mux.Handle("GET /api/password/{id}", routes.GetPassword(store)) mux.Handle("GET /api/password/{id}", routes.GetPassword(store))
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(store)) mux.Handle("HEAD /api/password/{id}", routes.HasPassword(store))
@ -50,6 +56,26 @@ func run() error {
return nil return nil
} }
func newStaticFS() (sfs fs.FS, err error) {
var fsType string
env("PASSED_STATIC_TYPE", &fsType, "embed")
switch fsType {
case "embed":
sfs, err = fs.Sub(embedFS, "static")
return
case "dir", "directory":
var path string
env("PASSED_STATIC_DIR_PATH", &path, "static")
sfs = os.DirFS(path)
return
default:
err = errors.New("unkown fs type")
return
}
}
func newStore() (store storage.Store, err error) { func newStore() (store storage.Store, err error) {
var storeType string var storeType string
var clearInterval int var clearInterval int

View file

@ -1,15 +0,0 @@
package routes
import (
"io/fs"
"net/http"
)
func ServeFiles(fileSystem fs.FS, pathInFs string) http.Handler {
newFs, err := fs.Sub(fileSystem, pathInFs)
if err != nil {
panic(err)
}
return http.FileServerFS(newFs)
}