From 31a1c6c1b458d69b35eb3bdd2277878d1e446997 Mon Sep 17 00:00:00 2001 From: AlDu2407 <40807522+AlDu2407@users.noreply.github.com> Date: Thu, 11 Apr 2024 21:43:25 +0200 Subject: [PATCH] (feat #70) Navbar configuration: (#74) - 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`. --------- Co-authored-by: Sourab Paul <86782734+SourabHere@users.noreply.github.com> Co-authored-by: matheusgomes28 --- Makefile | 4 ++-- app/app.go | 2 +- app/contact.go | 2 +- app/image.go | 4 ++-- app/post.go | 2 +- cmd/urchin-admin/main.go | 2 +- common/app_settings.go | 5 ++++ common/navigation.go | 5 ++-- docker/urchin_config.toml | 9 +++++++ .../app_settings/app_settings_test.go | 9 +++++++ urchin_config.toml | 9 +++++++ 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 +++-- 18 files changed, 70 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..37b3f71 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.AppNavbar.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..6aa8384 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.AppNavbar.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..2e65d31 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.AppNavbar.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.AppNavbar.Links) html_buffer := bytes.NewBuffer(nil) err = index_view.Render(c, html_buffer) diff --git a/app/post.go b/app/post.go index 83a756a..0fc5614 100644 --- a/app/post.go +++ b/app/post.go @@ -50,7 +50,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.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) diff --git a/cmd/urchin-admin/main.go b/cmd/urchin-admin/main.go index dd329d9..49659a6 100644 --- a/cmd/urchin-admin/main.go +++ b/cmd/urchin-admin/main.go @@ -21,7 +21,7 @@ func main() { flag.Parse() var app_settings common.AppSettings - if *config_toml != "" { + if *config_toml == "" { log.Error().Msgf("config not specified (--config)") os.Exit(-1) } diff --git a/common/app_settings.go b/common/app_settings.go index a19e4f3..0f9db9b 100644 --- a/common/app_settings.go +++ b/common/app_settings.go @@ -4,6 +4,10 @@ import ( "github.com/BurntSushi/toml" ) +type Navbar struct { + Links []Link `toml:"links"` +} + type AppSettings struct { DatabaseAddress string `toml:"database_address"` DatabasePort int `toml:"database_port"` @@ -12,6 +16,7 @@ type AppSettings struct { DatabaseName string `toml:"database_name"` WebserverPort int `toml:"webserver_port"` ImageDirectory string `toml:"image_dir"` + AppNavbar Navbar `toml:"navbar"` } func ReadConfigToml(filepath string) (AppSettings, error) { 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/docker/urchin_config.toml b/docker/urchin_config.toml index c0e834b..d7e2132 100644 --- a/docker/urchin_config.toml +++ b/docker/urchin_config.toml @@ -19,3 +19,12 @@ webserver_port = 8080 # Directory to use for storing uploaded images. image_dir = "./images" + +[navbar] +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/tests/app_tests/app_settings/app_settings_test.go b/tests/app_tests/app_settings/app_settings_test.go index 9672dd5..cc880c5 100644 --- a/tests/app_tests/app_settings/app_settings_test.go +++ b/tests/app_tests/app_settings/app_settings_test.go @@ -36,6 +36,15 @@ func TestCorrectToml(t *testing.T) { DatabaseName: "test_database_name", WebserverPort: 99999, DatabasePort: 666, + AppNavbar: common.Navbar{ + 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..839e93e 100644 --- a/urchin_config.toml +++ b/urchin_config.toml @@ -19,3 +19,12 @@ webserver_port = 8080 # Directory to use for storing uploaded images. image_dir = "./images" + +[navbar] +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 }