-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Added the remaining logic for pageHandler(...). + Added GetPage(...) to database. + Added left over page go files.
- Loading branch information
1 parent
68ad799
commit 6b88350
Showing
6 changed files
with
94 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package app | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/matheusgomes28/urchin/common" | ||
"github.com/matheusgomes28/urchin/database" | ||
"github.com/matheusgomes28/urchin/views" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func pageHandler(c *gin.Context, app_settings common.AppSettings, database database.Database) ([]byte, error) { | ||
var page_binding common.PageLinkBinding | ||
err := c.ShouldBindUri(&page_binding) | ||
|
||
if err != nil || len(page_binding.Link) == 0 { | ||
// TODO : we should be serving an error page | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid page uri"}) | ||
|
||
return nil, err | ||
} | ||
|
||
// Get the page with the ID | ||
page, err := database.GetPage(page_binding.Link) | ||
|
||
if err != nil || page.Content == "" { | ||
// TODO : serve the error page instead | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Page Not Found"}) | ||
return nil, err | ||
} | ||
|
||
// Generate HTML page | ||
page.Content = string(mdToHTML([]byte(page.Content))) | ||
post_view := views.MakePage(page.Title, page.Content, app_settings.AppNavbar.Links) | ||
html_buffer := bytes.NewBuffer(nil) | ||
if err = post_view.Render(c, html_buffer); err != nil { | ||
log.Error().Msgf("could not render: %v", err) | ||
} | ||
|
||
return html_buffer.Bytes(), nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package common | ||
|
||
type Page struct { | ||
Id int `json:"id"` | ||
Title string `json:"title"` | ||
Content string `json:"content"` | ||
Link string `json:"link"` | ||
} |
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
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