Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activate dynamic endpoint for swagger API dashboard #1844

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ services:
- ./container-volume/cb-tumblebug-container/log/:/app/log/
environment:
# - TB_ROOT_PATH=/app
# # Enable TB_SELF_ENDPOINT to specify an endpoint for CB-TB API (default: localhost:1323)
# # Use public IP if you want to access the API Dashboard from outside of localhost
# - TB_SELF_ENDPOINT=xxx.xxx.xxx.xxx:1323
- TB_SPIDER_REST_URL=http://cb-spider:1024/spider
- TB_ETCD_ENDPOINTS=http://cb-tumblebug-etcd:2379
# - TB_ETCD_AUTH_ENABLED=true
Expand All @@ -42,7 +45,6 @@ services:
# - TB_API_USERNAME=default
# - TB_API_PASSWORD=default
# - TB_AUTOCONTROL_DURATION_MS=10000
# - TB_SELF_ENDPOINT=localhost:1323
# - TB_DRAGONFLY_REST_URL=http://cb-dragonfly:9090/dragonfly
# - TB_DEFAULT_NAMESPACE=default
# - TB_DEFAULT_CREDENTIALHOLDER=admin
Expand Down Expand Up @@ -161,28 +163,29 @@ services:
retries: 3
start_period: 10s

# Swagger UI
swagger-ui:
image: swaggerapi/swagger-ui
container_name: swagger-ui
networks:
- external_network
ports:
- 1325:8080
volumes:
# cb-tumblebug swagger.yaml mount
- ./src/api/rest/docs/swagger.yaml:/swagger.yaml
environment:
# Options: https://github.com/swagger-api/swagger-ui/blob/37b8c1a8b67200dd425216ab8f97b725a429a5c0/docs/usage/configuration.md#docker
- SWAGGER_JSON=/swagger.yaml
logging:
# Disable logging
driver: "none"
healthcheck:
test: [ "CMD", "curl", "-f", "localhost", "1325"]
timeout: 5s
retries: 3
start_period: 3s
# # Swagger UI
# swagger-ui:
# image: swaggerapi/swagger-ui
# container_name: swagger-ui
# networks:
# - external_network
# ports:
# - 1325:8080
# volumes:
# # cb-tumblebug swagger.yaml mount
# - ./src/api/rest/docs/swagger.yaml:/swagger.yaml
# environment:
# # Options: https://github.com/swagger-api/swagger-ui/blob/37b8c1a8b67200dd425216ab8f97b725a429a5c0/docs/usage/configuration.md#docker
# - SWAGGER_JSON=/swagger.yaml
# - QUERY_CONFIG_ENABLED=true
# logging:
# # Disable logging
# driver: "none"
# healthcheck:
# test: [ "CMD", "curl", "-f", "localhost", "1325"]
# timeout: 5s
# retries: 3
# start_period: 3s

# # cb-tumblebug-etcd-conf
# cb-tumblebug-etcd-conf:
Expand Down
2 changes: 1 addition & 1 deletion src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14724,7 +14724,7 @@ const docTemplate = `{
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "latest",
Host: "localhost:1323",
Host: "",
BasePath: "/tumblebug",
Schemes: []string{},
Title: "CB-Tumblebug REST API",
Expand Down
1 change: 0 additions & 1 deletion src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
},
"version": "latest"
},
"host": "localhost:1323",
"basePath": "/tumblebug",
"paths": {
"/auth/test": {
Expand Down
2 changes: 1 addition & 1 deletion src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ info:
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: latest
servers:
- url: //localhost:1323/tumblebug
- url: /tumblebug
paths:
/auth/test:
get:
Expand Down
20 changes: 13 additions & 7 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

// "log"
"os/signal"
"strings"
"sync"
"syscall"
"time"
Expand All @@ -28,6 +29,7 @@ import (

"github.com/rs/zerolog/log"

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

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

// RunServer func start Rest API server
func RunServer(port string) {
func RunServer() {

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

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

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

selfEndpoint := os.Getenv("TB_SELF_ENDPOINT")
apiServer := "http://" + selfEndpoint + "/tumblebug/readyz"
apiDashboard := "http://localhost:1325"
mapUI := "http://localhost:1324"
selfEndpoint := strings.Split(model.SelfEndpoint, ":")
selfIp := selfEndpoint[0]
selfPort := selfEndpoint[1]

apiServer := fmt.Sprintf("http://%s:%s/tumblebug/readyz", selfIp, selfPort)
//apiDashboard := fmt.Sprintf("http://%s:%s", selfIp, "1325")
apiDashboard := fmt.Sprintf("http://%s:%s/tumblebug/api", selfIp, selfPort)
mapUI := fmt.Sprintf("http://%s:%s", selfIp, "1324")

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

port = fmt.Sprintf(":%s", port)
model.SystemReady = true
if err := e.Start(port); err != nil && err != http.ErrServerClosed {
if err := e.Start(":" + selfPort); err != nil && err != http.ErrServerClosed {
log.Error().Err(err).Msg("Error in Starting CB-Tumblebug API Server")
e.Logger.Panic("Shuttig down the server: ", err)
}
Expand Down
1 change: 1 addition & 0 deletions src/core/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var AutocontrolDurationMs string
var DefaultNamespace string
var DefaultCredentialHolder string
var EtcdEndpoints string
var SelfEndpoint string
var MyDB *sql.DB
var err error
var ORM *xorm.Engine
Expand Down
29 changes: 3 additions & 26 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bufio"
"context"
"encoding/csv"
"flag"
"fmt"
"os"
"os/user"
Expand Down Expand Up @@ -51,6 +50,7 @@ import (
func init() {
model.SystemReady = false

model.SelfEndpoint = common.NVL(os.Getenv("TB_SELF_ENDPOINT"), "localhost:1323")
model.SpiderRestUrl = common.NVL(os.Getenv("TB_SPIDER_REST_URL"), "http://localhost:1024/spider")
model.DragonflyRestUrl = common.NVL(os.Getenv("TB_DRAGONFLY_REST_URL"), "http://localhost:9090/dragonfly")
model.TerrariumRestUrl = common.NVL(os.Getenv("TB_TERRARIUM_REST_URL"), "http://localhost:8888/terrarium")
Expand All @@ -61,6 +61,7 @@ func init() {
model.AutocontrolDurationMs = common.NVL(os.Getenv("TB_AUTOCONTROL_DURATION_MS"), "10000")
model.DefaultNamespace = common.NVL(os.Getenv("TB_DEFAULT_NAMESPACE"), "default")
model.DefaultCredentialHolder = common.NVL(os.Getenv("TB_DEFAULT_CREDENTIALHOLDER"), "admin")

// Etcd
model.EtcdEndpoints = common.NVL(os.Getenv("TB_ETCD_ENDPOINTS"), "localhost:2379")

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

// @host localhost:1323
// @BasePath /tumblebug

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

// giving a default value of "1323"
port := flag.String("port", "1323", "port number for the restapiserver to listen to")
flag.Parse()

// validate arguments from flag
validationFlag := true
// validation: port
// set validationFlag to false if your number is not in [1-65535] range
if portInt, err := strconv.Atoi(*port); err == nil {
if portInt < 1 || portInt > 65535 {
validationFlag = false
}
} else {
validationFlag = false
}
if !validationFlag {
fmt.Printf("%s is not a valid port number.\n", *port)
fmt.Printf("Please retry with a valid port number (ex: -port=[1-65535]).\n")
os.Exit(1)
}

//Ticker for MCI Orchestration Policy

log.Info().Msg("[Initiate Multi-Cloud Orchestration]")

autoControlDuration, _ := strconv.Atoi(model.AutocontrolDurationMs) //ms
ticker := time.NewTicker(time.Millisecond * time.Duration(autoControlDuration))
go func() {
Expand Down Expand Up @@ -483,7 +460,7 @@ func main() {

// Start REST Server
go func() {
restServer.RunServer(*port)
restServer.RunServer()
wg.Done()
}()

Expand Down