Skip to content

Commit

Permalink
Make API port and host configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
thmull committed Aug 24, 2023
1 parent c58c08b commit 0ad88e6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
52 changes: 36 additions & 16 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
controllerHelpers "github.com/kiprotect/kodex/api/helpers/controller"
ginHelpers "github.com/kiprotect/kodex/api/helpers/gin"
"github.com/urfave/cli"
"strconv"
"sync"
)

Expand All @@ -51,8 +50,16 @@ func API(controller kodex.Controller, definitions interface{}) ([]cli.Command, e
Value: "",
Usage: "enable profiler and store results to given filename",
},
cli.IntFlag{
Name: "port",
Usage: "The port to bind to",
},
cli.StringFlag{
Name: "host",
Usage: "The host to bind to",
},
},
Usage: "Run the KIProtect API.",
Usage: "Run the Kodex API.",
Action: func(c *cli.Context) error {

blueprintName := ""
Expand All @@ -61,7 +68,7 @@ func API(controller kodex.Controller, definitions interface{}) ([]cli.Command, e
blueprintName = c.Args().Get(0)
}

return RunAPI(controller, apiDefinitions, "", nil, blueprintName)
return RunAPI(controller, apiDefinitions, c.String("host"), c.Int("port"), "", nil, blueprintName)
},
},
},
Expand All @@ -70,26 +77,41 @@ func API(controller kodex.Controller, definitions interface{}) ([]cli.Command, e

}

func RunAPI(controller kodex.Controller, definitions *api.Definitions, prefix string, handlerMaker func(api.Controller, http.Handler) (http.Handler, error), blueprintName string) error {
func RunAPI(controller kodex.Controller, definitions *api.Definitions, host string, port int, prefix string, handlerMaker func(api.Controller, http.Handler) (http.Handler, error), blueprintName string) error {
kodex.Log.Infof("Running Kodex - API %s", kodex.Version)

var wg sync.WaitGroup

port, ok := controller.Settings().Int("port")
if !ok {
port = 8000
}
host, ok := controller.Settings().String("host")
if !ok {
host = "0.0.0.0"
}

apiController, err := controllerHelpers.ApiController(controller, definitions)

if err != nil {
return err
}

var ok bool

if port == 0 {

port, ok = controller.Settings().Int("port")

if !ok {
port = 8000
}

}

if host == "" {

host, ok = controller.Settings().String("host")

if !ok {
host = "0.0.0.0"
}

}

bindAddress := fmt.Sprintf("%s:%d", host, port)

if blueprintName != "" {

blueprintConfig, err := kodex.LoadBlueprintConfig(apiController.Settings(), blueprintName, "")
Expand All @@ -111,9 +133,7 @@ func RunAPI(controller kodex.Controller, definitions *api.Definitions, prefix st

}

var addr = host + ":" + strconv.Itoa(port)

srv, _, err := ginHelpers.RunApi(apiController, addr, prefix, handlerMaker, &wg)
srv, _, err := ginHelpers.RunApi(apiController, bindAddress, prefix, handlerMaker, &wg)

if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ func RunApp(controller kodex.Controller, definitions *api.Definitions, blueprint
}, nil
}

return apiCmd.RunAPI(controller, definitions, "/api", handlerMaker, blueprintName)
return apiCmd.RunAPI(controller, definitions, "", 0, "/api", handlerMaker, blueprintName)
}
4 changes: 2 additions & 2 deletions cmd/helpers/kodex.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func Kodex(definitions *api.Definitions) {
}

app := cli.NewApp()
app.Name = "KIProtect"
app.Usage = "Run all KIProtect commands"
app.Name = "Kodex"
app.Usage = "Run all Kodex commands"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "level",
Expand Down

0 comments on commit 0ad88e6

Please sign in to comment.