-
Notifications
You must be signed in to change notification settings - Fork 0
/
exist.go
41 lines (32 loc) · 934 Bytes
/
exist.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
package handlers
import (
"net/http"
"goboxserver/db"
"encoding/json"
)
// This handler check if a user exists
type ExistHandler struct {
db *db.DB
}
// Create a new handler
func NewExistHandler (db *db.DB) *ExistHandler {
return &ExistHandler{ db: db }
}
// Handler function that checks if the user exist
func (h *ExistHandler) ServeHTTP (response http.ResponseWriter, request *http.Request) {
// Decode the parameters sent as query string in GET request
params := request.URL.Query()
// Get the username param
username := params.Get("username");
// If is not valid ...
if len(username) <= 0 {
// Send the error
http.Error(response, "Invalid Request", 400)
return
}
res := map[string]bool {
"exist": h.db.ExistUser(username),
}
// Send the json response to the client
json.NewEncoder(response).Encode(res)
}