Skip to content

Commit

Permalink
Fix flag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Boernsman committed Sep 24, 2024
1 parent f8b22a5 commit ef9362c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module whereisit

go 1.22.4

require github.com/gorilla/mux v1.8.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
4 changes: 0 additions & 4 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
51 changes: 44 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import (
"strings"
"sync"
"time"
"os"

"github.com/gorilla/mux"
)


const lifetime time.Duration = 24 * time.Hour

var devices struct {
Expand All @@ -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) {
Expand Down
13 changes: 0 additions & 13 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>NUPNP</title>
<link rel="icon" type="image/png" href="/favicon.png">
<link rel="stylesheet" media="screen" href="normalize.min.css">
<link rel="stylesheet" href="font-awesome-4.6.3/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="bulma-0.1.2/css/bulma.css"/>
Expand All @@ -16,18 +15,6 @@
<section class="hero is-primary is-medium is-bold">
<div class="hero-head">
<header class="nav">
<div class="container">
<div class="nav-left">
<a class="nav-item is-brand" href="/">
<img src="logo.png" alt="Logo">
</a>
</div>
<span class="nav-toggle">
<span></span>
<span></span>
</span>
</div>
</div>
</header>
</div>
</section>
Expand Down

0 comments on commit ef9362c

Please sign in to comment.