This repository was archived by the owner on Dec 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
53 lines (44 loc) · 1.32 KB
/
api.go
File metadata and controls
53 lines (44 loc) · 1.32 KB
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
package main
import (
"encoding/json"
"net/http"
"os"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
// PontusApiContext Execution context of REST API
type ApiContext struct {
store AppStore
}
type AppState struct {
Name string `json:"name"`
Metadata AppMetadata `json:"metadata"`
IsInstalled bool `json:"isInstalled"`
}
// Serve Start server and register HTTP handlers on given address
func (ctx *ApiContext) Serve(addr string) error {
gui := http.FileServer(&assetfs.AssetFS{
Asset: Asset, AssetDir: AssetDir,
AssetInfo: AssetInfo, Prefix: "gui",
})
router := mux.NewRouter().StrictSlash(true)
router.Path("/api/v1/apps").Methods("GET").HandlerFunc(ctx.apps)
router.PathPrefix("/").Handler(http.StripPrefix("/", gui))
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
return http.ListenAndServe(addr, loggedRouter)
}
// Return all current deployments
func (ctx *ApiContext) apps(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
apps := make([]AppState, 0)
for _, app := range ctx.store.Apps() {
apps = append(apps, AppState{
Name: app.Name,
Metadata: app.Metadata,
IsInstalled: ctx.store.IsInstalled(app.Name),
})
}
encoder := json.NewEncoder(w)
encoder.Encode(apps)
}