Skip to content

Commit

Permalink
Making page handler work
Browse files Browse the repository at this point in the history
+ Added the remaining logic for pageHandler(...).
+ Added GetPage(...) to database.
+ Added left over page go files.
  • Loading branch information
matheusgomes28 committed Oct 2, 2024
1 parent 68ad799 commit 6b88350
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 10 deletions.
27 changes: 17 additions & 10 deletions admin-app/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"

"github.com/gin-gonic/gin"
Expand All @@ -27,8 +26,14 @@ func postPageHandler(database database.Database) func(*gin.Context) {
return
}

err := checkRequiredPageData(add_page_request)
if err != nil {
c.JSON(http.StatusBadRequest, common.MsgErrorRes("invalid page data"))
return
}

decoder := json.NewDecoder(c.Request.Body)
err := decoder.Decode(&add_page_request)
err = decoder.Decode(&add_page_request)

if err != nil {
log.Warn().Msgf("invalid page request: %v", err)
Expand Down Expand Up @@ -129,22 +134,24 @@ func checkRequiredPageData(add_page_request AddPageRequest) error {
return fmt.Errorf("missing required data 'Content'")
}

err := validateLinkRegex(add_page_request.Link)
err := validateLink(add_page_request.Link)
if err != nil {
return err
}

return nil
}

func validateLinkRegex(link string) error {
match, err := regexp.MatchString("^[a-zA-Z0-9_\\-]+$", link)
if err != nil {
return fmt.Errorf("could not match the string: %v", err)
}
func validateLink(link string) error {
for char := range link {
char_val := int(char)
is_uppercase := (char_val >= 65) && (char_val <= 90)
is_lowercase := (char_val >= 97) && (char_val <= 122)
is_sign := (char == '-') || (char == '_')

if !match {
return fmt.Errorf("could not match the string")
if !(is_uppercase || is_lowercase || is_sign) {
return fmt.Errorf("invalid character in link %s", char)
}
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g

// Where all the static files (css, js, etc) are served from
r.Static("/static", "./static")

return r
}

Expand All @@ -54,6 +55,7 @@ func addCachableHandler(e *gin.Engine, method string, endpoint string, generator
if app_settings.CacheEnabled {
cached_endpoint, err := (*cache).Get(c.Request.RequestURI)
if err == nil {
log.Info().Msgf("cache hit for page: %s", c.Request.RequestURI)
c.Data(http.StatusOK, "text/html; charset=utf-8", cached_endpoint.Contents)
return
}
Expand Down
43 changes: 43 additions & 0 deletions app/page.go
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
}
8 changes: 8 additions & 0 deletions common/page.go
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"`
}
19 changes: 19 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Database interface {
ChangePost(id int, title string, excerpt string, content string) error
DeletePost(id int) (int, error)
AddPage(title string, content string, link string) (int, error)
GetPage(link string) (common.Page, error)
}

type SqlDatabase struct {
Expand Down Expand Up @@ -160,6 +161,24 @@ func (db SqlDatabase) AddPage(title string, content string, link string) (int, e
return int(id), nil
}

func (db SqlDatabase) GetPage(link string) (common.Page, error) {
rows, err := db.Connection.Query("SELECT id, title, content, link FROM pages WHERE link=?;", link)
if err != nil {
return common.Page{}, err
}
defer func() {
err = errors.Join(rows.Close())
}()

page := common.Page{}
rows.Next()
if err = rows.Scan(&page.Id, &page.Title, &page.Content, &page.Link); err != nil {
return common.Page{}, err
}

return page, nil
}

func MakeSqlConnection(user string, password string, address string, port int, database string) (SqlDatabase, error) {

/// TODO : let user specify the DB
Expand Down
5 changes: 5 additions & 0 deletions tests/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type DatabaseMock struct {
AddPostHandler func(string, string, string) (int, error)
DeletePostHandler func(int) (int, error)
AddPageHandler func(string, string, string) (int, error)
GetPageHandler func(string) (common.Page, error)
}

func (db DatabaseMock) GetPosts(limit int, offset int) ([]common.Post, error) {
Expand All @@ -35,3 +36,7 @@ func (db DatabaseMock) DeletePost(id int) (int, error) {
func (db DatabaseMock) AddPage(title string, content string, link string) (int, error) {
return db.AddPageHandler(title, content, link)
}

func (db DatabaseMock) GetPage(link string) (common.Page, error) {
return db.GetPageHandler(link)
}

0 comments on commit 6b88350

Please sign in to comment.