forked from shijuvar/go-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (131 loc) · 4.02 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"html/template"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
)
type Note struct {
Title string
Description string
CreatedOn time.Time
}
//View Model for edit
type EditNote struct {
Note
Id string
}
//Store for the Notes collection
var noteStore = make(map[string]Note)
//Variable to generate key for the collection
var id int = 0
var templates map[string]*template.Template
//Compile view templates
func init() {
if templates == nil {
templates = make(map[string]*template.Template)
}
templates["index"] = template.Must(template.ParseFiles("templates/index.html", "templates/base.html"))
templates["add"] = template.Must(template.ParseFiles("templates/add.html", "templates/base.html"))
templates["edit"] = template.Must(template.ParseFiles("templates/edit.html", "templates/base.html"))
}
//Render templates for the given name, template definition and data object
func renderTemplate(w http.ResponseWriter, name string, template string, viewModel interface{}) {
// Ensure the template exists in the map.
tmpl, ok := templates[name]
if !ok {
http.Error(w, "The template does not exist.", http.StatusInternalServerError)
}
err := tmpl.ExecuteTemplate(w, template, viewModel)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
//Handler for "/notes/save" for save a new item into the data store
func saveNote(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
title := r.PostFormValue("title")
desc := r.PostFormValue("description")
note := Note{title, desc, time.Now()}
//increment the value of id for generating key for the map
id++
//convert id value to string
k := strconv.Itoa(id)
noteStore[k] = note
http.Redirect(w, r, "/", 302)
}
//Handler for "/notes/add" for add a new item
func addNote(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "add", "base", nil)
}
//Handler for "/notes/edit/{id}" to edit an item
func editNote(w http.ResponseWriter, r *http.Request) {
var viewModel EditNote
//change: moved render template call inside if block.
//Read value from route variable
vars := mux.Vars(r)
k := vars["id"]
if note, ok := noteStore[k]; ok {
viewModel = EditNote{note, k}
renderTemplate(w, "edit", "base", viewModel)
} else {
http.Error(w, "Could not find the resource to edit.", http.StatusBadRequest)
}
}
//Handler for "/notes/update/{id}" which update an item into the data store
func updateNote(w http.ResponseWriter, r *http.Request) {
//Read value from route variable
vars := mux.Vars(r)
k := vars["id"]
var noteToUpd Note
if note, ok := noteStore[k]; ok {
r.ParseForm()
noteToUpd.Title = r.PostFormValue("title")
noteToUpd.Description = r.PostFormValue("description")
noteToUpd.CreatedOn = note.CreatedOn
//delete existing item and add the updated item
delete(noteStore, k)
noteStore[k] = noteToUpd
} else {
http.Error(w, "Could not find the resource to update.", http.StatusBadRequest)
}
http.Redirect(w, r, "/", 302)
}
//Handler for "/notes/delete/{id}" which delete an item form the store
func deleteNote(w http.ResponseWriter, r *http.Request) {
//Read value from route variable
vars := mux.Vars(r)
k := vars["id"]
// Remove from Store
if _, ok := noteStore[k]; ok {
//delete existing item
delete(noteStore, k)
} else {
http.Error(w, "Could not find the resource to delete.", http.StatusBadRequest)
}
http.Redirect(w, r, "/", 302)
}
//Handler for "/" which render the index page
func getNotes(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "index", "base", noteStore)
}
//Entry point of the program
func main() {
r := mux.NewRouter().StrictSlash(false)
fs := http.FileServer(http.Dir("public"))
r.Handle("/public/", fs)
r.HandleFunc("/", getNotes)
r.HandleFunc("/notes/add", addNote)
r.HandleFunc("/notes/save", saveNote)
r.HandleFunc("/notes/edit/{id}", editNote)
r.HandleFunc("/notes/update/{id}", updateNote)
r.HandleFunc("/notes/delete/{id}", deleteNote)
server := &http.Server{
Addr: ":8080",
Handler: r,
}
log.Println("Listening...")
server.ListenAndServe()
}