Skip to content

Commit 4fe86e4

Browse files
authored
Add configurable API host and update Swagger documentation (#96)
### TL;DR Added configurable API host for Swagger documentation and improved API configuration. ### What changed? - Introduced a new `APIConfig` struct in `configs/config.go` with a `Host` field. - Added a new command-line flag `--api-host` in `cmd/root.go` with a default value of "localhost:3000". - Updated `cmd/api.go` to set the Swagger host dynamically using the configured API host. - Removed the hardcoded host from `docs/docs.go`, `docs/swagger.json`, and `docs/swagger.yaml`. ### How to test? 1. Run the API with a custom host: ``` ./indexer api --api-host example.com:8080 ``` 2. Check the Swagger documentation to ensure the host is set correctly. ### Why make this change? This change allows for greater flexibility in deploying the API to different environments. By making the API host configurable, it becomes easier to adapt the service to various deployment scenarios without modifying the code. This is particularly useful for staging, production, and custom deployment setups.
2 parents 8bbafd0 + 0f54f0d commit 4fe86e4

File tree

6 files changed

+12
-5
lines changed

6 files changed

+12
-5
lines changed

cmd/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
"github.com/thirdweb-dev/indexer/internal/middleware"
1313

1414
// Import the generated Swagger docs
15-
_ "github.com/thirdweb-dev/indexer/docs"
15+
config "github.com/thirdweb-dev/indexer/configs"
16+
"github.com/thirdweb-dev/indexer/docs"
1617
)
1718

1819
var (
@@ -31,11 +32,12 @@ var (
3132
// @description API for querying blockchain transactions and events
3233
// @license.name Apache 2.0
3334
// @license.url https://github.com/thirdweb-dev/indexer/blob/main/LICENSE
34-
// @host localhost:3000
3535
// @BasePath /
3636
// @Security BasicAuth
3737
// @securityDefinitions.basic BasicAuth
3838
func RunApi(cmd *cobra.Command, args []string) {
39+
docs.SwaggerInfo.Host = config.Cfg.API.Host
40+
3941
r := gin.New()
4042
r.Use(gin.Logger())
4143
r.Use(gin.Recovery())

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func init() {
7878
rootCmd.PersistentFlags().String("storage-main-clickhouse-password", "", "Clickhouse password for main storage")
7979
rootCmd.PersistentFlags().String("storage-staging-clickhouse-username", "", "Clickhouse username for staging storage")
8080
rootCmd.PersistentFlags().String("storage-staging-clickhouse-password", "", "Clickhouse password for staging storage")
81+
rootCmd.PersistentFlags().String("api-host", "localhost:3000", "API host")
8182
viper.BindPFlag("rpc.url", rootCmd.PersistentFlags().Lookup("rpc-url"))
8283
viper.BindPFlag("rpc.blocks.blocksPerRequest", rootCmd.PersistentFlags().Lookup("rpc-blocks-blocksPerRequest"))
8384
viper.BindPFlag("rpc.blocks.batchDelay", rootCmd.PersistentFlags().Lookup("rpc-blocks-batchDelay"))
@@ -120,6 +121,7 @@ func init() {
120121
viper.BindPFlag("storage.orchestrator.redis.addr", rootCmd.PersistentFlags().Lookup("storage-orchestrator-redis-addr"))
121122
viper.BindPFlag("storage.orchestrator.redis.password", rootCmd.PersistentFlags().Lookup("storage-orchestrator-redis-password"))
122123
viper.BindPFlag("storage.orchestrator.redis.db", rootCmd.PersistentFlags().Lookup("storage-orchestrator-redis-db"))
124+
viper.BindPFlag("api.host", rootCmd.PersistentFlags().Lookup("api-host"))
123125
rootCmd.AddCommand(orchestratorCmd)
124126
rootCmd.AddCommand(apiCmd)
125127
}

configs/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,18 @@ type RPCConfig struct {
9191
Traces RPCTracesConfig `mapstructure:"traces"`
9292
}
9393

94+
type APIConfig struct {
95+
Host string `mapstructure:"host"`
96+
}
97+
9498
type Config struct {
9599
RPC RPCConfig `mapstructure:"rpc"`
96100
Log LogConfig `mapstructure:"log"`
97101
Poller PollerConfig `mapstructure:"poller"`
98102
Committer CommitterConfig `mapstructure:"committer"`
99103
FailureRecoverer FailureRecovererConfig `mapstructure:"failureRecoverer"`
100104
Storage StorageConfig `mapstructure:"storage"`
105+
API APIConfig `mapstructure:"api"`
101106
}
102107

103108
var Cfg Config

docs/docs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ const docTemplate = `{
954954
// SwaggerInfo holds exported Swagger Info so clients can modify it
955955
var SwaggerInfo = &swag.Spec{
956956
Version: "v0.0.1-beta",
957-
Host: "localhost:3000",
957+
Host: "",
958958
BasePath: "/",
959959
Schemes: []string{},
960960
Title: "Thirdweb Insight",

docs/swagger.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
},
1111
"version": "v0.0.1-beta"
1212
},
13-
"host": "localhost:3000",
1413
"basePath": "/",
1514
"paths": {
1615
"/{chainId}/events": {

docs/swagger.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ definitions:
121121
value:
122122
type: string
123123
type: object
124-
host: localhost:3000
125124
info:
126125
contact: {}
127126
description: API for querying blockchain transactions and events

0 commit comments

Comments
 (0)