Created
December 7, 2023 13:41
-
-
Save jnsgruk/f886468c45a1605760e0d5486241738b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//go:generate bash -c "hugo -s ./webui --minify" | |
import ( | |
"embed" | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"tailscale.com/tsnet" | |
) | |
var ( | |
//go:embed webui/public | |
site embed.FS | |
addr = flag.String("addr", ":443", "address to listen on") | |
hostname = flag.String("hostname", "drinks", "hostname to use on the tailnet") | |
) | |
func routeHandler(w http.ResponseWriter, r *http.Request) { | |
path := fmt.Sprintf("webui/public%s", r.URL.Path) | |
if path == "webui/public/" { | |
path = "webui/public/index.html" | |
} | |
data, err := site.ReadFile(path) | |
if err != nil { | |
w.WriteHeader(http.StatusNotFound) | |
fmt.Fprintf(w, "File not found: %s", path) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
w.Write(data) | |
} | |
func main() { | |
flag.Parse() | |
s := new(tsnet.Server) | |
s.Hostname = *hostname | |
defer s.Close() | |
ln, err := s.ListenTLS("tcp", *addr) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer ln.Close() | |
err = http.Serve(ln, http.HandlerFunc(routeHandler)) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment