-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (90 loc) · 2.17 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/rs/cors"
uuid "github.com/satori/go.uuid"
"github.com/urfave/negroni"
)
func newUserHandler(w http.ResponseWriter, r *http.Request) {
var user User
if r.Body == nil {
http.Error(w, "no request body found", 400)
return
}
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, err.Error(), 400)
}
u := NewUser(
user.ID,
user.Name,
user.Email,
)
saveErr := u.Save()
if saveErr != nil {
http.Error(w, "error while saving", 400)
return
}
}
func fetchOneUserHandler(w http.ResponseWriter, r *http.Request) {
var user User
vars := mux.Vars(r)
userID := vars["id"]
user.ID = uuid.FromStringOrNil(userID)
u := user.Fetch()
json.NewEncoder(w).Encode(u)
}
func fetchAllUsersHandler(w http.ResponseWriter, r *http.Request) {
var user User
people := user.FetchAll()
json.NewEncoder(w).Encode(people)
}
func deleteUserHandler(w http.ResponseWriter, r *http.Request) {
var user User
vars := mux.Vars(r)
id := vars["id"]
user.ID = uuid.FromStringOrNil(id)
err := user.Delete()
if err != nil {
http.Error(w, err.Error(), 400)
return
}
json.NewEncoder(w).Encode(map[string]string{
"message": "user successfully deleted",
})
}
func logger(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
t1 := time.Now()
next(w, r)
t2 := time.Now()
log.Printf("[%s] %q %v", r.Method, r.URL.String(), t2.Sub(t1))
}
// Router is a collection of the application's routes and returns an instance of negroni
func Router() *negroni.Negroni {
// router
r := mux.NewRouter()
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
n := negroni.New()
n.Use(negroni.HandlerFunc(logger))
n.Use(c)
r.HandleFunc("/users", fetchAllUsersHandler).Methods("GET")
r.HandleFunc("/users", newUserHandler).Methods("POST")
r.HandleFunc("/users/{id}", fetchOneUserHandler).Methods("GET")
r.HandleFunc("/users/{id}", deleteUserHandler).Methods("DELETE")
n.UseHandler(r)
return n
}
func main() {
// init DB
var s Storage
s.BucketInit()
fmt.Println("server is running on port 8080")
log.Fatal(http.ListenAndServe(":8080", Router()))
}