Skip to content

Commit

Permalink
Basic tsnet http server hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
giodamelio committed Apr 26, 2023
1 parent e2563f7 commit 0fab3ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions server/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package server

import (
"fmt"
"net/http"
"strconv"

"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"tailscale.com/tsnet"
)

func SetupHttpServer(tsServer *tsnet.Server, readDevices chan ReadDevicesOp) {
port := viper.GetInt("http-server.port")

log.
Info().
Int("port", port).
Msgf("Starting http server on port %d", port)

// Create the Tailscale listener
listener, err := tsServer.Listen("tcp", ":"+strconv.Itoa(viper.GetInt("http-server.port")))
if err != nil {
log.Fatal().Err(err).Msg("could not listen on tailnet")
}
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")
}))
if err != nil {
log.Fatal().Err(err).Msg("http server error")
}
}
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func Start() {
// Setup the DNS server
go SetupDnsServer(tsServer, reads)

if viper.GetBool("http-server.enabled") {
// Setup the http server
go SetupHttpServer(tsServer, reads)
}

// Keep track of all the devices
var state = make(DeviceMap)
for {
Expand Down

0 comments on commit 0fab3ce

Please sign in to comment.