forked from scaleshift/scaleshift
/
app.go
61 lines (55 loc) · 1.9 KB
/
app.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 controllers
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/swag"
"github.com/xtream-d-labs/ai-gateway/api/src/config"
"github.com/xtream-d-labs/ai-gateway/api/src/generated/models"
"github.com/xtream-d-labs/ai-gateway/api/src/generated/restapi/operations"
"github.com/xtream-d-labs/ai-gateway/api/src/generated/restapi/operations/app"
)
func appRoute(api *operations.AIGatewayAPI) {
api.AppGetVersionsHandler = app.GetVersionsHandlerFunc(getVersions)
api.AppGetEndpointsHandler = app.GetEndpointsHandlerFunc(getEndpoints)
}
// AppVersion defines application version
type AppVersion struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}
func getVersions(params app.GetVersionsParams) middleware.Responder {
version := swag.String(config.BuildVersion())
date := config.BuildDate()
if resp, err := http.Get("https://s3-ap-northeast-1.amazonaws.com/ai-gateway/latest-version.json"); err == nil {
defer resp.Body.Close()
if bytes, err := ioutil.ReadAll(resp.Body); err == nil {
latest := &AppVersion{}
if err = json.Unmarshal(bytes, latest); err == nil {
version = swag.String(fmt.Sprintf("%s-%s", latest.Version, latest.Commit))
date = latest.Date
}
}
}
return app.NewGetVersionsOK().WithPayload(&models.Versions{
Current: &models.Version{
Version: swag.String(config.BuildVersion()),
BuildDate: config.BuildDate(),
},
Latest: &models.Version{
Version: version,
BuildDate: date,
},
})
}
func getEndpoints(params app.GetEndpointsParams) middleware.Responder {
return app.NewGetEndpointsOK().WithPayload(&models.Endpoints{
DockerRegistry: config.Config.DockerRegistryEndpoint,
NgcRegistry: config.Config.NgcRegistryEndpoint,
KubernetesAPI: config.Config.KubernetesAPIEndpoint,
RescaleAPI: config.Config.RescaleEndpoint,
})
}