This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func NewServer() *gin.Engine { | |
RegisterSerializers() | |
// gin.SetMode(gin.ReleaseMode) // Production mode | |
srv := gin.New() | |
srv.Use(RegisterLogger(Logger)) | |
srv.Use(RegisterRecovery()) | |
srv.Use(ResisterDatabase()) | |
srv.Use(RegisterSession()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func RegisterLogger(out *os.File) gin.HandlerFunc { | |
return func(c *gin.Context) { | |
// Start timer | |
start := time.Now() | |
path := c.Request.URL.Path | |
raw := c.Request.URL.RawQuery | |
// Process request | |
c.Next() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func NoRouteFn(c *gin.Context) { | |
c.HTML(http.StatusOK, "index", gin.H{ | |
"title": "Hi! GoBlog.", | |
"error": "You are requesting something that does not exist.", | |
}) | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func RegisterRecovery() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
defer func() { | |
if err := recover(); err != nil { | |
httprequest, _ := httputil.DumpRequest(c.Request, false) | |
pp.Println(err) | |
m := gomail.NewMessage() | |
m.SetHeader("From", "goblog@cibernomadas.es") | |
m.SetHeader("To", "admin@cibernomadas.es") | |
m.SetHeader("Subject", "GoBlog Error") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func RegisterRecovery() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
defer func() { | |
if err := recover(); err != nil { | |
httprequest, _ := httputil.DumpRequest(c.Request, false) | |
m := gomail.NewMessage() | |
m.SetHeader("From", "goblog@cibernomadas.es") | |
m.SetHeader("To", "admin@cibernomadas.es") | |
m.SetHeader("Subject", "GoBlog Error") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
POST /edit_profile HTTP/1.1 | |
Host: localhost:8000 | |
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 | |
Accept-Encoding: gzip, deflate | |
Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3 | |
Cache-Control: no-cache | |
Connection: keep-alive | |
Content-Length: 24 | |
Content-Type: application/x-www-form-urlencoded | |
Cookie: sess=MTUzMDU2MzYxMHw5N2FVbUw2X1Rzb1Y2Z29CS2tVV29CQUxER2x4THJXWGFPZm5yaUMyd1FiOTlLX0dIUnA0X1pPYTlmMjREZWU5NU9Pb2ZEQTBEODVRUjl5WlhJaHJVb3Bsc01SN2UxQ3hsTDBKWjA5UFRUNUpRTGx5ekxCcW1xQlFMWW81MHFQcjEzT0NDNEg2djJHdXNYWEdxQTZQanN6QnI3X0hNRk01SURCTWdYZDdtMVpacHpQNUFlYVJteGFaalR1YTlYWndrdWVwMGJoUGZpUVJESGJ2dnp5UDNKUzhtM3JYb051MWVTOUdUUm1oTzBnb2ZPazVTbk5vOVl4VEJDelAwRmgzLW10cXV5XzJqY3lXUE9abl84OXE0SDI1eFJoMWppSUVMTjVPdHFqQ3dfMUpHX1AwTzVOUUlHbk9QVm1wRG0zT0YwckR6NWR1VlRiSnB4cGpWc3lsSjNHU09TMktvWVB1aEQzYmhJaWFERFJWVlppN29GY2o0ajBKZUx5ME95cjE4R2RLUmdiazZlbURCem54VC1rYU1Hb3V6MmZ4YlZoWFV3LWNqb1RiU1d6NHRRTEROUjZVMzA3eC1UTXFlMW1hcEFza24wYmFBYmtQZjNiM0I1QXpCWjU2OFNsNXRhR2I5eWY0VmxuSGRZV0F3bHZtYVUxdU90WmdaOFdB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func RegisterRecovery() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
defer func() { | |
if err := recover(); err != nil { | |
// Do recover things | |
c.AbortWithStatus(http.StatusInternalServerError) | |
} | |
}() | |
c.Next() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func EditProfileFn(c *gin.Context) { | |
currentUser := models.User{} | |
session := sessions.Default(c) | |
u := session.Get("user") | |
if u != nil { | |
currentUser = u.(models.User) | |
} | |
dbc, exist := c.Get("db") | |
if !exist { | |
c.HTML(http.StatusOK, "index", gin.H{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func NewServer() *gin.Engine { | |
RegisterSerializers() | |
gin.SetMode(gin.ReleaseMode) | |
srv := gin.Default() | |
srv.Use(ResisterDatabase()) | |
srv.Use(RegisterSession()) | |
srv.HTMLRender = TemplateRender() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func EditProfileFn(c *gin.Context) { | |
currentUser := models.User{} | |
session := sessions.Default(c) | |
u := session.Get("user") | |
if u != nil { | |
currentUser = u.(models.User) | |
} | |
dbc, exist := c.Get("db") | |
if !exist { | |
c.HTML(http.StatusOK, "index", gin.H{ |