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 e4bd457
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 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=
42 changes: 36 additions & 6 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,44 @@ 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(middleware.KeyAuth)

Check failure on line 51 in main.go

View workflow job for this annotation

GitHub Actions / build

undefined: middleware
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("Using public folder", publicFolder)



srv := &http.Server{

Check failure on line 64 in main.go

View workflow job for this annotation

GitHub Actions / build

declared and not used: srv
Handler: r,
Addr: "127.0.0.1:" + *httpPort,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
}

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

0 comments on commit e4bd457

Please sign in to comment.