master
bel 2023-06-17 08:49:24 -06:00
parent da4219a3ca
commit 6357c83aaa
1 changed files with 37 additions and 4 deletions

View File

@ -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) {
}