-
Notifications
You must be signed in to change notification settings - Fork 0
/
pages.go
132 lines (109 loc) · 3.35 KB
/
pages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
type headerData struct {
Title string
HideHeader bool
}
// handleIndex is a http.HandlerFunc that renders the index page to the ResponseWriter
func (app *App) handleIndex(w http.ResponseWriter, r *http.Request) {
var data struct {
HeaderData headerData
}
data.HeaderData.Title = "eGoNote"
data.HeaderData.HideHeader = true
app.templates.ExecuteTemplate(w, "index", data)
}
// handleLoginPage is a http.HandlerFunc that renders the login page to the ResponseWriter, it will redirect the request if the user is logged in
func (app *App) handleLoginPage(w http.ResponseWriter, r *http.Request) {
// check if user is logged in
userID := getUserIDFromContext(r)
if userID != 0 {
http.Redirect(w, r, "/notes", http.StatusSeeOther)
return
}
var data struct {
HeaderData headerData
}
data.HeaderData.Title = "Login"
data.HeaderData.HideHeader = true
app.templates.ExecuteTemplate(w, "login", data)
}
// handleRegisterPage is a http.HandlerFunc that renders the register page to the ResponseWriter, it will redirect the request if the user is logged in
func (app *App) handleRegisterPage(w http.ResponseWriter, r *http.Request) {
// check if user is logged in
userID := getUserIDFromContext(r)
if userID != 0 {
http.Redirect(w, r, "/notes", http.StatusSeeOther)
return
}
var data struct {
HeaderData headerData
}
data.HeaderData.Title = "Register"
data.HeaderData.HideHeader = true
app.templates.ExecuteTemplate(w, "register", data)
}
// handleNotesPage is a http.Handler that renders the notes page to the ResponseWriter, it will redirect the request if the user is not logged in
func (app *App) handleNotesPage(w http.ResponseWriter, r *http.Request) {
// confirm that user is logged in
userID := getUserIDFromContext(r)
if userID == 0 {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
var data struct {
HeaderData headerData
}
data.HeaderData.Title = "Notebook"
app.templates.ExecuteTemplate(w, "notes_page", data)
}
// handleIndividualNotesPage is a http.Handler that renders a specific note's page to the ResponseWriter, it will redirect the request if the user is not logged in
func (app *App) handleIndividualNotePage(w http.ResponseWriter, r *http.Request) {
// check if user is logged in
userID := getUserIDFromContext(r)
if userID == 0 {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
// get ID value to display correct note
idString := chi.URLParam(r, "id")
id, err := strconv.Atoi(idString)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad Request"))
return
}
var data struct {
HeaderData headerData
NoteID int
}
data.NoteID = id
if r.URL.Query().Get("edit") == "true" {
data.HeaderData.Title = "Editing Note"
app.templates.ExecuteTemplate(w, "edit_note_page", data)
} else {
data.HeaderData.Title = "Viewing Note"
app.templates.ExecuteTemplate(w, "individual_note_page", data)
}
}
func (app *App) handleSharelinkPage(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
data := struct {
HeaderData headerData
ID string
}{
headerData{
Title: "Viewing Sharelink",
HideHeader: true,
},
id,
}
err := app.templates.ExecuteTemplate(w, "sharelink_page", data)
if err != nil {
app.log.Println("Error processing template: ", err.Error())
}
}