1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Simplify routes, handle error route

This commit is contained in:
Zack Scholl
2017-03-23 07:32:40 -06:00
parent 3a29cc5adf
commit 9becf07745

View File

@ -57,7 +57,9 @@ func loadTemplates(list ...string) multitemplate.Render {
func handlePageRequest(c *gin.Context) { func handlePageRequest(c *gin.Context) {
page := c.Param("page") page := c.Param("page")
command := c.Param("command") command := c.Param("command")
if len(command) < 2 {
command = "/ "
}
// Serve static content from memory // Serve static content from memory
if page == "static" { if page == "static" {
filename := page + command filename := page + command
@ -103,7 +105,7 @@ func handlePageRequest(c *gin.Context) {
versionsText[i] = time.Unix(v/1000000000, 0).String() versionsText[i] = time.Unix(v/1000000000, 0).String()
} }
if command == "/raw" { if command[0:2] == "/r" {
c.Writer.Header().Set("Content-Type", contentType(p.Name)) c.Writer.Header().Set("Content-Type", contentType(p.Name))
c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Max-Age", "86400")
@ -114,11 +116,19 @@ func handlePageRequest(c *gin.Context) {
return return
} }
log.Debug(command) log.Debug(command)
log.Debug("%v", command[0:2] != "/e" &&
command[0:2] != "/v" &&
command[0:2] != "/l" &&
command[0:2] != "/h")
c.HTML(http.StatusOK, "index.tmpl", gin.H{ c.HTML(http.StatusOK, "index.tmpl", gin.H{
"EditPage": command == "/edit", "EditPage": command[0:2] == "/e", // /edit
"ViewPage": command == "/view", "ViewPage": command[0:2] == "/v", // /view
"ListPage": command == "/list", "ListPage": command[0:2] == "/l", // /list
"HistoryPage": command == "/history", "HistoryPage": command[0:2] == "/h", // /history
"DontKnowPage": command[0:2] != "/e" &&
command[0:2] != "/v" &&
command[0:2] != "/l" &&
command[0:2] != "/h",
"Page": p.Name, "Page": p.Name,
"RenderedPage": template.HTML([]byte(rawHTML)), "RenderedPage": template.HTML([]byte(rawHTML)),
"RawPage": rawText, "RawPage": rawText,
@ -127,6 +137,7 @@ func handlePageRequest(c *gin.Context) {
"IsLocked": p.IsLocked, "IsLocked": p.IsLocked,
"IsEncrypted": p.IsEncrypted, "IsEncrypted": p.IsEncrypted,
"ListItems": renderList(rawText), "ListItems": renderList(rawText),
"Route": "/" + page + command,
}) })
} }