/
main.go
68 lines (50 loc) · 2.27 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"net/http"
"github.com/Tinker-Ware/digital-ocean-service/infrastructure"
"github.com/Tinker-Ware/digital-ocean-service/interfaces"
"github.com/Tinker-Ware/digital-ocean-service/usecases"
"github.com/codegangsta/negroni"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
const defaultPath = "/etc/digital-ocean-service.conf"
var confFilePath = flag.String("conf", defaultPath, "Custom path for configuration file")
func main() {
flag.Parse()
config, err := infrastructure.GetConfiguration(*confFilePath)
if err != nil {
fmt.Println(err.Error())
panic("Cannot parse configuration")
}
doInteractor := usecases.DOInteractor{}
handler := interfaces.WebServiceHandler{
Interactor: doInteractor,
ID: config.ClientID,
Secret: config.ClientSecret,
Scopes: config.Scopes,
RedirectURI: config.RedirectURI,
APIHost: config.APIHost,
}
headers := handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Authorization"})
origins := handlers.AllowedOrigins([]string{"http://localhost", "http://provision.tinkerware.io", "https://provision.tinkerware.io", "http://mydevop.tinkerware.io", "https://mydevop.tinkerware.io"})
r := mux.NewRouter()
subrouter := r.PathPrefix("/api/v1/cloud").Subrouter()
subrouter.HandleFunc("/digital_ocean/", handler.Login)
subrouter.HandleFunc("/digital_ocean/oauth", handler.DOCallback).Methods("POST")
subrouter.HandleFunc("/digital_ocean/keys", handler.ShowKeys).Methods("GET")
subrouter.Handle("/digital_ocean/keys", interfaces.Adapt(http.HandlerFunc(handler.CreateKey), interfaces.GetToken(config.APIHost, config.Salt))).Methods("POST")
subrouter.HandleFunc("/digital_ocean/instances", handler.CreateDroplet).Methods("POST")
subrouter.HandleFunc("/digital_ocean/instances", handler.ListDroplets).Methods("GET")
subrouter.HandleFunc("/digital_ocean/instance/{instanceID}", handler.GetInstance).Methods("GET")
subrouter.Handle("/digital_ocean/instance/{instanceID}", interfaces.Adapt(http.HandlerFunc(handler.DestroyDroplet), interfaces.GetToken(config.APIHost, config.Salt))).Methods("DELETE")
n := negroni.Classic()
n.UseHandler(handlers.CORS(headers, origins)(r))
port := bytes.Buffer{}
port.WriteString(":")
port.WriteString(config.Port)
n.Run(port.String())
}