Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
added cli pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
WouterVisscher committed Jun 28, 2021
1 parent 9d5b98d commit 99a9681
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 61 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-sqlite3 v1.14.7
github.com/rs/cors v1.7.0
github.com/urfave/cli/v2 v2.3.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/arolek/p v0.0.0-20191103215535-df3c295ed582/go.mod h1:JPNItmi3yb44Q5QWM+Kh5n9oeRhfcJzPNS90mbLo25U=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -54,12 +57,18 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -73,6 +82,7 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
137 changes: 76 additions & 61 deletions start.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
Expand All @@ -12,67 +11,105 @@ import (
"oaf-server/server"
"os"
"regexp"
"strconv"

"github.com/getkin/kin-openapi/openapi3"
"github.com/urfave/cli/v2"

"github.com/rs/cors"
)

func main() {

bindHost := flag.String("s", envString("BIND_HOST", "0.0.0.0"), "server internal bind address, default; 0.0.0.0")
bindPort := flag.Int("p", envInt("BIND_PORT", 8080), "server internal bind address, default; 8080")
app := cli.NewApp()
app.Name = "GOAF"
app.Usage = "A Golang OGC API Features implementation"
app.HideVersion = true
app.HideHelp = true

app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "host",
Aliases: []string{"h"},
Usage: "server internal bind host address",
DefaultText: "0.0.0.0",
Required: false,
EnvVars: []string{"HOST"},
},
&cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "server internal bind port",
DefaultText: "8080",
Required: false,
EnvVars: []string{"PORT"},
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Path to the configuration",
Required: true,
EnvVars: []string{"CONFIG"},
},
}

configfilepath := flag.String("c", envString("CONFIG", ""), "configfile path")
flag.Parse()
app.Action = func(c *cli.Context) error {

config := &core.Config{}
config.ReadConfig(*configfilepath)
configfilepath := c.String("config")
config := &core.Config{}
config.ReadConfig(configfilepath)

// stage 1: create server with spec path and limits
apiServer, err := server.NewServer(config.Service.Url, config.Openapi, uint64(config.DefaultFeatureLimit), uint64(config.MaxFeatureLimit))
if err != nil {
log.Fatal("Server initialisation error:", err)
}
// stage 1: create server with spec path and limits
apiServer, err := server.NewServer(config.Service.Url, config.Openapi, uint64(config.DefaultFeatureLimit), uint64(config.MaxFeatureLimit))
if err != nil {
log.Fatal("Server initialisation error:", err)
}

// stage 2: Create providers based upon provider name
// commonProvider := core.NewCommonProvider(config.Openapi, uint64(config.DefaultFeatureLimit), uint64(config.MaxFeatureLimit))
providers := getProvider(apiServer.Openapi, *config)
// stage 2: Create providers based upon provider name
// commonProvider := core.NewCommonProvider(config.Openapi, uint64(config.DefaultFeatureLimit), uint64(config.MaxFeatureLimit))
providers := getProvider(apiServer.Openapi, *config)

if providers == nil {
log.Fatal("Incorrect provider provided valid names are: gpkg, postgis")
}
if providers == nil {
log.Fatal("Incorrect provider provided valid names are: gpkg, postgis")
}

// stage 3: Add providers, also initialises them
apiServer, err = apiServer.SetProviders(providers)
if err != nil {
log.Fatal("Server initialisation error:", err)
}
// stage 3: Add providers, also initialises them
apiServer, err = apiServer.SetProviders(providers)
if err != nil {
log.Fatal("Server initialisation error:", err)
}

// stage 4: Prepare routing
router := apiServer.Router()

// stage 4: Prepare routing
router := apiServer.Router()
// extra routing for healthcheck
addHealthHandler(router)

// extra routing for healthcheck
addHealthHandler(router)
fs := http.FileServer(http.Dir("swagger-ui"))
router.Handler(regexp.MustCompile("/swagger-ui"), http.StripPrefix("/swagger-ui/", fs))

fs := http.FileServer(http.Dir("swagger-ui"))
router.Handler(regexp.MustCompile("/swagger-ui"), http.StripPrefix("/swagger-ui/", fs))
// cors handler
handler := cors.Default().Handler(router)

// cors handler
handler := cors.Default().Handler(router)
host := c.String("host")
port := c.String("port")

// ServerEndpoint can be different from bind address due to routing externally
bindAddress := fmt.Sprintf("%v:%v", *bindHost, *bindPort)
bindAddress := "0.0.0.0:8080"
if host != "" && port != "" {
bindAddress = fmt.Sprintf("%v:%v", host, port)
}

log.Print("|\n")
log.Printf("| SERVING ON: %s \n", apiServer.ServiceEndpoint)
// ServerEndpoint can be different from bind address due to routing externally
log.Print("|\n")
log.Printf("| SERVING ON: %s \n", apiServer.ServiceEndpoint)

// stage 5: Start server
if err := http.ListenAndServe(bindAddress, handler); err != nil {
log.Fatal("ListenAndServe:", err)
// stage 5: Start server
return http.ListenAndServe(bindAddress, handler)
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

func getProvider(api *openapi3.T, config core.Config) codegen.Providers {
Expand All @@ -94,25 +131,3 @@ func addHealthHandler(router *server.RegexpHandler) {
}
})
}

func envString(key, defaultValue string) string {
value := os.Getenv(key)
if value != "" {
return value
}

return defaultValue
}

func envInt(key string, defaultValue int) int {
value := os.Getenv(key)
if value != "" {
i, e := strconv.ParseInt(value, 10, 32)
if e != nil {
return defaultValue
}
return int(i)
}

return defaultValue
}

0 comments on commit 99a9681

Please sign in to comment.