Skip to content

Commit bfdbd78

Browse files
authored
Merge pull request #604 from marle3003/develop
Develop
2 parents fa356ea + 1a09731 commit bfdbd78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3134
-593
lines changed

acceptance/petstore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (suite *PetStoreSuite) TestKafka_TopicConfig() {
195195
require.Equal(suite.T(), "petstore.order-event", r.Topics[0].Name)
196196
require.Len(suite.T(), r.Topics[0].Partitions, 2)
197197

198-
require.Len(suite.T(), suite.cmd.App.Http.List(), 1)
198+
require.Len(suite.T(), suite.cmd.App.ListHttp(), 1)
199199
}
200200

201201
func (suite *PetStoreSuite) TestKafka_Produce_InvalidFormat() {

api/handler.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
type handler struct {
20+
config static.Api
2021
path string
2122
base string
2223
app *runtime.App
@@ -28,6 +29,11 @@ type info struct {
2829
Version string `json:"version"`
2930
BuildTime string `json:"buildTime"`
3031
ActiveServices []string `json:"activeServices,omitempty"`
32+
Search search `json:"search"`
33+
}
34+
35+
type search struct {
36+
Enabled bool `json:"enabled"`
3137
}
3238

3339
type serviceType string
@@ -60,9 +66,10 @@ type apiError struct {
6066

6167
func New(app *runtime.App, config static.Api) http.Handler {
6268
h := &handler{
63-
path: config.Path,
64-
base: config.Base,
65-
app: app,
69+
config: config,
70+
path: config.Path,
71+
base: config.Base,
72+
app: app,
6673
}
6774

6875
if config.Dashboard {
@@ -128,6 +135,8 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
128135
h.handleConfig(w, r)
129136
case strings.HasPrefix(p, "/api/faker/tree"):
130137
h.handleFakerTree(w, r)
138+
case strings.HasPrefix(p, "/api/search"):
139+
h.getSearchResults(w, r)
131140
case h.fileServer != nil:
132141
if r.Method != "GET" {
133142
http.Error(w, fmt.Sprintf("method %v is not allowed", r.Method), http.StatusMethodNotAllowed)
@@ -164,7 +173,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
164173

165174
func (h *handler) getServices(w http.ResponseWriter, _ *http.Request) {
166175
services := make([]interface{}, 0)
167-
services = append(services, getHttpServices(h.app.Http, h.app.Monitor)...)
176+
services = append(services, getHttpServices(h.app.ListHttp(), h.app.Monitor)...)
168177
services = append(services, getKafkaServices(h.app.Kafka, h.app.Monitor)...)
169178
services = append(services, getMailServices(h.app.Mail, h.app.Monitor)...)
170179
services = append(services, getLdapServices(h.app.Ldap, h.app.Monitor)...)
@@ -188,8 +197,8 @@ func writeError(w http.ResponseWriter, err error, status int) {
188197
func (h *handler) getInfo(w http.ResponseWriter, _ *http.Request) {
189198
w.Header().Set("Content-Type", "application/json")
190199

191-
i := info{Version: h.app.Version, BuildTime: h.app.BuildTime}
192-
if len(h.app.Http.List()) > 0 {
200+
i := info{Version: h.app.Version, BuildTime: h.app.BuildTime, Search: search{Enabled: h.config.Search.Enabled}}
201+
if len(h.app.ListHttp()) > 0 {
193202
i.ActiveServices = append(i.ActiveServices, "http")
194203
}
195204
if len(h.app.Kafka.List()) > 0 {

api/handler_fileserver_test.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ func TestHandler_FileServer(t *testing.T) {
2323
fn func(t *testing.T, h http.Handler)
2424
fileServer http.Handler
2525
}{
26-
{
27-
name: "request api info",
28-
config: static.Api{Path: "/mokapi", Dashboard: true},
29-
fn: func(t *testing.T, h http.Handler) {
30-
try.Handler(t,
31-
http.MethodGet,
32-
"http://foo.api/mokapi/api/info",
33-
nil,
34-
"",
35-
h,
36-
try.HasStatusCode(200),
37-
try.HasHeader("Content-Type", "application/json"),
38-
try.HasBody(`{"version":"","buildTime":""}`))
39-
},
40-
},
4126
{
4227
name: "request web app",
4328
config: static.Api{Path: "/mokapi", Dashboard: true},
@@ -161,7 +146,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
161146
test: func(t *testing.T) {
162147
cfg := &static.Config{}
163148
app := runtime.New(cfg)
164-
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."))})
149+
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."))})
165150
h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true})
166151
try.Handler(t,
167152
http.MethodGet,
@@ -181,7 +166,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
181166
test: func(t *testing.T) {
182167
cfg := &static.Config{}
183168
app := runtime.New(cfg)
184-
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
169+
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
185170
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
186171
openapitest.WithPath("/pet/{petId}", openapitest.NewPath()),
187172
)},
@@ -205,7 +190,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
205190
test: func(t *testing.T) {
206191
cfg := &static.Config{}
207192
app := runtime.New(cfg)
208-
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
193+
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
209194
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
210195
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
211196
openapitest.WithPathInfo("foo", "bar"),
@@ -231,7 +216,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
231216
test: func(t *testing.T) {
232217
cfg := &static.Config{}
233218
app := runtime.New(cfg)
234-
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
219+
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
235220
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
236221
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
237222
openapitest.WithPathInfo("", "bar"),
@@ -256,7 +241,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
256241
test: func(t *testing.T) {
257242
cfg := &static.Config{}
258243
app := runtime.New(cfg)
259-
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
244+
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
260245
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
261246
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
262247
openapitest.WithOperation("GET", openapitest.NewOperation()),
@@ -281,7 +266,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
281266
test: func(t *testing.T) {
282267
cfg := &static.Config{}
283268
app := runtime.New(cfg)
284-
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
269+
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
285270
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
286271
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
287272
openapitest.WithOperation("GET", openapitest.NewOperation()),

api/handler_http.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ type securityScheme struct {
9191
Configs interface{} `json:"configs"`
9292
}
9393

94-
func getHttpServices(store *runtime.HttpStore, m *monitor.Monitor) []interface{} {
95-
list := store.List()
94+
func getHttpServices(list []*runtime.HttpInfo, m *monitor.Monitor) []interface{} {
9695
result := make([]interface{}, 0, len(list))
9796
for _, hs := range list {
9897
s := service{
@@ -124,7 +123,7 @@ func (h *handler) getHttpService(w http.ResponseWriter, r *http.Request, m *moni
124123
segments := strings.Split(r.URL.Path, "/")
125124
name := segments[4]
126125

127-
s := h.app.Http.Get(name)
126+
s := h.app.GetHttp(name)
128127
if s == nil {
129128
w.WriteHeader(404)
130129
return

api/handler_http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestHandler_Http(t *testing.T) {
7373
),
7474
}
7575
cfg.Info.Time = mustTime("2023-12-27T13:01:30+00:00")
76-
app.Http.Add(cfg)
76+
app.AddHttp(cfg)
7777
return app
7878
},
7979
requestUrl: "http://foo.api/api/services/http/foo",

api/handler_schema.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ func getSpecs(app *runtime.App, spec, name string) []interface{} {
334334
switch spec {
335335
case "openapi":
336336
if name == "" {
337-
for _, s := range app.Http.List() {
337+
for _, s := range app.ListHttp() {
338338
results = append(results, s)
339339
}
340340
}
341-
s := app.Http.Get(name)
341+
s := app.GetHttp(name)
342342
if s != nil {
343343
results = append(results, s)
344344
}
@@ -352,10 +352,10 @@ func getSpecs(app *runtime.App, spec, name string) []interface{} {
352352
}
353353
default:
354354
if name == "" {
355-
for _, s := range app.Http.List() {
355+
for _, s := range app.ListHttp() {
356356
results = append(results, s)
357357
}
358-
} else if s := app.Http.Get(name); s != nil {
358+
} else if s := app.GetHttp(name); s != nil {
359359
results = append(results, s)
360360
}
361361
if name == "" {

api/handler_search.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
"net/url"
6+
"strings"
7+
)
8+
9+
func (h *handler) getSearchResults(w http.ResponseWriter, r *http.Request) {
10+
w.Header().Set("Content-Type", "application/json")
11+
12+
queryText := getQueryParamInsensitive(r.URL.Query(), "querytext")
13+
results, err := h.app.Search(queryText)
14+
if err != nil {
15+
writeError(w, err, http.StatusInternalServerError)
16+
} else {
17+
writeJsonBody(w, results)
18+
}
19+
}
20+
21+
func getQueryParamInsensitive(values url.Values, key string) string {
22+
key = strings.ToLower(key)
23+
for k, v := range values {
24+
if strings.ToLower(k) == key && len(v) > 0 {
25+
return v[0]
26+
}
27+
}
28+
return ""
29+
}

api/handler_search_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package api
2+
3+
import (
4+
"mokapi/config/dynamic"
5+
"mokapi/config/dynamic/dynamictest"
6+
"mokapi/config/static"
7+
"mokapi/providers/openapi"
8+
"mokapi/providers/openapi/openapitest"
9+
"mokapi/runtime"
10+
"mokapi/try"
11+
"net/http"
12+
"testing"
13+
)
14+
15+
func TestHandler_SearchQuery(t *testing.T) {
16+
toConfig := func(c *openapi.Config) *dynamic.Config {
17+
cfg := &dynamic.Config{
18+
Info: dynamictest.NewConfigInfo(),
19+
Data: c,
20+
}
21+
return cfg
22+
}
23+
24+
testcases := []struct {
25+
name string
26+
app func() *runtime.App
27+
requestUrl string
28+
responseBody string
29+
}{
30+
{
31+
name: "empty search query",
32+
requestUrl: "/api/search/query",
33+
responseBody: `[{"type":"HTTP","configName":"","title":"foo"}]`,
34+
app: func() *runtime.App {
35+
app := runtime.New(&static.Config{Api: static.Api{Search: static.Search{
36+
Enabled: true,
37+
Analyzer: "ngram",
38+
Ngram: static.NgramAnalyzer{
39+
Min: 3,
40+
Max: 5,
41+
},
42+
}}})
43+
44+
cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "", ""))
45+
app.AddHttp(toConfig(cfg))
46+
47+
return app
48+
},
49+
},
50+
{
51+
name: "search with query text",
52+
requestUrl: "/api/search/query?queryText=foo",
53+
responseBody: `[{"type":"HTTP","configName":"","title":"foo","fragments":["\u003cmark\u003efoo\u003c/mark\u003e"]}]`,
54+
app: func() *runtime.App {
55+
app := runtime.New(&static.Config{Api: static.Api{Search: static.Search{
56+
Enabled: true,
57+
Analyzer: "ngram",
58+
Ngram: static.NgramAnalyzer{
59+
Min: 3,
60+
Max: 5,
61+
},
62+
}}})
63+
64+
cfg := openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "", ""))
65+
app.AddHttp(toConfig(cfg))
66+
67+
return app
68+
},
69+
},
70+
}
71+
72+
t.Parallel()
73+
for _, tc := range testcases {
74+
tc := tc
75+
t.Run(tc.name, func(t *testing.T) {
76+
t.Parallel()
77+
78+
h := New(tc.app(), static.Api{})
79+
80+
try.Handler(t,
81+
http.MethodGet,
82+
tc.requestUrl,
83+
nil,
84+
"",
85+
h,
86+
try.HasStatusCode(200),
87+
try.HasHeader("Content-Type", "application/json"),
88+
try.HasBody(tc.responseBody))
89+
})
90+
}
91+
}

0 commit comments

Comments
 (0)