Skip to content

Commit

Permalink
(feat #70) Navbar configuration: (#74)
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
Co-authored-by: matheusgomes28 <[email protected]>
  • Loading branch information
3 people authored Apr 11, 2024
1 parent 86f0ef8 commit 31a1c6c
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ clean:
rm -rf $(BUILD_DIR)

install-tools:
go install github.com/pressly/goose/v3/cmd/[email protected] 0.2.543
go install github.com/a-h/templ/cmd/[email protected] 0.2.543
go install github.com/pressly/goose/v3/cmd/[email protected]
go install github.com/a-h/templ/cmd/[email protected]
go install github.com/cosmtrek/[email protected]

.PHONY: all build test clean
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/urchin-admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions common/app_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions common/navigation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

type Link struct {
Name string
Href string
Name string
Href string
Title string
}
9 changes: 9 additions & 0 deletions docker/urchin_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
]
9 changes: 9 additions & 0 deletions tests/app_tests/app_settings/app_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions urchin_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
]
6 changes: 4 additions & 2 deletions views/contact.templ
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package views

templ MakeContactPage() {
import "github.com/matheusgomes28/urchin/common"

templ MakeContactPage(links []common.Link) {
<!DOCTYPE html>
<html lang="en">

Expand All @@ -14,7 +16,7 @@ templ MakeContactPage() {
</head>

<body>
@MakeNavBar()
@MakeNavBar(links)
<main>
<div id="contact-form">
<h2>Contact Us</h2>
Expand Down
24 changes: 13 additions & 11 deletions views/header.templ
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package views

templ MakeNavBar() {
<header>
<nav>
<ul>
for _, link := range GetUrchinLinks() {
<li><a href={templ.URL(link.Href)}>{ link.Name }</a></li> // Pass the links in here
}
</ul>
</nav>
</header>
<br />
import "github.com/matheusgomes28/urchin/common"

templ MakeNavBar(links []common.Link) {
<header>
<nav>
<ul>
for _, link := range links {
<li><a href={ templ.URL(link.Href) }>{ link.Name }</a></li> // Pass the links in here
}
</ul>
</nav>
</header>
<br/>
}
4 changes: 2 additions & 2 deletions views/image.templ
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
. "github.com/matheusgomes28/urchin/common"
)

templ MakeImagePage(image Image) {
templ MakeImagePage(image Image, links []Link) {
<!DOCTYPE html>
<html lang="en">

Expand All @@ -19,7 +19,7 @@ templ MakeImagePage(image Image) {
</head>

<body>
@MakeNavBar()
@MakeNavBar(links)
<main>
<article>
<h3>{ image.Name }</h3>
Expand Down
4 changes: 2 additions & 2 deletions views/images.templ
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
. "github.com/matheusgomes28/urchin/common"
)

templ MakeImagesPage(images []Image) {
templ MakeImagesPage(images []Image, links []Link) {
<!DOCTYPE html>
<html lang="en">

Expand Down Expand Up @@ -39,7 +39,7 @@ templ MakeImagesPage(images []Image) {
</head>

<body>
@MakeNavBar()
@MakeNavBar(links)
<main>
<article>
if len(images) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions views/index.templ
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
. "github.com/matheusgomes28/urchin/common"
)

templ MakeIndex(posts []Post) {
templ MakeIndex(posts []Post, links []Link) {
<!DOCTYPE html>
<html lang="en">

Expand All @@ -19,7 +19,7 @@ templ MakeIndex(posts []Post) {
</head>

<body>
@MakeNavBar()
@MakeNavBar(links)
<main>
for _, post := range posts {
<article>
Expand Down
15 changes: 0 additions & 15 deletions views/links.go

This file was deleted.

6 changes: 4 additions & 2 deletions views/post.templ
Original file line number Diff line number Diff line change
@@ -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) {
<!DOCTYPE html>
<html lang="en">

Expand All @@ -15,7 +17,7 @@ templ MakePostPage(title string, content string) {
</head>

<body>
@MakeNavBar()
@MakeNavBar(links)
<main>
<article>
<h2>{ title }</h2>
Expand Down

0 comments on commit 31a1c6c

Please sign in to comment.