/
main.go
99 lines (84 loc) · 3.46 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
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
//
// Copyright (c) 2021 Cisco Systems, Inc and its affiliates
// All Rights reserved
//
package main
import (
"github.com/CiscoDevNet/msx-examples/go-hello-world-service-7/internal/config"
"github.com/CiscoDevNet/msx-examples/go-hello-world-service-7/internal/consul"
"github.com/CiscoDevNet/msx-examples/go-hello-world-service-7/internal/datastore"
"github.com/CiscoDevNet/msx-examples/go-hello-world-service-7/internal/security"
"github.com/CiscoDevNet/msx-examples/go-hello-world-service-7/internal/swagger"
"github.com/CiscoDevNet/msx-examples/go-hello-world-service-7/internal/vault"
"log"
"net/http"
openapi "github.com/CiscoDevNet/msx-examples/go-hello-world-service-7/go"
)
func main() {
// Read the configuration.
config := config.ReadConfig()
log.Printf("Server started")
// Setup Consul.
consul, err := consul.NewConsul(config)
if err != nil {
log.Printf("Could not initialize Consul: %s", err.Error())
}
config.Consul.Prefix = consul.FindPrefix()
config.Vault.Prefix = "secret/" + config.Consul.Prefix
testConsul(config, &consul)
// Setup Vault.
vault, err := vault.NewVault(config)
if err != nil {
log.Printf("Could not initialize Vault: %s", err.Error())
}
testVault(config, &vault)
// Setup CockroachDB
datastore.UpdateConfig(config, &consul, &vault)
db, err := datastore.NewCockroachDB(config)
if err != nil {
log.Fatal("FATAL: Could not connect to DB: %s", err.Error())
}
err = db.BuildSchema()
if err != nil {
log.Fatal("FATAL: Could not build DB schema: %s", err.Error())
}
// Setup Swagger.
swagger.UpdateConfig(config, &consul, &vault)
swagger, err := swagger.NewSwagger(config)
if err != nil {
log.Fatalf("Could not setup Swagger: %s", err.Error())
}
// Setup Security.
security.UpdateConfig(config, &consul, &vault)
err = security.NewSecurity(config)
if err != nil {
log.Fatalf("Could not setup Security: %s", err.Error())
}
// Setup Controllers
ItemsApiController := openapi.NewItemsApiController(db)
LanguagesApiController := openapi.NewLanguagesApiController(db)
// Add insecure routes for Items.
router := openapi.NewRouter(ItemsApiController)
// Add secure routes for Languages.
secureRouter := security.AddSecureRoutes(router, LanguagesApiController)
// Add route for Swagger.
router.PathPrefix("/helloworld/swagger").HandlerFunc(swagger.SwaggerRoutes)
log.Fatal(http.ListenAndServe(":8080", secureRouter))
}
func testConsul(config *config.Config, consul *consul.HelloWorldConsul) {
// Read our favourites from Consul and print them to the console.
// Do not leak config in production as it is a security violation.
favouriteColor, _:= consul.GetString(config.Consul.Prefix + "/helloworldservice/favourite.color", "UNKNOWN")
log.Printf("My favourite color is %s.", favouriteColor)
favouriteFood, _ := consul.GetString(config.Consul.Prefix + "/helloworldservice/favourite.food", "UNKNOWN")
log.Printf("My favourite food is %s.", favouriteFood)
favouriteDinosaur, _ := consul.GetString(config.Consul.Prefix + "/helloworldservice/favourite.dinosaur", "UNKNOWN")
log.Printf("My favourite dinosaur is %s.", favouriteDinosaur)
}
func testVault(config *config.Config, vault *vault.HelloWorldVault) {
// Read a secret from Vault and it to the console.
// Do not leak secrets in production as it is a security violation.
secretSquirrelLocation, _ := vault.GetString(config.Vault.Prefix + "/helloworldservice/", "secret.squirrel.location", "UNKNOWN")
log.Printf("Where are the acorns buried?")
log.Print(secretSquirrelLocation)
}