From 4a2aa14e68b5b6b1b3c09e0b923178c7c98a671c Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Wed, 21 May 2025 17:43:52 -0400 Subject: Add a simple httpd --- INSTALL.sh | 2 +- Makefile | 2 +- bin-src/httpd.go | 35 +++++++++++++++++++++++++++++++++++ systemd/dashboard-httpd.service | 6 ++++++ systemd/dashboard-httpd.socket | 8 ++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 bin-src/httpd.go create mode 100644 systemd/dashboard-httpd.service create mode 100644 systemd/dashboard-httpd.socket diff --git a/INSTALL.sh b/INSTALL.sh index b024596..364d98c 100755 --- a/INSTALL.sh +++ b/INSTALL.sh @@ -10,4 +10,4 @@ sudo pacman -S --needed -- \ mkdir -p -- ~/.config/systemd/user/ ln -srf -- ~/dashboard/systemd/* ~/.config/systemd/user/ -systemctl --user enable --now dashboard-daily.timer +systemctl --user enable --now dashboard-daily.timer dashboard-httpd.socket diff --git a/Makefile b/Makefile index 4214f28..7ee6e91 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ outs = $(patsubst public-src/%,public/%,\ $(srcs)))) all: $(outs) all: public/3rd-party -all: bin/cron-daily +all: bin/cron-daily bin/httpd .PHONY: all NET-%: diff --git a/bin-src/httpd.go b/bin-src/httpd.go new file mode 100644 index 0000000..6b653bd --- /dev/null +++ b/bin-src/httpd.go @@ -0,0 +1,35 @@ +package main + +import ( + "errors" + "fmt" + "net" + "net/http" + "os" + "path/filepath" +) + +func mainWithError() error { + listenerFile := os.NewFile(uintptr(3), "listener") + if listenerFile == nil { + return errors.New("FD3 is not open") + } + + listener, err := net.FileListener(listenerFile) + if err != nil { + return err + } + + if err := os.Chdir(filepath.Dir(filepath.Dir(os.Args[0]))); err != nil { + return err + } + + return http.Serve(listener, http.FileServer(http.Dir("public"))) +} + +func main() { + if err := mainWithError(); err != nil { + fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err) + os.Exit(1) + } +} diff --git a/systemd/dashboard-httpd.service b/systemd/dashboard-httpd.service new file mode 100644 index 0000000..17d45fa --- /dev/null +++ b/systemd/dashboard-httpd.service @@ -0,0 +1,6 @@ +[Unit] +Description=Dashboard HTTP server +Requires=dashboard-httpd.socket + +[Service] +ExecStart=/bin/sh -c '~/dashboard/bin/httpd' diff --git a/systemd/dashboard-httpd.socket b/systemd/dashboard-httpd.socket new file mode 100644 index 0000000..e49aac4 --- /dev/null +++ b/systemd/dashboard-httpd.socket @@ -0,0 +1,8 @@ +[Unit] +Description=Dashboard HTTP server + +[Socket] +ListenStream=41198 + +[Install] +WantedBy=sockets.target -- cgit v1.2.3-2-g168b