-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (149 loc) · 3.81 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
package main
import (
"database/sql"
"fmt"
"encoding/json"
"github.com/gorilla/mux"
"github.com/rs/cors"
"log"
"net/http"
_ "github.com/lib/pq"
)
const (
host = "postgres"
port = 5432
user = "postgres"
password = "password"
dbname = "postgres"
createtable = `CREATE TABLE users (
id serial NOT NULL,
first_name TEXT,
last_name TEXT,
age INT,
email TEXT UNIQUE NOT NULL,
avatar TEXT,
CONSTRAINT userinfo_pkey PRIMARY KEY (id)
)`
)
type User struct {
Id int `json:"id"`
Name string `json:"name"`
Surname string `json:"surname"`
Age int `json:"age"`
Email string `json:"email"`
Avatar string `json:"avatar"`
}
var dbGlobal *sql.DB
func main() {
router := mux.NewRouter()
router.HandleFunc("/users", handleUsers).Methods("GET")
router.HandleFunc("/user", handleUser).Methods("POST")
router.HandleFunc("/delete", handleDelete).Methods("POST")
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
})
handler := c.Handler(router)
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
dbGlobal = db
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
stmt, err := db.Prepare(createtable)
if err != nil {
fmt.Println(err.Error())
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
fmt.Println(err.Error())
}
http.ListenAndServe(":8080", handler)
}
func handleDelete(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
switch req.Method {
case "POST":
log.Println("Delete received ")
user := new(User)
decoder := json.NewDecoder(req.Body)
error := decoder.Decode(&user)
if error != nil {
log.Println(error.Error())
http.Error(res, error.Error(), http.StatusInternalServerError)
return
}
log.Println(user)
sqlStatement := `
DELETE FROM users
WHERE id = $1;`
_, err := dbGlobal.Exec(sqlStatement, user.Id)
if err != nil {
log.Println(err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
}
}
func handleUser(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
switch req.Method {
case "POST":
log.Println("POST received %s", req.Body)
user := new(User)
decoder := json.NewDecoder(req.Body)
error := decoder.Decode(&user)
if error != nil {
log.Println(error.Error())
http.Error(res, error.Error(), http.StatusInternalServerError)
return
}
log.Println(user)
log.Println(dbGlobal)
var lastInsertId int
err := dbGlobal.QueryRow("INSERT INTO users(first_name,last_name,age,email,avatar) VALUES($1,$2,$3,$4,$5) returning id;", user.Name, user.Surname, user.Age, user.Email, user.Avatar).Scan(&lastInsertId)
if err != nil {
log.Println(err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("last inserted id =", lastInsertId)
res.WriteHeader(http.StatusCreated)
}
}
func handleUsers(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
rows, err := dbGlobal.Query("SELECT * FROM users")
if err != nil {
log.Println(err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
var users []User
for rows.Next() {
var usr User
err = rows.Scan(&usr.Id, &usr.Name, &usr.Surname, &usr.Age, &usr.Email, &usr.Avatar)
if err != nil {
fmt.Println("error:", err)
}
users = append(users, usr)
}
if len(users) > 0 {
b, err := json.Marshal(users)
if err != nil {
fmt.Println("error:", err)
}
fmt.Fprintf(res, string(b))
}
}