forked from linksmart/service-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (154 loc) · 4.88 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2014-2016 Fraunhofer Institute for Applied Information Technology FIT
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
"time"
"github.com/gorilla/context"
"github.com/justinas/alice"
_ "github.com/linksmart/go-sec/auth/keycloak/validator"
"github.com/linksmart/go-sec/auth/validator"
"github.com/linksmart/service-catalog/v3/catalog"
"github.com/oleksandr/bonjour"
"github.com/rs/cors"
uuid "github.com/satori/go.uuid"
"github.com/urfave/negroni"
)
var (
confPath = flag.String("conf", "conf/service-catalog.json", "Configuration file path")
profile = flag.Int("profile", 0, "Activate runtime profiling HTTP server on the given port")
version = flag.Bool("version", false, "Print the API version")
Version string // set with build flag
BuildNumber string // set with build flag
)
const LINKSMART = `
╦ ╦ ╔╗╔ ╦╔═ ╔═╗ ╔╦╗ ╔═╗ ╦═╗ ╔╦╗
║ ║ ║║║ ╠╩╗ ╚═╗ ║║║ ╠═╣ ╠╦╝ ║
╩═╝ ╩ ╝╚╝ ╩ ╩ ╚═╝ ╩ ╩ ╩ ╩ ╩╚═ ╩
`
func main() {
flag.Parse()
if *version {
fmt.Println(Version)
return
}
fmt.Print(LINKSMART)
logger.Printf("Starting Service Catalog")
if Version != "" {
logger.Printf("Version: %s", Version)
}
if BuildNumber != "" {
logger.Printf("Build Number: %s", BuildNumber)
}
if *profile != 0 {
logger.Println("Activating runtime profiling server on port", *profile)
go func() { logger.Println(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *profile), nil)) }()
}
// Load configuration
config, err := loadConfig(*confPath)
if err != nil {
logger.Fatalf("Error reading config file %v: %s", *confPath, err)
}
if config.ID == "" {
config.ID = uuid.NewV4().String()
}
// Setup storage
var storage catalog.Storage
switch config.Storage.Type {
case catalog.CatalogBackendMemory:
storage = catalog.NewMemoryStorage()
case catalog.CatalogBackendLevelDB:
storage, err = catalog.NewLevelDBStorage(config.Storage.DSN, nil)
if err != nil {
logger.Fatalf("Failed to start LevelDB storage: %s", err)
}
default:
logger.Fatalf("Could not create catalog API storage. Unsupported type: %v", config.Storage.Type)
}
var listeners []catalog.Listener
controller, err := catalog.NewController(storage, listeners...)
if err != nil {
storage.Close()
logger.Fatalf("Failed to start the controller: %s", err)
}
// Create http api
httpAPI := catalog.NewHTTPAPI(controller, config.ID, config.Description, Version)
go serveHTTP(httpAPI, config)
// Create mqtt api
go catalog.StartMQTTManager(controller, config.MQTT, config.ID)
// Announce service using DNS-SD
var bonjourS *bonjour.Server
if config.DNSSDEnabled {
bonjourS, err = bonjour.Register(config.Description, catalog.DNSSDServiceType, "", config.HTTP.BindPort, []string{"uri=/"}, nil)
if err != nil {
logger.Printf("Failed to register DNS-SD service: %s", err)
} else {
logger.Println("Registered service via DNS-SD using type", catalog.DNSSDServiceType)
}
}
// Ctrl+C / Kill handling
handler := make(chan os.Signal, 1)
signal.Notify(handler, os.Interrupt, os.Kill)
<-handler
logger.Println("Shutting down...")
// Stop bonjour registration
if bonjourS != nil {
bonjourS.Shutdown()
time.Sleep(1e9)
}
// Shutdown storage
err = controller.Stop()
if err != nil {
logger.Println(err.Error())
}
logger.Println("Stopped")
}
func serveHTTP(httpAPI *catalog.HttpAPI, config *Config) {
commonHandlers := alice.New(
context.ClearHandler,
loggingHandler,
//commonHeaders,
cors.AllowAll().Handler,
)
// Append auth handler if enabled
if config.Auth.Enabled {
// Setup ticket validator
v, err := validator.Setup(
config.Auth.Provider,
config.Auth.ProviderURL,
config.Auth.ServiceID,
config.Auth.BasicEnabled,
config.Auth.Authz)
if err != nil {
logger.Fatalln(err)
}
commonHandlers = commonHandlers.Append(v.Handler)
}
// Configure http router
r := newRouter()
// generic handlers
r.get("/health", commonHandlers.ThenFunc(healthHandler))
r.options("/{path:.*}", commonHandlers.ThenFunc(optionsHandler))
// service handlers
r.get("/", commonHandlers.ThenFunc(httpAPI.List))
r.post("/", commonHandlers.ThenFunc(httpAPI.Post))
// Accept an id with zero or one slash: [^/]+/?[^/]*
// -> [^/]+ one or more of anything but slashes /? optional slash [^/]* zero or more of anything but slashes
r.get("/{id:[^/]+/?[^/]*}", commonHandlers.ThenFunc(httpAPI.Get))
r.put("/{id:[^/]+/?[^/]*}", commonHandlers.ThenFunc(httpAPI.Put))
r.delete("/{id:[^/]+/?[^/]*}", commonHandlers.ThenFunc(httpAPI.Delete))
r.get("/{path}/{op}/{value:.*}", commonHandlers.ThenFunc(httpAPI.Filter))
// Configure the middleware
n := negroni.New(
negroni.NewRecovery(),
)
// Mount router
n.UseHandler(r)
// Start listener
n.Run(fmt.Sprintf("%s:%s", config.HTTP.BindAddr, strconv.Itoa(config.HTTP.BindPort)))
}