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
ifeq ($(BUILD_MODE),DEBUG)
LISTEN_PORT = ":9090"
LISTEN_ADDR = "localhost:9090"
else ifeq ($(BUILD_MODE),RELEASE)
LISTEN_PORT = ":80"
LISTEN_ADDR = "0.0.0.0:80"
else
$(error Unknown build mode)
endif
@ -11,7 +11,7 @@ endif
all: lts watcher
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
cc -o $@ $<

34
lts.go
View File

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