forked from le4ndro/gowt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (182 loc) · 5.03 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"database/sql"
"log"
"net/http"
"os"
"text/template"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
)
// Tool struct
type Tool struct {
Id int
Name string
Category string
URL string
Rating int
Notes string
}
func dbConn() (db *sql.DB) {
dbDriver := "mysql"
dbUser := os.Getenv("DATABASE_USERNAME")
dbPass := os.Getenv("DATABASE_PASSWORD")
dbName := os.Getenv("DATABASE_NAME")
dbServer := os.Getenv("DATABASE_SERVER")
dbPort := os.Getenv("DATABASE_PORT")
log.Println("Database host: " + dbServer)
log.Println("Database port: " + dbPort)
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@tcp("+dbServer+":"+dbPort+")/"+dbName)
if err != nil {
panic(err.Error())
}
return db
}
var tmpl = template.Must(template.ParseGlob("templates/*"))
//Index handler
func Index(w http.ResponseWriter, r *http.Request) {
db := dbConn()
selDB, err := db.Query("SELECT * FROM tools ORDER BY id DESC")
if err != nil {
panic(err.Error())
}
tool := Tool{}
res := []Tool{}
for selDB.Next() {
var id, rating int
var name, category, url, notes string
err := selDB.Scan(&id, &name, &category, &url, &rating, ¬es)
if err != nil {
panic(err.Error())
}
log.Println("Listing Row: Id " + string(id) + " | name " + name + " | category " + category + " | url " + url + " | rating " + string(rating) + " | notes " + notes)
tool.Id = id
tool.Name = name
tool.Category = category
tool.URL = url
tool.Rating = rating
tool.Notes = notes
res = append(res, tool)
}
tmpl.ExecuteTemplate(w, "Index", res)
defer db.Close()
}
//Show handler
func Show(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("id")
selDB, err := db.Query("SELECT * FROM tools WHERE id=?", nId)
if err != nil {
panic(err.Error())
}
tool := Tool{}
for selDB.Next() {
var id, rating int
var name, category, url, notes string
err := selDB.Scan(&id, &name, &category, &url, &rating, ¬es)
if err != nil {
panic(err.Error())
}
log.Println("Showing Row: Id " + string(id) + " | name " + name + " | category " + category + " | url " + url + " | rating " + string(rating) + " | notes " + notes)
tool.Id = id
tool.Name = name
tool.Category = category
tool.URL = url
tool.Rating = rating
tool.Notes = notes
}
tmpl.ExecuteTemplate(w, "Show", tool)
defer db.Close()
}
func New(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "New", nil)
}
func Edit(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("id")
selDB, err := db.Query("SELECT * FROM tools WHERE id=?", nId)
if err != nil {
panic(err.Error())
}
tool := Tool{}
for selDB.Next() {
var id, rating int
var name, category, url, notes string
err := selDB.Scan(&id, &name, &category, &url, &rating, ¬es)
if err != nil {
panic(err.Error())
}
tool.Id = id
tool.Name = name
tool.Category = category
tool.URL = url
tool.Rating = rating
tool.Notes = notes
}
tmpl.ExecuteTemplate(w, "Edit", tool)
defer db.Close()
}
func Insert(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
name := r.FormValue("name")
category := r.FormValue("category")
url := r.FormValue("url")
rating := r.FormValue("rating")
notes := r.FormValue("notes")
insForm, err := db.Prepare("INSERT INTO tools (name, category, url, rating, notes) VALUES (?, ?, ?, ?, ?)")
if err != nil {
panic(err.Error())
}
insForm.Exec(name, category, url, rating, notes)
log.Println("Insert Data: name " + name + " | category " + category + " | url " + url + " | rating " + rating + " | notes " + notes)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func Update(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
name := r.FormValue("name")
category := r.FormValue("category")
url := r.FormValue("url")
rating := r.FormValue("rating")
notes := r.FormValue("notes")
id := r.FormValue("uid")
insForm, err := db.Prepare("UPDATE tools SET name=?, category=?, url=?, rating=?, notes=? WHERE id=?")
if err != nil {
panic(err.Error())
}
insForm.Exec(name, category, url, rating, notes, id)
log.Println("UPDATE Data: name " + name + " | category " + category + " | url " + url + " | rating " + rating + " | notes " + notes)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func Delete(w http.ResponseWriter, r *http.Request) {
db := dbConn()
tool := r.URL.Query().Get("id")
delForm, err := db.Prepare("DELETE FROM tools WHERE id=?")
if err != nil {
panic(err.Error())
}
delForm.Exec(tool)
log.Println("DELETE " + tool)
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
log.Println("Server started on: http://localhost:8080")
http.HandleFunc("/", Index)
http.HandleFunc("/show", Show)
http.HandleFunc("/new", New)
http.HandleFunc("/edit", Edit)
http.HandleFunc("/insert", Insert)
http.HandleFunc("/update", Update)
http.HandleFunc("/delete", Delete)
http.ListenAndServe(":8080", nil)
}