diff --git a/server/http.go b/server/http.go index 2c9d7fb..f73582b 100644 --- a/server/http.go +++ b/server/http.go @@ -5,12 +5,18 @@ import ( "net/http" "strconv" + "github.com/giodamelio/tailscale-custom-domain-dns/tsapi" "github.com/rs/zerolog/log" "github.com/spf13/viper" "tailscale.com/tsnet" ) -func SetupHttpServer(tsServer *tsnet.Server, readDevices chan ReadDevicesOp) { +func SetupHttpServer( + tsServer *tsnet.Server, + ts *tsapi.TSApi, + readDevices chan ReadDevicesOp, + writeDevices chan WriteDevicesOp, +) { port := viper.GetInt("http-server.port") log. @@ -25,10 +31,21 @@ func SetupHttpServer(tsServer *tsnet.Server, readDevices chan ReadDevicesOp) { } defer listener.Close() - // Start an http server on the port - err = http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello World") + // Setup our router + router := http.NewServeMux() + router.Handle( + "/api/devices/refresh", + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + updateDevices(writeDevices, ts) + fmt.Fprintf(w, "Devices refreshed") + }), + ) + router.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.NotFound(w, r) })) + + // Start an http server on the port + err = http.Serve(listener, router) if err != nil { log.Fatal().Err(err).Msg("http server error") } diff --git a/server/server.go b/server/server.go index 57cf965..632e859 100644 --- a/server/server.go +++ b/server/server.go @@ -51,7 +51,12 @@ func Start() { if viper.GetBool("http-server.enabled") { // Setup the http server - go SetupHttpServer(tsServer, reads) + go SetupHttpServer( + tsServer, + ts, + reads, + writes, + ) } // Keep track of all the devices