57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package routes
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/gorilla/handlers"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
func Initialize() *httprouter.Router {
|
|
router := httprouter.New()
|
|
router.GET("/", index)
|
|
UserRoutes(router)
|
|
GameRoutes(router)
|
|
return router
|
|
}
|
|
|
|
func Serve(router *httprouter.Router) {
|
|
newRouter := handlers.CombinedLoggingHandler(os.Stdout, router)
|
|
newRouter = handlers.CompressHandler(newRouter)
|
|
|
|
idleConnsClosed := make(chan struct{})
|
|
|
|
server := &http.Server{Addr: ":3001", Handler: newRouter}
|
|
|
|
// Listen for CTRL-C(SIGTERM)
|
|
sigterm := make(chan os.Signal)
|
|
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
<-sigterm
|
|
// When CTRL-C is pressed shutdown the server
|
|
if err := server.Shutdown(context.Background()); err != nil {
|
|
log.Printf("HTTP server Shutdown: %v", err)
|
|
}
|
|
close(idleConnsClosed)
|
|
os.Exit(0)
|
|
}()
|
|
|
|
// Run the server
|
|
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
|
log.Fatalf("HTTP server ListenAndServe: %v", err)
|
|
}
|
|
|
|
<-idleConnsClosed
|
|
}
|
|
|
|
func index(writer http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
|
fmt.Fprintf(writer, "This is the Alai API server!")
|
|
}
|