test
This commit is contained in:
83
main.go
83
main.go
@@ -4,6 +4,12 @@ import (
|
||||
"ccsecrets/controllers"
|
||||
"ccsecrets/middlewares"
|
||||
"ccsecrets/models"
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -13,19 +19,82 @@ func main() {
|
||||
// Initiate connection to sqlite and make sure our schema is up to date
|
||||
models.ConnectDatabase()
|
||||
|
||||
r := gin.Default()
|
||||
// Create context that listens for the interrupt signal from the OS.
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
public := r.Group("/api")
|
||||
router := gin.Default()
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
time.Sleep(10 * time.Second)
|
||||
c.String(http.StatusOK, "Welcome Gin Server")
|
||||
})
|
||||
|
||||
// Define our routes underneath /api
|
||||
srv := &http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: router,
|
||||
}
|
||||
|
||||
// Register our routes
|
||||
public := router.Group("/api")
|
||||
public.POST("/register", controllers.Register)
|
||||
public.POST("/login", controllers.Login)
|
||||
|
||||
protected := r.Group("/api/admin")
|
||||
protected.Use(middlewares.JwtAuthMiddleware())
|
||||
protected.GET("/user", controllers.CurrentUser)
|
||||
// This is just PoC really, we can get rid of it
|
||||
//protected := r.Group("/api/admin")
|
||||
//protected.Use(middlewares.JwtAuthMiddleware())
|
||||
//protected.GET("/user", controllers.CurrentUser)
|
||||
|
||||
r.Run(":8080")
|
||||
// Get secrets
|
||||
protected := router.Group("/api/secret")
|
||||
protected.Use(middlewares.JwtAuthMiddleware())
|
||||
protected.GET("/device", controllers.CurrentUser)
|
||||
|
||||
// Initializing the server in a goroutine so that
|
||||
// it won't block the graceful shutdown handling below
|
||||
go func() {
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("listen: %s\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Listen for the interrupt signal.
|
||||
<-ctx.Done()
|
||||
|
||||
// Restore default behavior on the interrupt signal and notify user of shutdown.
|
||||
stop()
|
||||
log.Println("shutting down gracefully, press Ctrl+C again to force")
|
||||
|
||||
models.DisconnectDatabase()
|
||||
|
||||
// The context is used to inform the server it has 5 seconds to finish
|
||||
// the request it is currently handling
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Fatal("Server forced to shutdown: ", err)
|
||||
}
|
||||
|
||||
log.Println("Server exiting")
|
||||
|
||||
/*
|
||||
r := gin.Default()
|
||||
|
||||
// Define our routes underneath /api
|
||||
public := r.Group("/api")
|
||||
public.POST("/register", controllers.Register)
|
||||
public.POST("/login", controllers.Login)
|
||||
|
||||
// This is just PoC really, we can get rid of it
|
||||
//protected := r.Group("/api/admin")
|
||||
//protected.Use(middlewares.JwtAuthMiddleware())
|
||||
//protected.GET("/user", controllers.CurrentUser)
|
||||
|
||||
// Get secrets
|
||||
protected := r.Group("/api/secret")
|
||||
protected.Use(middlewares.JwtAuthMiddleware())
|
||||
protected.GET("/device", controllers.CurrentUser)
|
||||
|
||||
r.Run(":8443")
|
||||
*/
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user