Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 25, 2022 11:48
Show Gist options
  • Save percybolmer/aa8705ea80ec104bb75eaae0dfc31879 to your computer and use it in GitHub Desktop.
Save percybolmer/aa8705ea80ec104bb75eaae0dfc31879 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"net/http"
)
func main() {
// Create a root ctx and a CancelFunc which can be used to cancel retentionMap goroutine
rootCtx := context.Background()
ctx, cancel := context.WithCancel(rootCtx)
defer cancel()
setupAPI(ctx)
// Serve on port :8080, fudge yeah hardcoded port
log.Fatal(http.ListenAndServe(":8080", nil))
}
// setupAPI will start all Routes and their Handlers
func setupAPI(ctx context.Context) {
// Create a Manager instance used to handle WebSocket Connections
manager := NewManager(ctx)
// Serve the ./frontend directory at Route /
http.Handle("/", http.FileServer(http.Dir("./frontend")))
http.HandleFunc("/login", manager.loginHandler)
http.HandleFunc("/ws", manager.serveWS)
http.HandleFunc("/debug", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, len(manager.clients))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment