alai-server/backend/routes/router.go

66 lines
1.5 KiB
Go
Raw Normal View History

2022-06-29 21:26:05 -04:00
package routes
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
2023-01-31 23:06:45 -03:00
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/middlewares"
2022-06-29 21:26:05 -04:00
"github.com/gorilla/handlers"
"github.com/julienschmidt/httprouter"
)
func Initialize() *httprouter.Router {
router := httprouter.New()
router.GET("/", index)
2023-02-01 13:04:46 -03:00
FrameRoutes(router)
2022-06-29 21:26:05 -04:00
GameRoutes(router)
2023-02-01 13:08:28 -03:00
GodotVersionRoutes(router)
2023-02-01 12:48:23 -03:00
LevelRoutes(router)
2023-02-01 13:18:52 -03:00
ObjectNameRoutes(router)
2023-02-01 13:19:19 -03:00
ObjectStateRoutes(router)
2023-01-28 20:48:56 -03:00
OSRoutes(router)
2023-01-31 09:35:56 -03:00
PlayerRoutes(router)
UserRoutes(router)
2022-06-29 21:26:05 -04:00
return router
}
func Serve(router *httprouter.Router) {
newRouter := handlers.CombinedLoggingHandler(os.Stdout, router)
newRouter = handlers.CompressHandler(newRouter)
2023-01-31 23:06:45 -03:00
newRouter = middlewares.Cors(newRouter)
2022-06-29 21:26:05 -04:00
idleConnsClosed := make(chan struct{})
server := &http.Server{Addr: ":3001", Handler: newRouter}
// Listen for CTRL-C(SIGTERM)
sigterm := make(chan os.Signal)
2023-01-30 09:35:43 -03:00
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
2022-06-29 21:26:05 -04:00
go func() {
<-sigterm
// When CTRL-C is pressed shutdown the server
if err := server.Shutdown(context.Background()); err != nil {
2023-01-30 09:35:43 -03:00
log.Printf("HTTP server Shutdown: %v", err)
}
2022-06-29 21:26:05 -04:00
close(idleConnsClosed)
os.Exit(0)
}()
2023-01-30 09:35:43 -03:00
2022-06-29 21:26:05 -04:00
// 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!")
}