From ef9362c2940349f06b4235258cc6b5cd57e29fcd Mon Sep 17 00:00:00 2001 From: Boernsman <5207214+Boernsman@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:01:48 +0200 Subject: [PATCH] Fix flag parsing --- go.mod | 2 ++ go.sum | 2 ++ install.sh | 4 ---- main.go | 51 ++++++++++++++++++++++++++++++++++++++++------- public/index.html | 13 ------------ 5 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index b742a96..a4c45a0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module whereisit go 1.22.4 + +require github.com/gorilla/mux v1.8.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/install.sh b/install.sh index fabe44f..0231fa3 100755 --- a/install.sh +++ b/install.sh @@ -15,10 +15,6 @@ if [[ $EUID -ne 0 ]]; then exit 1 fi -# Build the Go binary (Optional step, assuming Go is installed) -echo "Building the Go binary..." -go build -o "${WORKING_DIR}/${BINARY_NAME}" - # Copy the binary to /usr/local/bin/ echo "Installing ${BINARY_NAME} binary to ${BINARY_INSTALL_PATH}" cp "${WORKING_DIR}/${BINARY_NAME}" "${BINARY_INSTALL_PATH}" diff --git a/main.go b/main.go index 13005b0..47112f2 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,12 @@ import ( "strings" "sync" "time" + "os" + + "github.com/gorilla/mux" ) + const lifetime time.Duration = 24 * time.Hour var devices struct { @@ -32,18 +36,51 @@ func main() { publicFolder := flag.String("public", "./public/", "Folder with the public files") httpPort := flag.String("http-port", "8180", "Port for the HTTP server") + // Parse the command-line flags + flag.Parse() + + // Check if the pubic folder exists + if _, err := os.Stat(*publicFolder); os.IsNotExist(err) { + log.Fatalf("Publich folder does not exist") + } + devices.d = make([]Device, 0) - http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {}) - http.HandleFunc("/api/register", RegisterDevice) - http.HandleFunc("/api/devices", ListDevices) - http.Handle("/", http.FileServer(http.Dir(*publicFolder))) + r := mux.NewRouter() + apiRouter := r.PathPrefix("/api").Subrouter() + apiRouter.Use(logRequest) + apiRouter.Use(keyAuth) + apiRouter.HandleFunc("/register", RegisterDevice) + apiRouter.HandleFunc("/devices", ListDevices) + + http.Handle("/", http.FileServer(http.Dir(*publicFolder))) go cleanup() - fmt.Println("Listen on port", httpPort) - // Note: use TLS - log.Fatal(http.ListenAndServe(":" + *httpPort, nil)) + fmt.Println("Listen on port", *httpPort) + fmt.Println("Using public folder", *publicFolder) + + srv := &http.Server{ + Handler: r, + Addr: "0.0.0.0:" + *httpPort, + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + log.Fatal(srv.ListenAndServe()) +} + +func logRequest(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Println("logRequest") + next.ServeHTTP(w, r) + }) +} + +func keyAuth(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Println("keyAuth") + next.ServeHTTP(w, r) + }) } func findDevice(ia string, ea string) (int, bool) { diff --git a/public/index.html b/public/index.html index 3a86807..a1b0c0b 100644 --- a/public/index.html +++ b/public/index.html @@ -5,7 +5,6 @@