Skip to content

Commit

Permalink
Added version route and version command line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mboben committed Nov 7, 2024
1 parent e73c643 commit fe0dd15
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
9 changes: 6 additions & 3 deletions indexer/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type IndexerContext interface {
}

type IndexerFlags struct {
Version bool

ConfigFileName string

// Set start epoch for voting cronjob to this value, overrides config and database value,
Expand All @@ -33,8 +35,7 @@ type indexerContext struct {
flags *IndexerFlags
}

func BuildContext() (IndexerContext, error) {
flags := parseIndexerFlags()
func BuildContext(flags *IndexerFlags) (IndexerContext, error) {
cfg, err := config.BuildConfig(flags.ConfigFileName)
if err != nil {
return nil, err
Expand All @@ -59,13 +60,15 @@ func (c *indexerContext) DB() *gorm.DB { return c.db }

func (c *indexerContext) Flags() *IndexerFlags { return c.flags }

func parseIndexerFlags() *IndexerFlags {
func ParseIndexerFlags() *IndexerFlags {
cfgFlag := flag.String("config", globalConfig.CONFIG_FILE, "Configuration file (toml format)")
versionFlag := flag.Bool("version", false, "Print version information and exit")
resetVotingFlag := flag.Int64("reset-voting", 0, "Set start epoch for voting cronjob to this value, overrides config and database value, valid values are > 0")
resetMirrorFlag := flag.Int64("reset-mirroring", 0, "Set start epoch for mirroring cronjob to this value, overrides config and database value, valid values are > 0")
flag.Parse()

return &IndexerFlags{
Version: *versionFlag,
ConfigFileName: *cfgFlag,
ResetVotingCronjob: *resetVotingFlag,
ResetMirrorCronjob: *resetMirrorFlag,
Expand Down
9 changes: 8 additions & 1 deletion indexer/main/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ import (
)

func main() {
ctx, err := context.BuildContext()
flags := context.ParseIndexerFlags()

if flags.Version {
fmt.Printf("Flare P-chain indexer version %s\n", shared.ApplicationVersion)
return
}

ctx, err := context.BuildContext(flags)
if err != nil {
fmt.Printf("%v\n", err)
return
Expand Down
5 changes: 5 additions & 0 deletions indexer/shared/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package shared

const (
ApplicationVersion = "2.0.0"
)
9 changes: 9 additions & 0 deletions indexer/shared/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func InitMetricsServer(cfg *config.MetricsConfig) {

r.Path("/metrics").Handler(promhttp.Handler())
r.Path("/health").HandlerFunc(healthHandler)
r.Path("/version").HandlerFunc(versionHandler)

srv := &http.Server{
Addr: cfg.PrometheusAddress,
Expand All @@ -70,6 +71,14 @@ func healthHandler(w http.ResponseWriter, r *http.Request) {
}
}

func versionHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
_, err := w.Write([]byte(ApplicationVersion))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}

func writeHealthResponse(w http.ResponseWriter) (err error) {
ok, err := getHealthStatus()
if err != nil {
Expand Down

0 comments on commit fe0dd15

Please sign in to comment.