diff --git a/vicuna-tools.d/main.go b/vicuna-tools.d/main.go index 0c0b340..f26c2bf 100644 --- a/vicuna-tools.d/main.go +++ b/vicuna-tools.d/main.go @@ -3,8 +3,13 @@ package main import ( "context" "flag" + "fmt" "log" + "net/http" "os" + "os/signal" + "sync" + "syscall" ) var Config struct { @@ -13,11 +18,17 @@ var Config struct { } func main() { - ctx, cleanup := contextWithCleanup(context.Background()) - defer cleanup() - config(ctx) + ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT) + defer can() + ctx, cleanup := contextWithCleanup(ctx) + defer cleanup() + + config(ctx) log.Printf("%+v", Config) + + listenAndServe(ctx) + log.Println("done") } func contextWithCleanup(ctx context.Context) (context.Context, func()) { @@ -49,7 +60,29 @@ func config(ctx context.Context) { } contextWithCleanupFunc(ctx, func() { os.RemoveAll(d) }) - flag.IntVar(&Config.Port, "p", 8080, "port to listen on") + flag.IntVar(&Config.Port, "p", 37070, "port to listen on") flag.StringVar(&Config.SessionD, "d", d, "dir to store sessions") flag.Parse() } + +func listenAndServe(ctx context.Context) { + wg := &sync.WaitGroup{} + s := &http.Server{ + Addr: fmt.Sprintf(":%d", Config.Port), + Handler: http.HandlerFunc(handle), + } + wg.Add(1) + go func() { + defer wg.Done() + if err := s.ListenAndServe(); err != nil && ctx.Err() == nil { + panic(err) + } + }() + + <-ctx.Done() + s.Close() + wg.Wait() +} + +func handle(w http.ResponseWriter, r *http.Request) { +}