From 31eaf0ad3fdce283652fb39656b9e297c5927a0e Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Wed, 14 Feb 2024 01:47:51 +0200 Subject: [PATCH] Added support for using cosmosdb-explorer --- api/config/config.go | 29 +++++++++++++++++++++++++---- api/config/models.go | 6 ++++++ api/handlers/explorer.go | 29 +++++++++++++++++++++++++++++ api/router.go | 2 ++ main.go | 8 ++++++-- 5 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 api/handlers/explorer.go diff --git a/api/config/config.go b/api/config/config.go index 2611105..52a9917 100644 --- a/api/config/config.go +++ b/api/config/config.go @@ -1,7 +1,28 @@ package config -var Config = ServerConfig{ - DatabaseAccount: "localhost", - DatabaseDomain: "localhost", - DatabaseEndpoint: "https://localhost:8081/", +import ( + "flag" + "fmt" +) + +var Config = ServerConfig{} + +func ParseFlags() { + host := flag.String("Host", "localhost", "Hostname") + port := flag.Int("Port", 8081, "Listen port") + explorerPath := flag.String("ExplorerDir", "/home/pk/pro/cosmos-explorer/dist", "Path to cosmos-explorer files") + tlsCertificatePath := flag.String("Cert", "../example.crt", "Hostname") + tlsCertificateKey := flag.String("CertKey", "../example.key", "Hostname") + + flag.Parse() + + Config.Host = *host + Config.Port = *port + Config.ExplorerPath = *explorerPath + Config.TLS_CertificatePath = *tlsCertificatePath + Config.TLS_CertificateKey = *tlsCertificateKey + + Config.DatabaseAccount = Config.Host + Config.DatabaseDomain = Config.Host + Config.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", Config.Host, Config.Port) } diff --git a/api/config/models.go b/api/config/models.go index bdbcb0c..e7b98f1 100644 --- a/api/config/models.go +++ b/api/config/models.go @@ -4,4 +4,10 @@ type ServerConfig struct { DatabaseAccount string DatabaseDomain string DatabaseEndpoint string + + ExplorerPath string + Port int + Host string + TLS_CertificatePath string + TLS_CertificateKey string } diff --git a/api/handlers/explorer.go b/api/handlers/explorer.go new file mode 100644 index 0000000..1b18376 --- /dev/null +++ b/api/handlers/explorer.go @@ -0,0 +1,29 @@ +package handlers + +import ( + "fmt" + + "github.com/gin-gonic/gin" + "github.com/pikami/cosmium/api/config" +) + +func RegisterExplorerHandlers(router *gin.Engine) { + explorer := router.Group("/_explorer") + { + explorer.Use(func(ctx *gin.Context) { + if ctx.Param("filepath") == "/config.json" { + endpoint := fmt.Sprintf("https://%s:%d", config.Config.Host, config.Config.Port) + ctx.JSON(200, gin.H{ + "BACKEND_ENDPOINT": endpoint, + "MONGO_BACKEND_ENDPOINT": endpoint, + "PROXY_PATH": "/", + "EMULATOR_ENDPOINT": endpoint, + }) + ctx.Abort() + } else { + ctx.Next() + } + }) + explorer.Static("/", config.Config.ExplorerPath) + } +} diff --git a/api/router.go b/api/router.go index 14ab16c..9dea0a6 100644 --- a/api/router.go +++ b/api/router.go @@ -35,5 +35,7 @@ func CreateRouter() *gin.Engine { router.GET("/offers", handlers.GetOffers) router.GET("/", handlers.GetServerInfo) + handlers.RegisterExplorerHandlers(router) + return router } diff --git a/main.go b/main.go index 266ca83..6669780 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,15 @@ import ( "fmt" "github.com/pikami/cosmium/api" + "github.com/pikami/cosmium/api/config" ) func main() { - fmt.Println("Hello world") + config.ParseFlags() router := api.CreateRouter() - router.RunTLS(":8081", "../example.crt", "../example.key") + router.RunTLS( + fmt.Sprintf(":%d", config.Config.Port), + config.Config.TLS_CertificatePath, + config.Config.TLS_CertificateKey) }