Handle web errors better.
This commit is contained in:
parent
d661c0352f
commit
6fe5510291
18
src/web.go
18
src/web.go
@ -21,6 +21,11 @@ func (i *Instance) Serve(addr string) error {
|
|||||||
|
|
||||||
// TODO: replace Default with New and use custom logger and stuff?
|
// TODO: replace Default with New and use custom logger and stuff?
|
||||||
i.router = gin.Default()
|
i.router = gin.Default()
|
||||||
|
i.router.NoRoute(func() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
c.HTML(http.StatusNotFound, "page_404.html", nil)
|
||||||
|
}
|
||||||
|
}())
|
||||||
|
|
||||||
// Load templates
|
// Load templates
|
||||||
funcmap := template.FuncMap{
|
funcmap := template.FuncMap{
|
||||||
@ -42,7 +47,6 @@ func (i *Instance) Serve(addr string) error {
|
|||||||
i.router.Static("/static", "./static")
|
i.router.Static("/static", "./static")
|
||||||
|
|
||||||
i.router.GET("/", i.page_index)
|
i.router.GET("/", i.page_index)
|
||||||
i.router.GET("/error", i.page_error)
|
|
||||||
|
|
||||||
i.router.GET("/server/:server_id/*slug", i.page_server)
|
i.router.GET("/server/:server_id/*slug", i.page_server)
|
||||||
i.router.GET("/server/:server_id", i.page_server)
|
i.router.GET("/server/:server_id", i.page_server)
|
||||||
@ -61,16 +65,16 @@ func (i *Instance) page_index(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Instance) page_error(c *gin.Context) {
|
|
||||||
c.HTML(http.StatusNotFound, "page_404.html", nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Instance) page_server(c *gin.Context) {
|
func (i *Instance) page_server(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("server_id"), 10, 0)
|
id, err := strconv.ParseInt(c.Param("server_id"), 10, 0)
|
||||||
check_error(err)
|
if err != nil {
|
||||||
|
c.HTML(http.StatusNotFound, "page_404.html", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s, err := i.DB.GetServer(int(id))
|
s, err := i.DB.GetServer(int(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Redirect(http.StatusTemporaryRedirect, "/error")
|
c.HTML(http.StatusNotFound, "page_404.html", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
type weekday struct {
|
type weekday struct {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user