Skip to content

Commit dbe70b0

Browse files
committed
feat: refactor App initialization to use functional options for framework and OpenAPI spec configuration
1 parent 201d7e7 commit dbe70b0

10 files changed

Lines changed: 54 additions & 33 deletions

File tree

app.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,45 @@ type App struct {
1515
endpoints []*Endpoint
1616
}
1717

18-
func New(frameworkName, basePkgName string) *App {
18+
type AppConfig struct {
19+
framework framework.Framework
20+
spec *spec.Spec
21+
}
22+
23+
type AppOption func(*AppConfig)
24+
25+
func WithCreateFramework(frameworkName string) AppOption {
26+
return func(c *AppConfig) {
27+
c.framework = framework.GetFramework(frameworkName)
28+
}
29+
}
30+
31+
func WithFramework(framework framework.Framework) AppOption {
32+
return func(c *AppConfig) {
33+
c.framework = framework
34+
}
35+
}
36+
37+
func WithAutoOpenAPISpec(basePkgName string) AppOption {
38+
return func(c *AppConfig) {
39+
c.spec = spec.New(basePkgName)
40+
}
41+
}
42+
43+
func New(opts ...AppOption) *App {
44+
config := &AppConfig{}
45+
for _, opt := range opts {
46+
opt(config)
47+
}
1948

2049
s := &App{
21-
framework: framework.GetFramework(frameworkName),
22-
spec: spec.New(basePkgName),
50+
framework: config.framework,
51+
spec: config.spec,
2352
endpoints: []*Endpoint{},
2453
}
25-
addOpenAPIRoutes(s)
54+
if s.spec != nil {
55+
addOpenAPIRoutes(s)
56+
}
2657
return s
2758
}
2859

@@ -63,11 +94,10 @@ func (s *App) PATCH(path string, handlers ...interface{}) *Endpoint {
6394
func (s *App) Sync() {
6495
for _, e := range s.endpoints {
6596
s.framework.Register(e.path, e.method, s.createHandler(e.handlers...))
66-
if e.addToSpec {
97+
if e.addToSpec && s.spec != nil {
6798
s.spec.Register(s.framework.GetOpenAPICompatiblePathPattern(e.path), e.method, e.tags, e.operationId, e.summary, e.description, e.handlers...)
6899
}
69100
}
70-
s.spec.Write("openapi.gen.json")
71101
}
72102

73103
func (s *App) ListenAndServe(addr string) error {

openapi.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const redocUI = `<!DOCTYPE html>
8282
</html>`
8383

8484
func addOpenAPIRoutes(app *App) {
85+
// TODO make the routes configurable
8586
app.GET("/_try/swagger", func() (*types.HTMLResponse, error) {
8687
return &types.HTMLResponse{
8788
HTML: swaggerUI,

pkg/spec/main.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package spec
22

33
import (
4-
"encoding/json"
54
"net/http"
6-
"os"
75
"reflect"
86
"strings"
97

@@ -120,11 +118,3 @@ func (s *Spec) Register(path string, method string, tags []string, operationId,
120118
s.spec.Paths.Value(path).Patch = op
121119
}
122120
}
123-
124-
func (s *Spec) Write(path string) error {
125-
json, err := json.MarshalIndent(s.spec, "", " ")
126-
if err != nil {
127-
return err
128-
}
129-
return os.WriteFile(path, json, 0644)
130-
}

tests/basics_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestNoRoutes(t *testing.T) {
1818
frameworks := utils.GetAllFrameworks()
1919
for _, framework := range frameworks {
2020
t.Run(framework, func(t *testing.T) {
21-
app := simplapi.New(framework, "")
21+
app := simplapi.New(simplapi.WithCreateFramework(framework))
2222
fApp := app.GetApp()
2323

2424
req := httptest.NewRequest(http.MethodGet, "/", nil)
@@ -43,7 +43,7 @@ func TestGET(t *testing.T) {
4343
frameworks := utils.GetAllFrameworks()
4444
for _, framework := range frameworks {
4545
t.Run(framework, func(t *testing.T) {
46-
app := simplapi.New(framework, "")
46+
app := simplapi.New(simplapi.WithCreateFramework(framework))
4747
fApp := app.GetApp()
4848

4949
app.GET("/", func() (*HelloResponse, error) {
@@ -72,7 +72,7 @@ func TestListen(t *testing.T) {
7272
frameworks := utils.GetAllFrameworks()
7373
for _, framework := range frameworks {
7474
t.Run(framework, func(t *testing.T) {
75-
app := simplapi.New(framework, "")
75+
app := simplapi.New(simplapi.WithCreateFramework(framework))
7676

7777
app.GET("/", func() (*HelloResponse, error) {
7878
return &HelloResponse{

tests/chain_tests/chain_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestChainReturnedFromFirstFunc(t *testing.T) {
2828
frameworks := utils.GetAllFrameworks()
2929
for _, framework := range frameworks {
3030
t.Run(framework, func(t *testing.T) {
31-
app := simplapi.New(framework, "")
31+
app := simplapi.New(simplapi.WithCreateFramework(framework))
3232
fApp := app.GetApp()
3333

3434
func1 := func() (*HelloResponse, error) {
@@ -62,7 +62,7 @@ func TestChainReturnedFromSecondFunc(t *testing.T) {
6262
frameworks := utils.GetAllFrameworks()
6363
for _, framework := range frameworks {
6464
t.Run(framework, func(t *testing.T) {
65-
app := simplapi.New(framework, "")
65+
app := simplapi.New(simplapi.WithCreateFramework(framework))
6666
fApp := app.GetApp()
6767

6868
func1 := func() (*HelloResponse, error) {
@@ -96,7 +96,7 @@ func TestChainReturnedFromNoFunc(t *testing.T) {
9696
frameworks := utils.GetAllFrameworks()
9797
for _, framework := range frameworks {
9898
t.Run(framework, func(t *testing.T) {
99-
app := simplapi.New(framework, "")
99+
app := simplapi.New(simplapi.WithCreateFramework(framework))
100100
fApp := app.GetApp()
101101

102102
func1 := func() (*HelloResponse, error) {
@@ -125,7 +125,7 @@ func TestChainReturnedErrorFromFirstFunc(t *testing.T) {
125125
frameworks := utils.GetAllFrameworks()
126126
for _, framework := range frameworks {
127127
t.Run(framework, func(t *testing.T) {
128-
app := simplapi.New(framework, "")
128+
app := simplapi.New(simplapi.WithCreateFramework(framework))
129129
fApp := app.GetApp()
130130

131131
func1 := func() (*HelloResponse, error) {
@@ -155,7 +155,7 @@ func TestContextInChain(t *testing.T) {
155155
frameworks := utils.GetAllFrameworks()
156156
for _, framework := range frameworks {
157157
t.Run(framework, func(t *testing.T) {
158-
app := simplapi.New(framework, "")
158+
app := simplapi.New(simplapi.WithCreateFramework(framework))
159159
fApp := app.GetApp()
160160

161161
func1 := func(ctx *context.Context) (*HelloResponse, error) {

tests/injection_tests/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func SetupTest(frameworkName string, method string) *simplapi.App {
14-
return simplapi.New(frameworkName, "")
14+
return simplapi.New(simplapi.WithCreateFramework(frameworkName))
1515
}
1616

1717
type HelloResponse struct {

tests/multipart_tests/multipart_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestFileUpload(t *testing.T) {
1919
frameworks := utils.GetAllFrameworks()
2020
for _, framework := range frameworks {
2121
t.Run(framework, func(t *testing.T) {
22-
app := simplapi.New(framework, "")
22+
app := simplapi.New(simplapi.WithCreateFramework(framework))
2323
fApp := app.GetApp()
2424

2525
type FileUploadInput struct {

tests/panic_tests/panic_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestNonFuncHandler(t *testing.T) {
2424
assert.NotNil(t, r)
2525
assert.Equal(t, "handler must be a function", r)
2626
}()
27-
app := simplapi.New("fiber", "")
27+
app := simplapi.New(simplapi.WithCreateFramework("fiber"))
2828
app.GET("/", "hello")
2929
app.Sync()
3030
}
@@ -35,7 +35,7 @@ func TestNoOutputHandler(t *testing.T) {
3535
assert.NotNil(t, r)
3636
assert.Equal(t, "handler must return an error at the least", r)
3737
}()
38-
app := simplapi.New("fiber", "")
38+
app := simplapi.New(simplapi.WithCreateFramework("fiber"))
3939
app.GET("/", func() {})
4040
app.Sync()
4141
}
@@ -46,7 +46,7 @@ func TestHandlerThatDoesnReturnError(t *testing.T) {
4646
assert.NotNil(t, r)
4747
assert.Equal(t, "handler must return an error at the last position", r)
4848
}()
49-
app := simplapi.New("fiber", "")
49+
app := simplapi.New(simplapi.WithCreateFramework("fiber"))
5050
app.GET("/", func() *HelloResponse {
5151
return nil
5252
})
@@ -59,7 +59,7 @@ func TestHandlerResponseMustBePointer(t *testing.T) {
5959
assert.NotNil(t, r)
6060
assert.Equal(t, "response type must be a pointer", r)
6161
}()
62-
app := simplapi.New("fiber", "")
62+
app := simplapi.New(simplapi.WithCreateFramework("fiber"), simplapi.WithAutoOpenAPISpec("tests"))
6363
app.GET("/", func() (HelloResponse, error) {
6464
return HelloResponse{}, nil
6565
})

tests/swagger_spec_tests/swagger_url_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestOpenAPIJsonURL(t *testing.T) {
1616
frameworks := utils.GetAllFrameworks()
1717
for _, framework := range frameworks {
18-
app := simplapi.New(framework, "")
18+
app := simplapi.New(simplapi.WithCreateFramework(framework), simplapi.WithAutoOpenAPISpec("tests"))
1919
app.Sync()
2020
fApp := app.GetApp()
2121

@@ -31,7 +31,7 @@ func TestOpenAPIJsonURL(t *testing.T) {
3131
func TestTryURL(t *testing.T) {
3232
frameworks := utils.GetAllFrameworks()
3333
for _, framework := range frameworks {
34-
app := simplapi.New(framework, "")
34+
app := simplapi.New(simplapi.WithCreateFramework(framework), simplapi.WithAutoOpenAPISpec("tests"))
3535
app.Sync()
3636
fApp := app.GetApp()
3737
{

tests/swagger_spec_tests/variety_of_schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (r *Response3) GetStatusCode() int {
7575
func TestAllPossibleSchemaStuff(t *testing.T) {
7676
frameworks := utils.GetAllFrameworks()
7777
for _, framework := range frameworks {
78-
app := simplapi.New(framework, "")
78+
app := simplapi.New(simplapi.WithCreateFramework(framework), simplapi.WithAutoOpenAPISpec("tests"))
7979

8080
app.POST("/json", func(input JsonInput) (*Response1, *Response2, *Response3, error) {
8181
return nil, nil, nil, nil

0 commit comments

Comments
 (0)