func main() { if !cfg.Debug { gin.SetMode(gin.ReleaseMode) } r := gin.New() // Global middleware r.Use(gin.Logger()) r.Use(gin.Recovery()) r.HTMLRender = pongo2gin.Default() r.Static("/static", "./static") r.GET("/", func(c *gin.Context) { c.HTML(200, "index.html", pongo2.Context{"name": "world"}) }) wsServer := NewWsServer() r.GET("/ws/", func(c *gin.Context) { channel := wsServer.GetChannel(BROADCAST_CHANNEL) channel.ServeHTTP(c.Writer, c.Request) }) r.GET("/ws/:channel", func(c *gin.Context) { name := c.Param("channel") channel := wsServer.GetChannel(name) channel.ServeHTTP(c.Writer, c.Request) }) if !cfg.Debug { srv := &graceful.Server{ Timeout: 10 * time.Second, Server: &http.Server{ Addr: cfg.Listen, Handler: r, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, }, } srv.ListenAndServe() } else { srv := http.Server{ Addr: cfg.Listen, Handler: r, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } srv.ListenAndServe() } }
// runWeb is an starts the GIN application func runWeb(ctx *cli.Context) { r := gin.Default() r.HTMLRender = pongo2gin.Default() // Use Pongo2 for templates setupMiddleware(r) setupRoutes(r) // Initialise nosurf for csrf token support. csrfHandler := nosurf.New(r) csrfHandler.SetFailureHandler(http.HandlerFunc(csrfFailed)) csrfHandler.ExemptRegexp("/api/(.*)") // ignore API urls for the time being // Start the Gin application with nosurf (for csrf protection). // This is an alternative way to start up the Gin application. http.ListenAndServe(":"+config.Config.Port, csrfHandler) }