This repository has been archived by the owner on Jan 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
136 lines (125 loc) · 3.95 KB
/
app.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"gopkg.in/oauth2.v3/server"
)
type App struct {
Router *mux.Router
DB *sql.DB
OauthServer *server.Server
}
func (a *App) Initialize(user, password, dbname string) {
connectionString :=
fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbname)
var err error
a.DB, err = sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
a.Router = mux.NewRouter()
a.OauthServer = oauth_server()
a.initializeRoutes()
}
func (a *App) Run(addr string) {
log.Fatal(http.ListenAndServe(":8000", a.Router))
}
func (a *App) initializeRoutes() {
a.Router.HandleFunc("/login", a.login).Methods("POST")
a.Router.HandleFunc("/signup", a.signup).Methods("POST")
a.Router.HandleFunc("/services/{userid}", a.getServices).Methods("GET")
a.Router.HandleFunc("/services", a.putService).Methods("POST")
a.Router.HandleFunc("/services", a.deleteService).Methods("DELETE")
a.Router.HandleFunc("/authorize", a.oauthAuthorize).Methods("POST")
a.Router.HandleFunc("/token", a.oauthToken).Methods("GET")
}
func (a *App) oauthAuthorize(w http.ResponseWriter, r *http.Request) {
err := a.OauthServer.HandleAuthorizeRequest(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}
func (a *App) oauthToken(w http.ResponseWriter, r *http.Request) {
a.OauthServer.HandleTokenRequest(w, r)
}
func (a *App) login(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var u user
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&u); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
if err := u.login(a.DB); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"error": "ok"})
}
func (a *App) signup(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var u user
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&u); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
if err := u.signup(a.DB); err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"error": "ok"})
}
func (a *App) getServices(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
vars := mux.Vars(r)
serviceNames, err := getServices(vars["userid"], a.DB)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"services": strings.Join(serviceNames, ","), "error": "ok"})
}
func (a *App) putService(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var s service
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&s); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
if err := s.createService(a.DB); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"error": "ok"})
}
func (a *App) deleteService(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var s service
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&s); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
if err := s.deleteService(a.DB); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"error": "ok"})
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}