/
api.go
135 lines (125 loc) · 3.85 KB
/
api.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
// Copyright 2015 diaats authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/gorilla/pat"
)
func handler(fn http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if config.Username != "" && config.Password != "" {
if username, password, ok := r.BasicAuth(); !ok || username != config.Username || password != config.Password {
w.Header().Add("WWW-Authenticate", `Basic realm="diaats"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
}
fn.ServeHTTP(w, r)
})
}
func createInstance(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
if name == "" {
http.Error(w, "please provide the name of the instance", http.StatusBadRequest)
return
}
planName := r.FormValue("plan")
if planName == "" {
http.Error(w, "please provide the name of the plan", http.StatusBadRequest)
return
}
plan, err := getPlan(planName)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = CreateInstance(name, plan)
if err != nil {
status := http.StatusInternalServerError
if err == ErrInstanceAlreadyExists {
status = http.StatusConflict
}
http.Error(w, err.Error(), status)
return
}
w.WriteHeader(http.StatusCreated)
}
func bindApp(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
instance, err := GetInstance(name)
if err != nil {
status := http.StatusInternalServerError
if err == ErrInstanceNotFound {
status = http.StatusNotFound
}
http.Error(w, err.Error(), status)
return
}
encodedEndpoints, _ := json.Marshal(instance.Endpoints())
encodedEnvs, _ := json.Marshal(instance.EnvMap())
envVarName := fmt.Sprintf("DIAATS_%s_INSTANCE", strings.ToUpper(instance.Plan.Name))
dockerEnvVarName := fmt.Sprintf("DIAATS_%s_DOCKER_ENVS", strings.ToUpper(instance.Plan.Name))
data := map[string]string{
envVarName: string(encodedEndpoints),
dockerEnvVarName: string(encodedEnvs),
}
err = json.NewEncoder(w).Encode(data)
if err != nil {
log.Printf("ERROR - failed to encode JSON: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func removeInstance(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
err := DestroyInstance(name)
if err != nil {
status := http.StatusInternalServerError
if err == ErrInstanceNotFound {
status = http.StatusNotFound
}
http.Error(w, err.Error(), status)
return
}
}
func instanceStatus(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
_, err := GetInstance(name)
if err != nil {
status := http.StatusInternalServerError
if err == ErrInstanceNotFound {
status = http.StatusNotFound
}
http.Error(w, err.Error(), status)
return
}
w.WriteHeader(http.StatusNoContent)
}
func listPlans(w http.ResponseWriter, r *http.Request) {
result := make([]map[string]string, len(config.Plans))
for i, plan := range config.Plans {
result[i] = plan.ToMap()
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func buildMuxer() http.Handler {
m := pat.New()
m.Post("/resources/{name}/bind-app", handler(bindApp))
m.Delete("/resources/{name}/bind-app", handler(func(http.ResponseWriter, *http.Request) {}))
m.Post("/resources/{name}/bind", handler(func(http.ResponseWriter, *http.Request) {}))
m.Delete("/resources/{name}/bind", handler(func(http.ResponseWriter, *http.Request) {}))
m.Get("/resources/{name}/status", handler(instanceStatus))
m.Delete("/resources/{name}", handler(removeInstance))
m.Get("/resources/plans", handler(listPlans))
m.Post("/resources", handler(createInstance))
return m
}