First commit
This commit is contained in:
14
backend/routes/game.go
Normal file
14
backend/routes/game.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"backend/controllers"
|
||||
"backend/middlewares"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func GameRoutes(router *httprouter.Router) {
|
||||
router.POST("/game", controllers.PostGame)
|
||||
router.GET("/games", middlewares.Authenticate(controllers.ListGames))
|
||||
router.GET("/game/:id", middlewares.Authenticate(controllers.GetGame))
|
||||
}
|
56
backend/routes/router.go
Normal file
56
backend/routes/router.go
Normal file
@@ -0,0 +1,56 @@
|
||||
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!")
|
||||
}
|
17
backend/routes/user.go
Normal file
17
backend/routes/user.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"backend/controllers"
|
||||
"backend/middlewares"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func UserRoutes(router *httprouter.Router) {
|
||||
router.POST("/login", controllers.Login)
|
||||
router.GET("/users", middlewares.Authenticate(controllers.ListUsers))
|
||||
router.POST("/user", middlewares.Authenticate(controllers.CreateUser))
|
||||
router.GET("/user/:id", middlewares.Authenticate(controllers.GetUser))
|
||||
router.PATCH("/user/:id", middlewares.Authenticate(controllers.UpdateUser))
|
||||
router.GET("/auth", middlewares.Authenticate(controllers.AuthenticateUser))
|
||||
}
|
Reference in New Issue
Block a user