We can now get the serve address

This commit is contained in:
kamkow1
2025-06-21 15:14:21 +02:00
parent 61e218e58f
commit 8ce2789588
2 changed files with 26 additions and 14 deletions

View File

@ -1,9 +1,9 @@
BUILD_MODE ?= DEBUG BUILD_MODE ?= DEBUG
ifeq ($(BUILD_MODE),DEBUG) ifeq ($(BUILD_MODE),DEBUG)
LISTEN_PORT = ":9090" LISTEN_ADDR = "localhost:9090"
else ifeq ($(BUILD_MODE),RELEASE) else ifeq ($(BUILD_MODE),RELEASE)
LISTEN_PORT = ":80" LISTEN_ADDR = "0.0.0.0:80"
else else
$(error Unknown build mode) $(error Unknown build mode)
endif endif
@ -11,7 +11,7 @@ endif
all: lts watcher all: lts watcher
lts: lts.go uuid.go lts: lts.go uuid.go
go build -ldflags="-X main.LISTEN_PORT=$(LISTEN_PORT)" go build -ldflags="-X main.LISTEN_ADDR=$(LISTEN_ADDR)"
watcher: watcher.c watcher: watcher.c
cc -o $@ $< cc -o $@ $<

34
lts.go
View File

@ -8,6 +8,7 @@ import (
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -17,7 +18,9 @@ import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
) )
var LISTEN_PORT string var LISTEN_ADDR string
var listener net.Listener
//go:embed tmpls/* //go:embed tmpls/*
var tmpls embed.FS var tmpls embed.FS
@ -108,7 +111,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("myfile") file, handler, err := r.FormFile("myfile")
if err != nil { if err != nil {
log.Println(err) log.Println("Error getting file from form: ", err)
http.Error(w, "Could not get the file", http.StatusBadRequest) http.Error(w, "Could not get the file", http.StatusBadRequest)
return return
} }
@ -134,7 +137,7 @@ func handleHome(w http.ResponseWriter, r *http.Request) {
home_tmpl, _ := tmpls.ReadFile("tmpls/home.html") home_tmpl, _ := tmpls.ReadFile("tmpls/home.html")
tmpl := template.Must(template.New("home").Parse(string(home_tmpl))) tmpl := template.Must(template.New("home").Parse(string(home_tmpl)))
if err := tmpl.Execute(w, nil); err != nil { if err := tmpl.Execute(w, nil); err != nil {
log.Println(err) log.Println("Error executing template: ", err)
} }
} }
@ -151,7 +154,7 @@ func handleBrowse(w http.ResponseWriter, r *http.Request) {
storeEntries, err := os.ReadDir(storePath) storeEntries, err := os.ReadDir(storePath)
if err != nil { if err != nil {
log.Println(err) log.Printf("Error reading store path %s %v\n", storePath, err)
return return
} }
@ -176,7 +179,7 @@ func handleBrowse(w http.ResponseWriter, r *http.Request) {
} }
if err := tmpl.Execute(w, records); err != nil { if err := tmpl.Execute(w, records); err != nil {
log.Println(err) log.Println("Error executing template: ", err)
} }
} }
@ -184,7 +187,7 @@ func reloadAllowedUsers() {
log.Println("loading ALLOWED_USERS.txt") log.Println("loading ALLOWED_USERS.txt")
allowedUsersTxt, err := ioutil.ReadFile("./ALLOWED_USERS.txt") allowedUsersTxt, err := ioutil.ReadFile("./ALLOWED_USERS.txt")
if err != nil { if err != nil {
log.Println(err) log.Println("Error reading ALLOWED_USERS.txt: ", err)
return return
} }
scanner := bufio.NewScanner(strings.NewReader(string(allowedUsersTxt))) scanner := bufio.NewScanner(strings.NewReader(string(allowedUsersTxt)))
@ -200,7 +203,7 @@ func reloadAllowedUsers() {
func watchAllowedUsers() chan bool { func watchAllowedUsers() chan bool {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error creating new watcher: ", err)
} }
defer watcher.Close() defer watcher.Close()
@ -220,7 +223,7 @@ func watchAllowedUsers() chan bool {
if !ok { if !ok {
return return
} }
log.Println(err) log.Println("Watcher error: ", err)
case <-done: case <-done:
break break
} }
@ -229,7 +232,7 @@ func watchAllowedUsers() chan bool {
err = watcher.Add("./ALLOWED_USERS.txt") err = watcher.Add("./ALLOWED_USERS.txt")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error adding ALLOWED_USERS.txt to watcher: ", err)
} }
return done return done
@ -245,8 +248,17 @@ func main() {
http.Handle("/etc/", http.FileServerFS(etc)) http.Handle("/etc/", http.FileServerFS(etc))
http.Handle("/store/", http.StripPrefix("/store/", http.FileServer(http.Dir(storePath)))) http.Handle("/store/", http.StripPrefix("/store/", http.FileServer(http.Dir(storePath))))
log.Printf("Listening on %s\n", LISTEN_PORT) listener, err := net.Listen("tcp", LISTEN_ADDR)
http.ListenAndServe(LISTEN_PORT, nil) if err != nil {
log.Fatal("Error starting server: ", err)
}
log.Printf("Listening on %s\n", listener.Addr().String())
err = http.Serve(listener, nil)
if err != nil {
log.Fatal("Error in serving: ", err)
}
doneWatching<-true doneWatching<-true
} }