Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat #70) Navbar configuration: #74

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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 @@

// 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)

Check warning on line 62 in app/contact.go

View check run for this annotation

Codecov / codecov/patch

app/contact.go#L62

Added line #L62 was not covered by tests
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 @@
}

// if not cached, create the cache
index_view := views.MakeImagesPage(images)
index_view := views.MakeImagesPage(images, app_settings.Links)

Check warning on line 39 in app/image.go

View check run for this annotation

Codecov / codecov/patch

app/image.go#L39

Added line #L39 was not covered by tests
html_buffer := bytes.NewBuffer(nil)

err = index_view.Render(c, html_buffer)
Expand All @@ -60,7 +60,7 @@
}

// if not cached, create the cache
index_view := views.MakeImagePage(image)
index_view := views.MakeImagePage(image, app_settings.Links)

Check warning on line 63 in app/image.go

View check run for this annotation

Codecov / codecov/patch

app/image.go#L63

Added line #L63 was not covered by tests
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 @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion common/app_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DatabaseName string `toml:"database_name"`
WebserverPort int `toml:"webserver_port"`
ImageDirectory string `toml:"image_dir"`
Links []Link `toml:"links"`
}

func LoadSettings() (AppSettings, error) {
Expand Down Expand Up @@ -62,7 +63,7 @@

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")

Check warning on line 66 in common/app_settings.go

View check run for this annotation

Codecov / codecov/patch

common/app_settings.go#L66

Added line #L66 was not covered by tests
}

return AppSettings{
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
}
7 changes: 7 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,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)
Expand Down
8 changes: 8 additions & 0 deletions urchin_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ webserver_port = 8080

# Directory to use for storing uploaded images.
image_dir = "./images"

links = [
AlDu2407 marked this conversation as resolved.
Show resolved Hide resolved
{ 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
Loading