Skip to content

Commit

Permalink
Add API for sending the config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
haoming29 committed Oct 23, 2023
1 parent b65cc83 commit 87027ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 1 addition & 2 deletions generate/param_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ func GenParamStruct() {
// Same file-reading logic as GenParamEnum
filename, _ := filepath.Abs("../docs/parameters.yaml")
yamlFile, err := os.Open(filename)
defer yamlFile.Close()

if err != nil {
panic(err)
}
defer yamlFile.Close()

decoder := yaml.NewDecoder(yamlFile)

Expand Down
14 changes: 14 additions & 0 deletions origin_ui/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ func resetLoginHandler(ctx *gin.Context) {
}
}

func getConfig(ctx *gin.Context) {
config, err := param.GetUnmarshaledConfig()
if err != nil {
ctx.JSON(500, gin.H{"error": "Failed to get the unmarshaled config"})
return
}

ctx.JSON(200, config)
}

func ConfigureOriginUI(router *gin.Engine) error {
if router == nil {
return errors.New("Origin configuration passed a nil pointer")
Expand Down Expand Up @@ -389,6 +399,10 @@ func ConfigureOriginUI(router *gin.Engine) error {
}
})

// Register using router now. Can be converted to a group when more routes
// are added
router.GET("/api/v1.0/config", authHandler, getConfig)

router.GET("/view/*path", func(ctx *gin.Context) {
path := ctx.Param("path")

Expand Down
9 changes: 5 additions & 4 deletions param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
//go:generate go run ../generate/param_generator.go

var (
viperConfig config
viperConfig *config
)

// Unmarshal Viper config into a struct viperConfig
func UnmarshalConfig() error {
err := viper.Unmarshal(&viperConfig)
viperConfig = new(config)
err := viper.Unmarshal(viperConfig)
if err != nil {
return err
}
Expand All @@ -24,8 +25,8 @@ func UnmarshalConfig() error {

// Return the unmarshaled viper config struct as a pointer
func GetUnmarshaledConfig() (*config, error) {
if &viperConfig == nil {
if viperConfig == nil {
return nil, errors.New("Config hasn't been unmarshaled yet.")
}
return &viperConfig, nil
return viperConfig, nil
}

0 comments on commit 87027ca

Please sign in to comment.