Skip to content

Commit

Permalink
Add route to http server that allows refreshing the device list
Browse files Browse the repository at this point in the history
  • Loading branch information
giodamelio committed Apr 26, 2023
1 parent c99b485 commit 4945593
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
25 changes: 21 additions & 4 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
}
Expand Down
7 changes: 6 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4945593

Please sign in to comment.