Skip to content

Commit c4cdc64

Browse files
committed
move args into environment variable for boot configuration and add enums into query parameters
1 parent 7a95081 commit c4cdc64

File tree

10 files changed

+33
-22
lines changed

10 files changed

+33
-22
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build:
66
upx -9 ./build/openapi2krakend
77

88
dockerize: build
9-
docker buildx build --platform=linux/amd64 -f docker/Dockerfile -t okhuz/openapi2krakend:0.0.6 .
9+
docker buildx build --platform=linux/amd64 -f docker/Dockerfile -t okhuz/openapi2krakend:0.0.7 .
1010

1111
test:
1212
go test ./... -v

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ files and convert.
1919
-directory: folder where swagger files live. default is swagger folder in repository.
2020
<br>
2121
-encoding: backend encoding for whole endpoints. default is "json".
22-
<br>
23-
-global-timeout: sets global timeout, default is 3000ms, if there is a `x-timeout` attribute in any of the swagger files
24-
this will be overwritten.
2522

2623
### Usage
2724

@@ -57,9 +54,11 @@ configuration file.
5754
| LOG_PREFIX | Log prefix for filtering | `string` | `[KRAKEND]` | no |
5855
| LOG_SYSLOG | Enable syslog | `bool` | `true` | no |
5956
| LOG_STDOUT | Enable stdout | `bool` | `true` | no |
60-
| ENABLE_CORS | Enable CORS plugin for KrakenD | `bol` | `false` | no |
57+
| ENABLE_CORS | Enable CORS plugin for KrakenD | `bool` | `false` | no |
6158
| ALLOWED_ORIGINS | Comma seperated allowed origins, it will be used when ENABLE_CORS is true | `string` | `*` | no |
6259
| ALLOWED_METHODS | Comma seperated allowed methods, it will be used when ENABLE_CORS is true | `string` | `GET,HEAD,POST,PUT,DELETE,CONNECT,OPTIONS,TRACE,PATCH` | no |
60+
| GLOBAL_TIMEOUT | Sets global timeout across all endpoints | `string` | `3000ms` | no |
61+
| ENCODING | Sets default encoding. Values are json, safejson, xml, rss, string, no-op | `string` | `json` | no |
6362

6463
````shell
6564
kubectl apply -f ./deployment

deployment/deployment.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ spec:
1414
spec:
1515
initContainers:
1616
- name: init-configuration
17-
image: okhuz/openapi2krakend:0.0.6
17+
image: okhuz/openapi2krakend:0.0.7
1818
imagePullPolicy: Always
1919
env:
2020
- name: API_URLS
2121
value: "<comma delimited api specification endpoints, format must be OpenAPI v3>"
22+
- name: GLOBAL_TIMEOUT
23+
value: "3600s"
2224
- name: ENABLE_LOGGING
2325
value: "true"
26+
- name: ENCODING
27+
value: "no-op"
2428
- name: LOG_LEVEL
2529
value: "DEBUG"
2630
- name: LOG_PREFIX
27-
value: "[KRAKEND]"
31+
value: "[TENERA]"
2832
- name: LOG_SYSLOG
2933
value: "false"
3034
- name: ENABLE_CORS

pkg/converter/converter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ func Convert(swaggerDirectory string, encoding string, globalTimeout string) mod
6060
}
6161
} else if explodedParams.Type == "Array" {
6262
krakendEndpoint.InsertQuerystringParams(parameter.Name)
63+
} else if explodedParams.Type == "string" && explodedParams.Enum != nil {
64+
krakendEndpoint.InsertQuerystringParams(parameter.Name)
6365
}
6466
} else {
6567
krakendEndpoint.InsertQuerystringParams(parameter.Name)
6668
}
69+
} else if parameter.In == "header" {
70+
krakendEndpoint.InsertHeadersToPass(parameter.Name)
6771
}
6872
}
6973

pkg/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import (
44
"encoding/json"
55
"flag"
66
"github.com/okhuz/openapi2krakend/pkg/converter"
7+
"github.com/okhuz/openapi2krakend/pkg/utility"
78
"os"
89
"path"
910
)
1011

1112
func main() {
1213
swaggerDirectory := flag.String("directory", "./swagger", "Directory of the swagger files")
13-
encoding := flag.String("encoding", "json", "Sets default encoding. Values are json, safejson, xml, rss, string, no-op")
14-
globalTimeout := flag.String("globalTimeout", "3000ms", "Sets global timeout")
1514

1615
flag.Parse()
1716

18-
configuration := converter.Convert(*swaggerDirectory, *encoding, *globalTimeout)
17+
encoding := utility.GetEnv("ENCODING", "json");
18+
globalTimeout := utility.GetEnv("GLOBAL_TIMEOUT", "3000ms")
19+
20+
configuration := converter.Convert(*swaggerDirectory, encoding, globalTimeout)
1921

2022
file, _ := json.MarshalIndent(configuration, "", " ")
2123
_ = os.MkdirAll(path.Join(path.Base(""), "output"), 0777)

pkg/models/configuration.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"github.com/okhuz/openapi2krakend/pkg/utility"
45
"strings"
56
)
67

@@ -67,10 +68,10 @@ type Configuration struct {
6768
func NewConfiguration(outputEncoding string, timeout string) Configuration {
6869
var extraConfig = make(map[string]interface{}, 15)
6970

70-
if getEnv("ENABLE_CORS", "false") == "true" {
71+
if utility.GetEnv("ENABLE_CORS", "false") == "true" {
7172
extraConfig["security/cors"] = NewCors()
7273
}
73-
if getEnv("ENABLE_LOGGING", "false") == "true" {
74+
if utility.GetEnv("ENABLE_LOGGING", "false") == "true" {
7475
extraConfig["telemetry/logging"] = NewLogging()
7576
}
7677

pkg/models/plugins.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package models
22

33
import (
44
"errors"
5+
"github.com/okhuz/openapi2krakend/pkg/utility"
56
"log"
67
"strings"
78
)
@@ -30,7 +31,7 @@ func NewLogging() Logging {
3031

3132
func getLogLevel() (string, error) {
3233
LogLevels := []string{"CRITICAL", "DEBUG", "ERROR", "INFO", "WARNING"}
33-
LogLevel := strings.ToUpper(getEnv("LOG_LEVEL", "WARNING"))
34+
LogLevel := strings.ToUpper(utility.GetEnv("LOG_LEVEL", "WARNING"))
3435

3536
for i := range LogLevels {
3637
if LogLevels[i] == LogLevel {
@@ -42,19 +43,19 @@ func getLogLevel() (string, error) {
4243
}
4344

4445
func getLogPrefix() string {
45-
return getEnv("LOG_PREFIX", "[KRAKEND]")
46+
return utility.GetEnv("LOG_PREFIX", "[KRAKEND]")
4647
}
4748

4849
func getSysLog() bool {
49-
if strings.ToLower(getEnv("LOG_SYSLOG", "true")) == "true" {
50+
if strings.ToLower(utility.GetEnv("LOG_SYSLOG", "true")) == "true" {
5051
return true
5152
} else {
5253
return false
5354
}
5455
}
5556

5657
func getStdout() bool {
57-
if strings.ToLower(getEnv("LOG_STDOUT", "true")) == "true" {
58+
if strings.ToLower(utility.GetEnv("LOG_STDOUT", "true")) == "true" {
5859
return true
5960
} else {
6061
return false
@@ -69,9 +70,9 @@ type Cors struct {
6970
}
7071

7172
func NewCors() Cors {
72-
allowOrigins := strings.Split(getEnv("ALLOWED_ORIGINS", "*"), ",")
73+
allowOrigins := strings.Split(utility.GetEnv("ALLOWED_ORIGINS", "*"), ",")
7374
var allowedMethods []string
74-
methodsEnv := getEnv("ALLOWED_METHODS", "")
75+
methodsEnv := utility.GetEnv("ALLOWED_METHODS", "")
7576
if methodsEnv != "" {
7677
allowedMethods = strings.Split(methodsEnv, ",")
7778
} else {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package models
1+
package utility
22

33
import "os"
44

5-
func getEnv(key, fallback string) string {
5+
func GetEnv(key, fallback string) string {
66
if value, ok := os.LookupEnv(key); ok {
77
return value
88
}

scripts/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ export LOG_PREFIX="[TEST]"
77
export ALLOWED_ORIGINS=https://tenera.io
88
export ENABLE_CORS=true
99
export ALLOWED_METHODS="GET,POST"
10+
export GLOBAL_TIMEOUT="3600s"
11+
export ENCODING="no-op"
1012

1113
go run ./pkg

swagger/pet-store.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"openapi": "3.0.2",
3-
"x-timeout": "30000ms",
43
"info": {
54
"title": "Swagger Petstore - OpenAPI 3.0",
65
"description": "This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about\nSwagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!\nYou can now help us improve the API whether it's by making changes to the definition itself or to the code.\nThat way, with time, we can improve the API in general, and expose some of the new features in OAS3.\n\nSome useful links:\n- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)\n- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)",
@@ -48,7 +47,6 @@
4847
"paths": {
4948
"/pet": {
5049
"put": {
51-
"x-timeout": "20000ms",
5250
"tags": [
5351
"pet"
5452
],

0 commit comments

Comments
 (0)