From bfdff1e2312d526ea78350fbc1fe887a58d02d22 Mon Sep 17 00:00:00 2001 From: Al Du Date: Mon, 1 Apr 2024 14:29:07 +0200 Subject: [PATCH] (feat #70) Navbar configuration: - Removed fixed links in Urchin. - Added configurable `links` property in Urchin configuration. - Updated application settings to include new links property. - Updated `templ` to add additional parameter for links array. - Updated `app_settings_test`. (fix) Makefile: - Fixed version numbers in `install-tools`. --- Makefile | 4 ++-- app/app.go | 2 +- app/contact.go | 2 +- app/image.go | 4 ++-- app/post.go | 2 +- common/app_settings.go | 3 ++- common/navigation.go | 5 ++-- .../app_settings/app_settings_test.go | 7 ++++++ urchin_config.toml | 8 +++++++ views/contact.templ | 6 +++-- views/header.templ | 24 ++++++++++--------- views/image.templ | 4 ++-- views/images.templ | 4 ++-- views/index.templ | 4 ++-- views/links.go | 15 ------------ views/post.templ | 6 +++-- 16 files changed, 54 insertions(+), 46 deletions(-) delete mode 100644 views/links.go diff --git a/Makefile b/Makefile index 92754d5..bdb3dfc 100644 --- a/Makefile +++ b/Makefile @@ -27,8 +27,8 @@ clean: rm -rf $(BUILD_DIR) install-tools: - go install github.com/pressly/goose/v3/cmd/goose@v3.18.0 0.2.543 - go install github.com/a-h/templ/cmd/templ@v0.2.543 0.2.543 + go install github.com/pressly/goose/v3/cmd/goose@v3.18.0 + go install github.com/a-h/templ/cmd/templ@v0.2.543 go install github.com/cosmtrek/air@v1.49.0 .PHONY: all build test clean diff --git a/app/app.go b/app/app.go index 4f412f2..aa7530e 100644 --- a/app/app.go +++ b/app/app.go @@ -105,7 +105,7 @@ func homeHandler(c *gin.Context, settings common.AppSettings, db database.Databa } // if not cached, create the cache - index_view := views.MakeIndex(posts) + index_view := views.MakeIndex(posts, settings.Links) html_buffer := bytes.NewBuffer(nil) err = index_view.Render(c, html_buffer) diff --git a/app/contact.go b/app/contact.go index a9cbfc5..35220d9 100644 --- a/app/contact.go +++ b/app/contact.go @@ -59,7 +59,7 @@ func makeContactFormHandler() func(*gin.Context) { // TODO : This is a duplicate of the index handler... abstract func contactHandler(c *gin.Context, app_settings common.AppSettings, db database.Database) ([]byte, error) { - index_view := views.MakeContactPage() + index_view := views.MakeContactPage(app_settings.Links) html_buffer := bytes.NewBuffer(nil) if err := index_view.Render(c, html_buffer); err != nil { log.Error().Msgf("could not render: %v", err) diff --git a/app/image.go b/app/image.go index e087b5e..f8900df 100644 --- a/app/image.go +++ b/app/image.go @@ -36,7 +36,7 @@ func imagesHandler(c *gin.Context, app_settings common.AppSettings, database dat } // if not cached, create the cache - index_view := views.MakeImagesPage(images) + index_view := views.MakeImagesPage(images, app_settings.Links) html_buffer := bytes.NewBuffer(nil) err = index_view.Render(c, html_buffer) @@ -60,7 +60,7 @@ func imageHandler(c *gin.Context, app_settings common.AppSettings, database data } // if not cached, create the cache - index_view := views.MakeImagePage(image) + index_view := views.MakeImagePage(image, app_settings.Links) html_buffer := bytes.NewBuffer(nil) err = index_view.Render(c, html_buffer) diff --git a/app/post.go b/app/post.go index 6626099..36ae13a 100644 --- a/app/post.go +++ b/app/post.go @@ -41,7 +41,7 @@ func postHandler(c *gin.Context, app_settings common.AppSettings, database datab // Generate HTML page post.Content = string(mdToHTML([]byte(post.Content))) - post_view := views.MakePostPage(post.Title, post.Content) + post_view := views.MakePostPage(post.Title, post.Content, app_settings.Links) html_buffer := bytes.NewBuffer(nil) if err = post_view.Render(c, html_buffer); err != nil { log.Error().Msgf("could not render: %v", err) diff --git a/common/app_settings.go b/common/app_settings.go index 965e2f8..0128bfb 100644 --- a/common/app_settings.go +++ b/common/app_settings.go @@ -16,6 +16,7 @@ type AppSettings struct { DatabaseName string `toml:"database_name"` WebserverPort int `toml:"webserver_port"` ImageDirectory string `toml:"image_dir"` + Links []Link `toml:"links"` } func LoadSettings() (AppSettings, error) { @@ -62,7 +63,7 @@ func LoadSettings() (AppSettings, error) { image_directory := os.Getenv("URCHIN_IMAGE_DIRECTORY") if len(image_directory) == 0 { - return AppSettings{}, fmt.Errorf("URCHIN_IMAGE_DIRECTORY is not defined\n") + return AppSettings{}, fmt.Errorf("URCHIN_IMAGE_DIRECTORY is not defined") } return AppSettings{ diff --git a/common/navigation.go b/common/navigation.go index ac10c63..e19ef6d 100644 --- a/common/navigation.go +++ b/common/navigation.go @@ -1,6 +1,7 @@ package common type Link struct { - Name string - Href string + Name string + Href string + Title string } diff --git a/tests/app_tests/app_settings/app_settings_test.go b/tests/app_tests/app_settings/app_settings_test.go index 9672dd5..7c5297f 100644 --- a/tests/app_tests/app_settings/app_settings_test.go +++ b/tests/app_tests/app_settings/app_settings_test.go @@ -36,6 +36,13 @@ func TestCorrectToml(t *testing.T) { DatabaseName: "test_database_name", WebserverPort: 99999, DatabasePort: 666, + Links: []common.Link{ + {Name: "Home", Href: "/", Title: "Homepage"}, + {Name: "About", Href: "/about", Title: "About page"}, + {Name: "Services", Href: "/services", Title: "Services page"}, + {Name: "Images", Href: "/images", Title: "Images page"}, + {Name: "Contact", Href: "/contact", Title: "Contacts page"}, + }, } bytes, err := toml.Marshal(expected) assert.Nil(t, err) diff --git a/urchin_config.toml b/urchin_config.toml index 2b68fe1..c9766b4 100644 --- a/urchin_config.toml +++ b/urchin_config.toml @@ -19,3 +19,11 @@ webserver_port = 8080 # Directory to use for storing uploaded images. image_dir = "./images" + +links = [ + { name = "Home", href = "/", title = "Homepage" }, + { name = "About", href = "/about", title = "About page" }, + { name = "Services", href = "/services", title = "Services page" }, + { name = "Images", href = "/images", title = "Images page" }, + { name = "Contact", href = "/contact", title = "Contacts page" }, +] diff --git a/views/contact.templ b/views/contact.templ index 76b1db1..2ab0d50 100644 --- a/views/contact.templ +++ b/views/contact.templ @@ -1,6 +1,8 @@ package views -templ MakeContactPage() { +import "github.com/matheusgomes28/urchin/common" + +templ MakeContactPage(links []common.Link) { @@ -14,7 +16,7 @@ templ MakeContactPage() { - @MakeNavBar() + @MakeNavBar(links)

Contact Us

diff --git a/views/header.templ b/views/header.templ index e58614a..524d980 100644 --- a/views/header.templ +++ b/views/header.templ @@ -1,14 +1,16 @@ package views -templ MakeNavBar() { -
- -
-
+import "github.com/matheusgomes28/urchin/common" + +templ MakeNavBar(links []common.Link) { +
+ +
+
} diff --git a/views/image.templ b/views/image.templ index 83e63b0..05d1452 100644 --- a/views/image.templ +++ b/views/image.templ @@ -5,7 +5,7 @@ import ( . "github.com/matheusgomes28/urchin/common" ) -templ MakeImagePage(image Image) { +templ MakeImagePage(image Image, links []Link) { @@ -19,7 +19,7 @@ templ MakeImagePage(image Image) { - @MakeNavBar() + @MakeNavBar(links)

{ image.Name }

diff --git a/views/images.templ b/views/images.templ index 017cbce..ddb94f5 100644 --- a/views/images.templ +++ b/views/images.templ @@ -5,7 +5,7 @@ import ( . "github.com/matheusgomes28/urchin/common" ) -templ MakeImagesPage(images []Image) { +templ MakeImagesPage(images []Image, links []Link) { @@ -39,7 +39,7 @@ templ MakeImagesPage(images []Image) { - @MakeNavBar() + @MakeNavBar(links)
if len(images) == 0 { diff --git a/views/index.templ b/views/index.templ index 3a2a905..d0b3edc 100644 --- a/views/index.templ +++ b/views/index.templ @@ -5,7 +5,7 @@ import ( . "github.com/matheusgomes28/urchin/common" ) -templ MakeIndex(posts []Post) { +templ MakeIndex(posts []Post, links []Link) { @@ -19,7 +19,7 @@ templ MakeIndex(posts []Post) { - @MakeNavBar() + @MakeNavBar(links)
for _, post := range posts {
diff --git a/views/links.go b/views/links.go deleted file mode 100644 index 204f9c3..0000000 --- a/views/links.go +++ /dev/null @@ -1,15 +0,0 @@ -package views - -import "github.com/matheusgomes28/urchin/common" - -// This function is used to define links for the navigation. -// Reduces the amount of copying navigation structures to all the views. -func GetUrchinLinks() []common.Link { - return []common.Link{ - {Name: "Home", Href: "/"}, - {Name: "About", Href: "/about"}, - {Name: "Services", Href: "/services"}, - {Name: "Images", Href: "/images"}, - {Name: "Contact", Href: "/contact"}, - } -} diff --git a/views/post.templ b/views/post.templ index b25b102..4f7fd21 100644 --- a/views/post.templ +++ b/views/post.templ @@ -1,7 +1,9 @@ package views +import "github.com/matheusgomes28/urchin/common" -templ MakePostPage(title string, content string) { + +templ MakePostPage(title string, content string, links []common.Link) { @@ -15,7 +17,7 @@ templ MakePostPage(title string, content string) { - @MakeNavBar() + @MakeNavBar(links)

{ title }