diff --git a/TODO.md b/TODO.md
index 622d6ba..f295b39 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,9 +2,10 @@
List of things to add to this project
* Improve config and use reflection when loading it
+* Do to other methods what was done to GetDays/GetNotes?
+* API revamp
* Check export function for improvements
-* Add notes to frontend?
+* Add note creation and deletion to frontend
* More slog.Debug and a debug flag?
-* Think about timezones
-* Consider more secure auth methods
+* Consider less clunky auth method
* *Go* dependency-less? <-- this is a terrible idea
\ No newline at end of file
diff --git a/pages/list.html b/pages/list.html
index 195f2cd..f902047 100644
--- a/pages/list.html
+++ b/pages/list.html
@@ -1,5 +1,6 @@
{{define "main"}}
-
{{.Title}}
+{{.Title}}
+{{.Description}}
{{range .Entries}}
- {{.Title}}
diff --git a/public/main.css b/public/main.css
index e878684..374719c 100644
--- a/public/main.css
+++ b/public/main.css
@@ -1,7 +1,6 @@
body {
color: #454545;
background-color: #f5f0e1;
- font-size: 16px;
margin: 2em auto;
max-width: 640px;
padding: 1em;
@@ -12,10 +11,13 @@ body {
display: flex;
flex-direction: column;
}
-h2 { margin-bottom:12px; }
a, a:visited { color: #f85552; }
a:hover, a:visited:hover { color: #e66868; }
+h2 { margin-bottom:12px; }
+.list-title { margin-bottom: 0}
+.list-desc { margin-top: 0 }
+
textarea, input {
background: #f5f2ee;
max-width: 640px;
diff --git a/routes.go b/routes.go
index da54941..90e2794 100644
--- a/routes.go
+++ b/routes.go
@@ -12,8 +12,9 @@ import (
)
type EntryList struct {
- Title string
- Entries []Entry
+ Title string
+ Description string
+ Entries []Entry
}
type Entry struct {
@@ -22,6 +23,8 @@ type Entry struct {
Link string
}
+type formatEntries func([]string) []Entry
+
// NotFound returns a user-friendly 404 error page
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
@@ -70,27 +73,15 @@ func PostToday(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}
-// GetDays renders HTML list of previous days' entries
-func GetDays(w http.ResponseWriter, r *http.Request) {
- day, err := ListFiles("day")
+// GetEntries is a generic HTML renderer for a list
+func GetEntries(w http.ResponseWriter, r *http.Request, title string, description string, dir string, format formatEntries) {
+ filesList, err := ListFiles(dir)
if err != nil {
- slog.Error("error reading file list", "directory", "day", "error", err)
+ slog.Error("error reading file list", "directory", dir, "error", err)
InternalError(w, r)
return
}
- var daysFormatted []Entry
- for i := range day {
- v := day[len(day)-1-i] // This is suboptimal, but reverse order is better here
- dayString := v
- t, err := time.Parse(time.DateOnly, v)
- if err == nil {
- dayString = t.Format("02 Jan 2006")
- }
- if v == time.Now().Format(time.DateOnly) {
- dayString = "Today"
- }
- daysFormatted = append(daysFormatted, Entry{Title: dayString, Link: "day/" + v})
- }
+ var filesFormatted = format(filesList)
files := []string{"./pages/base.html", "./pages/list.html"}
ts, err := template.ParseFiles(files...)
@@ -100,7 +91,7 @@ func GetDays(w http.ResponseWriter, r *http.Request) {
return
}
- err = ts.ExecuteTemplate(w, "base", EntryList{Title: "Previous days", Entries: daysFormatted})
+ err = ts.ExecuteTemplate(w, "base", EntryList{Title: title, Description: description, Entries: filesFormatted})
if err != nil {
slog.Error("error executing template", "error", err)
InternalError(w, r)
@@ -108,6 +99,38 @@ func GetDays(w http.ResponseWriter, r *http.Request) {
}
}
+// GetDays renders HTML list of previous days' entries
+func GetDays(w http.ResponseWriter, r *http.Request) {
+ GetEntries(w, r, "Previous days", "", "day", func(files []string) []Entry {
+ var filesFormatted []Entry
+ for i := range files {
+ v := files[len(files)-1-i] // This is suboptimal, but reverse order is better here
+ dayString := v
+ t, err := time.Parse(time.DateOnly, v)
+ if err == nil {
+ dayString = t.Format("02 Jan 2006")
+ }
+ if v == time.Now().Format(time.DateOnly) {
+ dayString = "Today"
+ }
+ filesFormatted = append(filesFormatted, Entry{Title: dayString, Link: "day/" + v})
+ }
+ return filesFormatted
+ })
+}
+
+// GetNotes renders HTML list of all notes
+func GetNotes(w http.ResponseWriter, r *http.Request) {
+ GetEntries(w, r, "Notes", "/notes/ for a new note", "notes", func(files []string) []Entry {
+ var filesFormatted []Entry
+ for _, v := range files {
+ titleString := strings.Replace(v, "-", " ", -1) // FIXME: what if I need a hyphen?
+ filesFormatted = append(filesFormatted, Entry{Title: titleString, Link: "notes/" + v})
+ }
+ return filesFormatted
+ })
+}
+
// GetDay renders HTML page for a specific day entry
func GetDay(w http.ResponseWriter, r *http.Request) {
dayString := chi.URLParam(r, "day")
@@ -146,36 +169,6 @@ func GetDay(w http.ResponseWriter, r *http.Request) {
}
}
-// GetNotes renders HTML list of all notes
-func GetNotes(w http.ResponseWriter, r *http.Request) {
- notes, err := ListFiles("notes")
- if err != nil {
- slog.Error("error reading file list", "directory", "notes", "error", err)
- InternalError(w, r)
- return
- }
- var notesFormatted []Entry
- for _, v := range notes {
- titleString := strings.Replace(v, "-", " ", -1) // FIXME: what if I need a hyphen?
- notesFormatted = append(notesFormatted, Entry{Title: titleString, Link: "notes/" + v})
- }
-
- files := []string{"./pages/base.html", "./pages/list.html"}
- ts, err := template.ParseFiles(files...)
- if err != nil {
- slog.Error("error parsing template files", "error", err)
- InternalError(w, r)
- return
- }
-
- err = ts.ExecuteTemplate(w, "base", EntryList{Title: "Notes", Entries: notesFormatted})
- if err != nil {
- slog.Error("error executing template", "error", err)
- InternalError(w, r)
- return
- }
-}
-
// GetNote renders HTML page for a note
func GetNote(w http.ResponseWriter, r *http.Request) {
noteString := chi.URLParam(r, "note")