-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_account.go
61 lines (53 loc) · 1.58 KB
/
api_account.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
package main
import (
"fmt"
"net/http"
log "github.com/Sirupsen/logrus"
"gopkg.in/mgo.v2"
)
// AccountHandler dispatches requests to handlers that manage the /account resource based on
// request method.
func AccountHandler(c *Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
CreateHandler(c, w, r)
default:
APIError{
Message: fmt.Sprintf("Unsupported method %s. Only POST is accepted for this resource.",
r.Method),
}.Log("").Report(w, http.StatusMethodNotAllowed)
}
}
// CreateHandler creates and persists a new account based on a username and password. An error is
// returned if the username is not unique. Otherwise, an accepted status is returned.
func CreateHandler(c *Context, w http.ResponseWriter, r *http.Request) {
accountName, password, ok := ExtractPasswordCredentials(w, r, "Account creation")
if !ok {
return
}
account, err := NewAccount(accountName, password)
if err != nil {
APIError{
Message: fmt.Sprintf("Unable to create account: %v", err),
}.Log("").Report(w, http.StatusInternalServerError)
return
}
err = c.Storage.CreateAccount(account)
if mgo.IsDup(err) {
APIError{
Message: fmt.Sprintf(
`The account name "%s" has already been taken. Please choose another.`,
accountName,
),
}.Log("").Report(w, http.StatusConflict)
return
}
if err != nil {
APIError{Message: "Internal storage error."}.Log(accountName).Report(w, http.StatusInternalServerError)
return
}
log.WithFields(log.Fields{
"account": accountName,
}).Info("Account created successfully.")
w.WriteHeader(http.StatusCreated)
}