wipper
parent
da4219a3ca
commit
6357c83aaa
|
|
@ -3,8 +3,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Config struct {
|
var Config struct {
|
||||||
|
|
@ -13,11 +18,17 @@ var Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cleanup := contextWithCleanup(context.Background())
|
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||||
defer cleanup()
|
defer can()
|
||||||
config(ctx)
|
|
||||||
|
|
||||||
|
ctx, cleanup := contextWithCleanup(ctx)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
config(ctx)
|
||||||
log.Printf("%+v", Config)
|
log.Printf("%+v", Config)
|
||||||
|
|
||||||
|
listenAndServe(ctx)
|
||||||
|
log.Println("done")
|
||||||
}
|
}
|
||||||
|
|
||||||
func contextWithCleanup(ctx context.Context) (context.Context, func()) {
|
func contextWithCleanup(ctx context.Context) (context.Context, func()) {
|
||||||
|
|
@ -49,7 +60,29 @@ func config(ctx context.Context) {
|
||||||
}
|
}
|
||||||
contextWithCleanupFunc(ctx, func() { os.RemoveAll(d) })
|
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.StringVar(&Config.SessionD, "d", d, "dir to store sessions")
|
||||||
flag.Parse()
|
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) {
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue