// GET Example HandlerfuncindexHandler(c router.Context) {
c.Write("This is index Page")
}
// POST Example HandlerfuncregisterHandler(c router.Context) {
c.Write("This is Register Page")
// WIll post JSONc.JSON(structData)
}
// Middleware Example 1funcgMiddleware1(c router.Context) {
fmt.Println(c.URL.Path)
fmt.Println("This is Global Middleware1")
// New Request Contextc.NewContext("key", "value")
// Create a cookie session if cookie with name "key" does not existif_, err:=c.GetSession("key"); err!=nil {
c.NewSession("key")
}
}
// Middleware Example 2funcgMiddleware2(c router.Context) {
// Get a Request ContextctxVal:=c.GetContext("key")
fmt.Println(ctxVal)
// Get a cookie Sessioncookie, err:=c.GetSession("ket")
iferr!=nil {
// handle error
}
fmt.Println(cookie.Value) // print cookie value// Use c.DeleteSession to delete a cookiec.DeleteSession("key")
}
// Create New multiplexer / routerr:=router.New()
// You can add as many middleware as you like,// they will load in the order they are added.// Handle Global Middleware for every requestsr.Use(gMiddleware1, gMiddleware2)
// Handle Group Middleware for the specific requestadmin:=r.Group("/admin", aMiddleware1, aMiddleware2)
//You can also add Middleware by using the Use method to a Groupadmin.Use(aMiddleware1)
// GET Request With Admin Middleware (localhost:8000/admin/index)admin.GET("/index", adminHandler)
// GET requests (localhost:8000/)r.GET("/", indexHandler)
// POST requests (localhost:8000/register | /login | /logout)r.POST("/register", registerHandler)
r.POST("/login", loginHandler)
r.POST("/logout". logoutHandler)
// Serve Static Files such as css, js (with Gzip and cache)r.ServeFiles("urlPath", "dirPath", "prefix")
// Serve Faviconr.ServeFavicon("Relative/Path/To/Favicon")
// ListenAndServer.Listen(":8000")