Skip to content

Commit a61e5a9

Browse files
Activate dynamic endpoint for swagger API dashboard
2 parents 07e7c75 + a5cc432 commit a61e5a9

File tree

7 files changed

+45
-59
lines changed

7 files changed

+45
-59
lines changed

docker-compose.yaml

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ services:
2828
- ./container-volume/cb-tumblebug-container/log/:/app/log/
2929
environment:
3030
# - TB_ROOT_PATH=/app
31+
# # Enable TB_SELF_ENDPOINT to specify an endpoint for CB-TB API (default: localhost:1323)
32+
# # Use public IP if you want to access the API Dashboard from outside of localhost
33+
# - TB_SELF_ENDPOINT=xxx.xxx.xxx.xxx:1323
3134
- TB_SPIDER_REST_URL=http://cb-spider:1024/spider
3235
- TB_ETCD_ENDPOINTS=http://cb-tumblebug-etcd:2379
3336
# - TB_ETCD_AUTH_ENABLED=true
@@ -42,7 +45,6 @@ services:
4245
# - TB_API_USERNAME=default
4346
# - TB_API_PASSWORD=default
4447
# - TB_AUTOCONTROL_DURATION_MS=10000
45-
# - TB_SELF_ENDPOINT=localhost:1323
4648
# - TB_DRAGONFLY_REST_URL=http://cb-dragonfly:9090/dragonfly
4749
# - TB_DEFAULT_NAMESPACE=default
4850
# - TB_DEFAULT_CREDENTIALHOLDER=admin
@@ -161,28 +163,29 @@ services:
161163
retries: 3
162164
start_period: 10s
163165

164-
# Swagger UI
165-
swagger-ui:
166-
image: swaggerapi/swagger-ui
167-
container_name: swagger-ui
168-
networks:
169-
- external_network
170-
ports:
171-
- 1325:8080
172-
volumes:
173-
# cb-tumblebug swagger.yaml mount
174-
- ./src/api/rest/docs/swagger.yaml:/swagger.yaml
175-
environment:
176-
# Options: https://github.com/swagger-api/swagger-ui/blob/37b8c1a8b67200dd425216ab8f97b725a429a5c0/docs/usage/configuration.md#docker
177-
- SWAGGER_JSON=/swagger.yaml
178-
logging:
179-
# Disable logging
180-
driver: "none"
181-
healthcheck:
182-
test: [ "CMD", "curl", "-f", "localhost", "1325"]
183-
timeout: 5s
184-
retries: 3
185-
start_period: 3s
166+
# # Swagger UI
167+
# swagger-ui:
168+
# image: swaggerapi/swagger-ui
169+
# container_name: swagger-ui
170+
# networks:
171+
# - external_network
172+
# ports:
173+
# - 1325:8080
174+
# volumes:
175+
# # cb-tumblebug swagger.yaml mount
176+
# - ./src/api/rest/docs/swagger.yaml:/swagger.yaml
177+
# environment:
178+
# # Options: https://github.com/swagger-api/swagger-ui/blob/37b8c1a8b67200dd425216ab8f97b725a429a5c0/docs/usage/configuration.md#docker
179+
# - SWAGGER_JSON=/swagger.yaml
180+
# - QUERY_CONFIG_ENABLED=true
181+
# logging:
182+
# # Disable logging
183+
# driver: "none"
184+
# healthcheck:
185+
# test: [ "CMD", "curl", "-f", "localhost", "1325"]
186+
# timeout: 5s
187+
# retries: 3
188+
# start_period: 3s
186189

187190
# # cb-tumblebug-etcd-conf
188191
# cb-tumblebug-etcd-conf:

src/api/rest/docs/docs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14724,7 +14724,7 @@ const docTemplate = `{
1472414724
// SwaggerInfo holds exported Swagger Info so clients can modify it
1472514725
var SwaggerInfo = &swag.Spec{
1472614726
Version: "latest",
14727-
Host: "localhost:1323",
14727+
Host: "",
1472814728
BasePath: "/tumblebug",
1472914729
Schemes: []string{},
1473014730
Title: "CB-Tumblebug REST API",

src/api/rest/docs/swagger.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
},
1515
"version": "latest"
1616
},
17-
"host": "localhost:1323",
1817
"basePath": "/tumblebug",
1918
"paths": {
2019
"/auth/test": {

src/api/rest/docs/swagger.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ info:
1212
url: http://www.apache.org/licenses/LICENSE-2.0.html
1313
version: latest
1414
servers:
15-
- url: //localhost:1323/tumblebug
15+
- url: /tumblebug
1616
paths:
1717
/auth/test:
1818
get:

src/api/rest/server/server.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
// "log"
2121
"os/signal"
22+
"strings"
2223
"sync"
2324
"syscall"
2425
"time"
@@ -28,6 +29,7 @@ import (
2829

2930
"github.com/rs/zerolog/log"
3031

32+
"github.com/cloud-barista/cb-tumblebug/src/api/rest/docs"
3133
"github.com/cloud-barista/cb-tumblebug/src/api/rest/server/auth"
3234

3335
rest_common "github.com/cloud-barista/cb-tumblebug/src/api/rest/server/common"
@@ -88,7 +90,7 @@ const (
8890
)
8991

9092
// RunServer func start Rest API server
91-
func RunServer(port string) {
93+
func RunServer() {
9294

9395
log.Info().Msg("REST API Server is starting")
9496

@@ -119,6 +121,7 @@ func RunServer(port string) {
119121
//e.colorer.Printf(banner, e.colorer.Red("v"+Version), e.colorer.Blue(website))
120122

121123
// Route for system management
124+
docs.SwaggerInfo.Host = model.SelfEndpoint
122125
swaggerRedirect := func(c echo.Context) error {
123126
return c.Redirect(http.StatusMovedPermanently, "/tumblebug/api/index.html")
124127
}
@@ -515,10 +518,14 @@ func RunServer(port string) {
515518
g.PUT("/:nsId/testDeleteObjectAssociation/:resourceType/:resourceId", rest_resource.RestTestDeleteObjectAssociation)
516519
g.GET("/:nsId/testGetAssociatedObjectCount/:resourceType/:resourceId", rest_resource.RestTestGetAssociatedObjectCount)
517520

518-
selfEndpoint := os.Getenv("TB_SELF_ENDPOINT")
519-
apiServer := "http://" + selfEndpoint + "/tumblebug/readyz"
520-
apiDashboard := "http://localhost:1325"
521-
mapUI := "http://localhost:1324"
521+
selfEndpoint := strings.Split(model.SelfEndpoint, ":")
522+
selfIp := selfEndpoint[0]
523+
selfPort := selfEndpoint[1]
524+
525+
apiServer := fmt.Sprintf("http://%s:%s/tumblebug/readyz", selfIp, selfPort)
526+
//apiDashboard := fmt.Sprintf("http://%s:%s", selfIp, "1325")
527+
apiDashboard := fmt.Sprintf("http://%s:%s/tumblebug/api", selfIp, selfPort)
528+
mapUI := fmt.Sprintf("http://%s:%s", selfIp, "1324")
522529

523530
fmt.Print(resetColor)
524531
fmt.Printf(" Default Namespace: %s%s%s\n", warningColor, model.DefaultNamespace, resetColor)
@@ -565,9 +572,8 @@ func RunServer(port string) {
565572
}
566573
}(&wg)
567574

568-
port = fmt.Sprintf(":%s", port)
569575
model.SystemReady = true
570-
if err := e.Start(port); err != nil && err != http.ErrServerClosed {
576+
if err := e.Start(":" + selfPort); err != nil && err != http.ErrServerClosed {
571577
log.Error().Err(err).Msg("Error in Starting CB-Tumblebug API Server")
572578
e.Logger.Panic("Shuttig down the server: ", err)
573579
}

src/core/model/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ var AutocontrolDurationMs string
7373
var DefaultNamespace string
7474
var DefaultCredentialHolder string
7575
var EtcdEndpoints string
76+
var SelfEndpoint string
7677
var MyDB *sql.DB
7778
var err error
7879
var ORM *xorm.Engine

src/main.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bufio"
1919
"context"
2020
"encoding/csv"
21-
"flag"
2221
"fmt"
2322
"os"
2423
"os/user"
@@ -51,6 +50,7 @@ import (
5150
func init() {
5251
model.SystemReady = false
5352

53+
model.SelfEndpoint = common.NVL(os.Getenv("TB_SELF_ENDPOINT"), "localhost:1323")
5454
model.SpiderRestUrl = common.NVL(os.Getenv("TB_SPIDER_REST_URL"), "http://localhost:1024/spider")
5555
model.DragonflyRestUrl = common.NVL(os.Getenv("TB_DRAGONFLY_REST_URL"), "http://localhost:9090/dragonfly")
5656
model.TerrariumRestUrl = common.NVL(os.Getenv("TB_TERRARIUM_REST_URL"), "http://localhost:8888/terrarium")
@@ -61,6 +61,7 @@ func init() {
6161
model.AutocontrolDurationMs = common.NVL(os.Getenv("TB_AUTOCONTROL_DURATION_MS"), "10000")
6262
model.DefaultNamespace = common.NVL(os.Getenv("TB_DEFAULT_NAMESPACE"), "default")
6363
model.DefaultCredentialHolder = common.NVL(os.Getenv("TB_DEFAULT_CREDENTIALHOLDER"), "admin")
64+
6465
// Etcd
6566
model.EtcdEndpoints = common.NVL(os.Getenv("TB_ETCD_ENDPOINTS"), "localhost:2379")
6667

@@ -412,7 +413,6 @@ func addIndexes() error {
412413
// @license.name Apache 2.0
413414
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
414415

415-
// @host localhost:1323
416416
// @BasePath /tumblebug
417417

418418
// @securityDefinitions.basic BasicAuth
@@ -423,31 +423,8 @@ func addIndexes() error {
423423
// @description Type "Bearer" followed by a space and JWT token ([TBD] Get token in http://xxx.xxx.xxx.xxx:xxx/auth)
424424
func main() {
425425

426-
// giving a default value of "1323"
427-
port := flag.String("port", "1323", "port number for the restapiserver to listen to")
428-
flag.Parse()
429-
430-
// validate arguments from flag
431-
validationFlag := true
432-
// validation: port
433-
// set validationFlag to false if your number is not in [1-65535] range
434-
if portInt, err := strconv.Atoi(*port); err == nil {
435-
if portInt < 1 || portInt > 65535 {
436-
validationFlag = false
437-
}
438-
} else {
439-
validationFlag = false
440-
}
441-
if !validationFlag {
442-
fmt.Printf("%s is not a valid port number.\n", *port)
443-
fmt.Printf("Please retry with a valid port number (ex: -port=[1-65535]).\n")
444-
os.Exit(1)
445-
}
446-
447426
//Ticker for MCI Orchestration Policy
448-
449427
log.Info().Msg("[Initiate Multi-Cloud Orchestration]")
450-
451428
autoControlDuration, _ := strconv.Atoi(model.AutocontrolDurationMs) //ms
452429
ticker := time.NewTicker(time.Millisecond * time.Duration(autoControlDuration))
453430
go func() {
@@ -483,7 +460,7 @@ func main() {
483460

484461
// Start REST Server
485462
go func() {
486-
restServer.RunServer(*port)
463+
restServer.RunServer()
487464
wg.Done()
488465
}()
489466

0 commit comments

Comments
 (0)