mirror of
https://hacklab.nilfm.cc/felt
synced 2024-10-22 14:31:48 +00:00
61 lines
950 B
Go
61 lines
950 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"nilfm.cc/git/felt/mongodb"
|
|
"nilfm.cc/git/felt/gametable"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
err := run()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
l, err := net.Listen("tcp", os.Args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dbEngine := &mongodb.DbEngine{}
|
|
err = dbEngine.Init(os.Args[2])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
|
|
|
|
gt := gametable.New(dbEngine)
|
|
s := &http.Server{
|
|
Handler: gt,
|
|
ReadTimeout: time.Second * 10,
|
|
WriteTimeout: time.Second * 10,
|
|
}
|
|
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
errc <- s.Serve(l)
|
|
}()
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
signal.Notify(sigs, os.Interrupt)
|
|
select {
|
|
case err := <-errc:
|
|
log.Printf("failed to serve: %v", err)
|
|
case sig := <-sigs:
|
|
log.Printf("terminating: %v", sig)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
defer cancel()
|
|
|
|
return s.Shutdown(ctx)
|
|
}
|