Skip to content

Commit

Permalink
Change base URL to canonical
Browse files Browse the repository at this point in the history
  • Loading branch information
saaste committed Mar 17, 2024
1 parent f4aa01b commit 970c0d4
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion components/bookmark_add_form.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ define "bookmark_add_form" }}
<form method="post" action="{{ .BaseURL}}/admin/bookmarks/add" id="bookmark-form">
<form method="post" action="{{ .BaseURL}}admin/bookmarks/add" id="bookmark-form">
<div class="input-group">
<label for="url">URL (required)</label>
<div class="input-group horizontal">
Expand Down
2 changes: 1 addition & 1 deletion components/bookmark_delete_form.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ define "bookmark_delete_form" }}
<form method="post" action="{{ .BaseURL}}/admin/bookmarks/{{ .Bookmark.ID }}/delete">
<form method="post" action="{{ .BaseURL}}admin/bookmarks/{{ .Bookmark.ID }}/delete">
<p>Are you sure you want to delete the following bookmark?</p>

<div class="info-box">
Expand Down
2 changes: 1 addition & 1 deletion components/bookmark_edit_form.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ define "bookmark_edit_form" }}
<form method="post" action="{{ .BaseURL}}/admin/bookmarks/{{ .Bookmark.ID }}" id="bookmark-form">
<form method="post" action="{{ .BaseURL}}admin/bookmarks/{{ .Bookmark.ID }}" id="bookmark-form">
<div class="input-group">
<label for="url">URL (required)</label>
<input type="text" name="url" id="url" value="{{ .Bookmark.URL }}" required aria-required="true" />
Expand Down
2 changes: 1 addition & 1 deletion components/login_form.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ define "login_form" }}
<form method="post" action="{{ .BaseURL }}/login">
<form method="post" action="{{ .BaseURL }}login">
<div class="input-group">
<label for="password">Password</label>
<div class="input-group horizontal">
Expand Down
4 changes: 2 additions & 2 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ author_name: ""
# Author's email address. Shown in feeds
author_email: ""

# Base URL of the site
# Base URL of the site with a trailing slash (e.g https://example.com/)
base_url: http://localhost:8000

# Password required for managing bookmarks
Expand Down Expand Up @@ -49,7 +49,7 @@ template: default
# Use Gotify notifications
gotify_enabled: false

# Gotify base url
# Gotify base url with a trailing slash (e.g. https://example.com/push/)
# gotify_url: https://mygotifyinstance.com

# Gotify app token
Expand Down
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"fmt"
"os"
"strings"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -44,5 +46,10 @@ func LoadConfig() (*AppConfig, error) {

appConfig.AppVersion = appVersion

// Validate base URL
if !strings.HasSuffix(appConfig.BaseURL, "/") {
return nil, fmt.Errorf("base URL must have a trailing slash")
}

return appConfig, nil
}
10 changes: 5 additions & 5 deletions handlers/admin_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func (h *Handler) HandlePrivateBookmarks(w http.ResponseWriter, r *http.Request) {
isAuthenticated := h.isAuthenticated(r)
if !isAuthenticated {
http.Redirect(w, r, fmt.Sprintf("%s/login", h.appConf.BaseURL), http.StatusFound)
http.Redirect(w, r, fmt.Sprintf("%slogin", h.appConf.BaseURL), http.StatusFound)
return
}

Expand Down Expand Up @@ -45,7 +45,7 @@ func (h *Handler) HandlePrivateBookmarks(w http.ResponseWriter, r *http.Request)
func (h *Handler) HandleBrokenBookmarks(w http.ResponseWriter, r *http.Request) {
isAuthenticated := h.isAuthenticated(r)
if !isAuthenticated {
http.Redirect(w, r, fmt.Sprintf("%s/login", h.appConf.BaseURL), http.StatusFound)
http.Redirect(w, r, fmt.Sprintf("%slogin", h.appConf.BaseURL), http.StatusFound)
return
}

Expand All @@ -72,7 +72,7 @@ func (h *Handler) HandleBrokenBookmarks(w http.ResponseWriter, r *http.Request)
func (h *Handler) HandleBookmarkAdd(w http.ResponseWriter, r *http.Request) {
isAuthenticated := h.isAuthenticated(r)
if !isAuthenticated {
http.Redirect(w, r, fmt.Sprintf("%s/login", h.appConf.BaseURL), http.StatusFound)
http.Redirect(w, r, fmt.Sprintf("%slogin", h.appConf.BaseURL), http.StatusFound)
return
}

Expand Down Expand Up @@ -130,7 +130,7 @@ func (h *Handler) HandleBookmarkAdd(w http.ResponseWriter, r *http.Request) {
func (h *Handler) HandleBookmarkEdit(w http.ResponseWriter, r *http.Request) {
isAuthenticated := h.isAuthenticated(r)
if !isAuthenticated {
http.Redirect(w, r, fmt.Sprintf("%s/login", h.appConf.BaseURL), http.StatusFound)
http.Redirect(w, r, fmt.Sprintf("%slogin", h.appConf.BaseURL), http.StatusFound)
return
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func (h *Handler) HandleBookmarkEdit(w http.ResponseWriter, r *http.Request) {
func (h *Handler) HandleBookmarkDelete(w http.ResponseWriter, r *http.Request) {
isAuthenticated := h.isAuthenticated(r)
if !isAuthenticated {
http.Redirect(w, r, fmt.Sprintf("%s/login", h.appConf.BaseURL), http.StatusFound)
http.Redirect(w, r, fmt.Sprintf("%slogin", h.appConf.BaseURL), http.StatusFound)
return
}

Expand Down
8 changes: 4 additions & 4 deletions notifications/gotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestSendGotifyMessage(t *testing.T) {
}{
{
testName: "Successful send",
gotifyURL: "https://example.org",
gotifyURL: "https://example.org/",
gotifyToken: "test-token",
clientResponse: &http.Response{
StatusCode: http.StatusOK,
Expand All @@ -54,7 +54,7 @@ func TestSendGotifyMessage(t *testing.T) {
},
{
testName: "Invalid HTTP status from server",
gotifyURL: "https://example.org",
gotifyURL: "https://example.org/",
gotifyToken: "test-token",
clientResponse: &http.Response{
StatusCode: http.StatusInternalServerError,
Expand All @@ -65,7 +65,7 @@ func TestSendGotifyMessage(t *testing.T) {
},
{
testName: "Client error",
gotifyURL: "https://example.org",
gotifyURL: "https://example.org/",
gotifyToken: "test-token",
clientDoError: fmt.Errorf("mock error"),
expectedError: "sending a request failed: mock error",
Expand All @@ -78,7 +78,7 @@ func TestSendGotifyMessage(t *testing.T) {
},
{
testName: "Missing gotify token",
gotifyURL: "https://example.org",
gotifyURL: "https://example.org/",
gotifyToken: "",
expectedError: "notifications are disabled because Gotify is not configured",
},
Expand Down
10 changes: 5 additions & 5 deletions templates/default/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
<h1><a href="{{ .BaseURL }}">{{ .SiteName }}</a></h1>
<nav>
{{ if .IsAuthenticated }}
<a href="{{ .BaseURL }}/admin/bookmarks/add">Add Bookmark</a> |
<a href="{{ .BaseURL }}/admin/bookmarks">Private Bookmarks</a> |
<a href="{{ .BaseURL }}admin/bookmarks/add">Add Bookmark</a> |
<a href="{{ .BaseURL }}admin/bookmarks">Private Bookmarks</a> |
{{ if .BrokenBookmarksExist }}
<a href="{{ .BaseURL }}/admin/bookmarks/broken" class="warning">Broken Bookmarks</a> |
<a href="{{ .BaseURL }}admin/bookmarks/broken" class="warning">Broken Bookmarks</a> |
{{ end}}
<a href="{{ .BaseURL }}/logout">Log out</a>
<a href="{{ .BaseURL }}logout">Log out</a>
{{ else }}
<a href="{{ .BaseURL }}/login">Log in</a>
<a href="{{ .BaseURL }}login">Log in</a>
{{ end }}
</nav>
</header>
Expand Down
14 changes: 7 additions & 7 deletions templates/default/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h2>Search</h2>
<div class="header">
<h2>{{ .Title }}</h2>
{{ if .Bookmarks }}
<a href='{{ feedUrl "rss" }}'><img src="{{ .BaseURL }}/assets/feed.png" class="icon" alt="RSS feed icon" title="RSS feed"></a>
<a href='{{ feedUrl "rss" }}'><img src="{{ .BaseURL }}assets/feed.png" class="icon" alt="RSS feed icon" title="RSS feed"></a>
{{ end }}
</div>

Expand All @@ -29,8 +29,8 @@ <h2>{{ .Title }}</h2>
<h3><a href="{{ .URL }}" target="_blank" rel="noopener noreferrer">{{ .Title }}</a></h3>
{{ if $.IsAuthenticated }}
<div>
{{ if eq .IsWorking false }}<img src="{{ $.BaseURL }}/assets/warning.png" class="icon" alt="Exclamation point icon" title="Bookmark is broken">{{ end }}
{{ if eq .IsPrivate true }}<img src="{{ $.BaseURL }}/assets/lock.png" class="icon" alt="Lock icon" title="Bookmark is private">{{ end }}
{{ if eq .IsWorking false }}<img src="{{ $.BaseURL }}assets/warning.png" class="icon" alt="Exclamation point icon" title="Bookmark is broken">{{ end }}
{{ if eq .IsPrivate true }}<img src="{{ $.BaseURL }}assets/lock.png" class="icon" alt="Lock icon" title="Bookmark is private">{{ end }}
</div>
{{ end }}
</div>
Expand All @@ -44,14 +44,14 @@ <h3><a href="{{ .URL }}" target="_blank" rel="noopener noreferrer">{{ .Title }}<
<div class="tags">
Tags:
{{ range .Tags }}
<a href="{{ $.BaseURL}}/tags/{{ . }}">{{ . }}</a>
<a href="{{ $.BaseURL}}tags/{{ . }}">{{ . }}</a>
{{ end }}
</div>
{{ end}}
{{ if $.IsAuthenticated }}
<div class="actions">
<a href="{{ $.BaseURL }}/admin/bookmarks/{{ .ID }}">Edit</a>
<a href="{{ $.BaseURL }}/admin/bookmarks/{{ .ID }}/delete">Delete</a>
<a href="{{ $.BaseURL }}admin/bookmarks/{{ .ID }}">Edit</a>
<a href="{{ $.BaseURL }}admin/bookmarks/{{ .ID }}/delete">Delete</a>
</div>
{{ end }}
</div>
Expand Down Expand Up @@ -84,7 +84,7 @@ <h3><a href="{{ .URL }}" target="_blank" rel="noopener noreferrer">{{ .Title }}<
<h2>Tags</h2>
<ul>
{{ range .Tags }}
<li><a href="{{ $.BaseURL}}/tags/{{ . }}">{{ . }}</a></li>
<li><a href="{{ $.BaseURL}}tags/{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>
</section>
Expand Down

0 comments on commit 970c0d4

Please sign in to comment.