diff options
| author | Leonardo Bishop <me@leonardobishop.net> | 2025-07-16 18:42:50 +0100 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.net> | 2025-07-16 18:42:50 +0100 |
| commit | 3af8911d46913b8e47c2bf18536a1ec4b7c21596 (patch) | |
| tree | 51944840b5d12f2088ef9d0f982557e72d19205a | |
| parent | 4c3e5c256930249798768b0195c42efca1428586 (diff) | |
Add systemd service files and fixes
| -rw-r--r-- | .gitignore | 7 | ||||
| -rw-r--r-- | Makefile | 19 | ||||
| -rw-r--r-- | dist/config.toml | 13 | ||||
| -rw-r--r-- | dist/scrapbook.service | 11 | ||||
| -rw-r--r-- | main.go | 26 | ||||
| -rw-r--r-- | web/command/html/site.go | 10 |
6 files changed, 64 insertions, 22 deletions
@@ -1,3 +1,6 @@ -config.toml +/config.toml scrapbook -out +runlocal +*.tgz +description-pak +*.deb @@ -1,6 +1,7 @@ BINARY_NAME=scrapbook SYS_CONF_DIR=/etc/scrapbook -SYS_CONF_DIR=/var/lib/scrapbook +SYS_DATA_DIR=/var/lib/scrapbook +SYSTEMD_DIR=/etc/systemd/system all: build @@ -9,9 +10,19 @@ build: runlocal: PWD=$(shell pwd) - mkdir -p out - go build -ldflags "-X 'github.com/LMBishop/scrapbook/pkg/constants.SysConfDir=${PWD}/out/config' -X 'github.com/LMBishop/scrapbook/pkg/constants.SysDataDir=${PWD}/out/data'" -o out/${BINARY_NAME} main.go - cd out; ./${BINARY_NAME} + mkdir -p runlocal + go build -ldflags "-X 'github.com/LMBishop/scrapbook/pkg/constants.SysConfDir=${PWD}/runlocal/config' -X 'github.com/LMBishop/scrapbook/pkg/constants.SysDataDir=${PWD}/runlocal/data'" -o runlocal/${BINARY_NAME} main.go + cd runlocal; ./${BINARY_NAME} + +install: build + install -Dm755 ${BINARY_NAME} /usr/local/bin/${BINARY_NAME} + +installconf: + install -Dm755 dist/scrapbook.service ${SYSTEMD_DIR}/ + install -Dm755 dist/config.toml ${SYS_CONF_DIR}/config.toml + if ! getent passwd scrapbook > /dev/null; then \ + useradd --system --create-home --shell /usr/sbin/nologin --home-dir ${SYS_DATA_DIR} scrapbook ;\ + fi clean: go clean diff --git a/dist/config.toml b/dist/config.toml new file mode 100644 index 0000000..a557edc --- /dev/null +++ b/dist/config.toml @@ -0,0 +1,13 @@ +[Listen] +Address = '0.0.0.0' +Port = 80 + +[Command] +Host = '' +Secret = '' + +[[API]] +Enable = true + +[[Web]] +Enable = true diff --git a/dist/scrapbook.service b/dist/scrapbook.service new file mode 100644 index 0000000..c12ed45 --- /dev/null +++ b/dist/scrapbook.service @@ -0,0 +1,11 @@ +[Unit] +Description=scrapbook server + +[Service] +User=scrapbook +Group=scrapbook +Restart=always +ExecStart=/usr/local/bin/scrapbook + +[Install] +WantedBy=multi-user.target @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "net/http" + "os" "path" "path/filepath" @@ -32,21 +33,16 @@ func main() { siteIndex := index.NewSiteIndex() - err = index.ScanDirectory(path.Join(constants.SysDataDir, "sites"), siteIndex) + sitesDirectory := path.Join(constants.SysDataDir, "sites") + os.MkdirAll(sitesDirectory, 0o755) + + err = index.ScanDirectory(sitesDirectory, siteIndex) if err != nil { panic(fmt.Errorf("could not scan data directory: %w", err)) } slog.Info("initial data directory scan complete", "sites", len(siteIndex.GetSites())) - if cfg.Command.Host == "" { - slog.Warn("command interface host is empty - neither api or web interface will be accessible") - } - - if cfg.Command.Secret == "" { - slog.Warn("command interface secret is empty - neither api or web interface will be accessible") - } - secretKey := make([]byte, 32) rand.Read(secretKey) @@ -54,10 +50,18 @@ func main() { mux := http.NewServeMux() - mux.Handle(fmt.Sprintf("%s/api/", cfg.Command.Host), http.StripPrefix("/api/", api.NewMux(&cfg, siteIndex))) - mux.Handle(fmt.Sprintf("%s/", cfg.Command.Host), web.NewMux(&cfg, siteIndex, authenticator)) + if cfg.Command.Host == "" { + slog.Warn("command interface host is empty - neither api or web interface will be accessible") + } else { + mux.Handle(fmt.Sprintf("%s/api/", cfg.Command.Host), http.StripPrefix("/api/", api.NewMux(&cfg, siteIndex))) + mux.Handle(fmt.Sprintf("%s/", cfg.Command.Host), web.NewMux(&cfg, siteIndex, authenticator)) + } mux.HandleFunc("/", server.ServeSite(siteIndex)) + if cfg.Command.Secret == "" { + slog.Warn("command interface secret is empty - neither api or web interface will be accessible") + } + err = http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port), mux) slog.Error("http server closing", "reason", err.Error()) } diff --git a/web/command/html/site.go b/web/command/html/site.go index b3e0cc1..cb1fa31 100644 --- a/web/command/html/site.go +++ b/web/command/html/site.go @@ -63,12 +63,12 @@ func SitePage(mainConfig *config.MainConfig, site *site.Site) Node { } }), ), - Div( - Class("control-group group-right"), - - navButton("Upload new version", "upload"), - ), }), + Div( + Class("control-group group-right"), + + navButton("Upload new version", "upload"), + ), H2(Text("Information")), |
