-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
126 lines (93 loc) · 2.97 KB
/
handlers.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
package api
import (
"fmt"
"net/http"
"github.com/IoTPanic/pixelpusher/internal/db"
log "github.com/sirupsen/logrus"
)
func apiRoot(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Pixelpusher API")
}
func handleHealthcheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
// Get the fields
username, password, ok := r.BasicAuth()
if !ok { // Check if the request is OK
w.WriteHeader(http.StatusBadRequest)
return
}
u, err := db.QueryUserFromUsername(username)
if err != nil {
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
if u.Password == hashPassword(password) {
// TODO GENERATE AND REGISTER TOKEN
}
w.WriteHeader(http.StatusUnauthorized)
}
// Will only be handling a GET request for the versioning info
func handleVersionRequest(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "1")
}
// Handle a request for a user with a ID this can be a GET, PUT,
// or DELETE request for different operations
func handleUserRequest(w http.ResponseWriter, r *http.Request) {
id, _ := resolveIDs(r)
if id == -1 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "Could get get user ID from path")
return
}
_, err := db.QueryUserFromID(id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Errorln("Failed to query user from ID", err.Error())
return
}
switch r.Method {
case "GET":
case "PUT":
case "DELETE":
default:
w.WriteHeader(http.StatusBadRequest)
}
// query user and cast to protobuf
}
// Get a list of users based off instance with a GET request or
// create a new user with a POST request.
func handleUsersRequest(w http.ResponseWriter, r *http.Request) {
}
// Handle a basic login operation, this goes through with a POST
// request.
func handleUserLogin(w http.ResponseWriter, r *http.Request) {
}
// GET request hander to get the current state of the instance.
func handleStateRequest(w http.ResponseWriter, r *http.Request) {
}
// Return a list of devices who have currently active connections.
func handleCurrentDevicesRequest(w http.ResponseWriter, r *http.Request) {
}
// Allow for a GET, PUT, or DELETE request on a device resource with
// a ID, and optionally a project ID.
func handleDeviceRequest(w http.ResponseWriter, r *http.Request) {
}
// Handle a GET or POST request to query or create a new device for
// the instance or a project through a PID.
func handleDevicesRequest(w http.ResponseWriter, r *http.Request) {
}
// Get all the projects for the instance with a GET request or create
// a new project with a POST request.
func handleProjectsRequest(w http.ResponseWriter, r *http.Request) {
}
// Query a project by ID with the usual GET request, or use a PUT
// request to mutate the resource or DELETE request to delete it.
func handleProjectRequest(w http.ResponseWriter, r *http.Request) {
}
/** For copy and paste
func n(w http.ResponseWriter, r *http.Request){
}
*/