forked from siderolabs/devise
/
main.go
67 lines (55 loc) · 1.31 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/autonomy/devise/api"
"golang.org/x/net/context"
"golang.org/x/net/http2"
"google.golang.org/grpc"
)
const (
address = "localhost:50000"
port = ":8081"
)
type app struct {
config []byte
}
type response map[string]interface{}
func main() {
app, err := newApp()
if err != nil {
log.Fatalf("Failed to create app: %v", err)
}
var srv http.Server
http2.VerboseLogs = true
srv.Addr = port
// This enables http2 support
http2.ConfigureServer(&srv, nil)
http.HandleFunc("/config", app.templateHandler)
log.Fatal(srv.ListenAndServe())
}
func (app *app) templateHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response{"config": string(app.config)})
}
func newApp() (*app, error) {
b, err := ioutil.ReadFile("example.yaml")
if err != nil {
log.Printf("%v", err)
}
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return nil, err
}
defer conn.Close()
c := api.NewDeviseClient(conn)
// Contact the server and get the rendered plan.
r, err := c.Template(context.Background(), &api.TemplateRequest{Template: b})
if err != nil {
return nil, err
}
return &app{config: r.Rendered}, nil
}