Skip to content

Commit 7acafef

Browse files
authored
Merge pull request #429 from marle3003/v0.11
V0.11
2 parents a8bf498 + eddd73d commit 7acafef

File tree

157 files changed

+6374
-2278
lines changed

Some content is hidden

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

157 files changed

+6374
-2278
lines changed

.idea/workspace.xml

Lines changed: 945 additions & 498 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,39 @@ Its core feature are:
3636
- **Multiple Provider support**: File, HTTP, GIT, NPM to gather configurations and scripts.
3737
- **Dashboard** to see what's going on.
3838

39-
 
40-
<p align="center">
41-
<a href="https://www.buymeacoffee.com/mokapi" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
42-
<p align="center">
43-
44-
## Example
45-
46-
<img src="webui/public/everythingcode.png" alt="Mokapi Web UI" title="Mokapi Web UI" />
39+
## Usage
4740

48-
## Web UI
41+
Windows
42+
```shell
43+
chcco install mokapi
44+
mokapi --providers-http-url https://petstore31.swagger.io/api/v31/openapi.json
45+
```
4946

50-
<img src="webui.png" alt="Mokapi Web UI" title="Mokapi Web UI" />
47+
MacOS
48+
```shell
49+
brew tap marle3003/tap
50+
brew install mokapi
51+
mokapi --providers-http-url https://petstore31.swagger.io/api/v31/openapi.json
52+
```
5153

52-
## Usage
54+
Docker
55+
```shell
56+
docker run --env 'MOKAPI_Providers_Http_URL'='https://petstore31.swagger.io/api/v31/openapi.json' -p 80:80 -p 8080:8080 mokapi/mokapi:latest
57+
```
5358

59+
Run a request
5460
```shell
55-
docker run --env 'MOKAPI_Services_Swagger-Petstore_Config_Url'='https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml' \
56-
--env 'MOKAPI_Services_Swagger-Petstore_Http_Servers[0]_Url'='http://:80' \
57-
-p 80:80 -p 8080:8080 \
58-
mokapi/mokapi:latest
61+
curl http://localhost/api/v31/pet/2 -H 'Accept: application/json'
5962
```
6063

64+
## Example
65+
66+
<img src="webui/public/control-mock-api-everything.png" alt="Mokapi Web UI" title="Mokapi Web UI" />
67+
68+
## Dashboard
69+
70+
<img src="webui.png" alt="Mokapi Web UI" title="Mokapi Web UI" />
71+
6172
## Documentation
6273

6374
- [Get Started](https://mokapi.io/docs/guides/get-started/welcome)
@@ -68,3 +79,8 @@ docker run --env 'MOKAPI_Services_Swagger-Petstore_Config_Url'='https://raw.gith
6879
- [Javascript API](https://mokapi.io/docs/javascript-api)
6980
- [Examples & Tutorials](https://mokapi.io/docs/examples)
7081
- [Blogs](https://mokapi.io/docs/blogs)
82+
83+
&nbsp;
84+
<p align="center">
85+
<a href="https://www.buymeacoffee.com/mokapi" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
86+
<p align="center">

api/handler.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
132132
}
133133
if isAsset(r.URL.Path) {
134134
r.URL.Path = "/assets/" + filepath.Base(r.URL.Path)
135-
} else if filepath.Ext(r.URL.Path) == ".svg" || filepath.Ext(r.URL.Path) == ".png" {
135+
} else if isImage(r.URL.Path) {
136136
// don't change url
137137
} else {
138138
if len(h.path) > 0 || len(h.base) > 0 {
@@ -213,3 +213,13 @@ func writeJsonBody(w http.ResponseWriter, i interface{}) {
213213
func isAsset(path string) bool {
214214
return strings.Contains(path, "/assets/")
215215
}
216+
217+
func isImage(path string) bool {
218+
str := filepath.Ext(path)
219+
switch str {
220+
case ".jpg", ".jpeg", ".png", ".svg":
221+
return true
222+
default:
223+
return false
224+
}
225+
}

api/handler_fileserver_test.go

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ func TestHandler_FileServer(t *testing.T) {
3737
try.HasHeader("Content-Type", "application/json"),
3838
try.HasBody(`{"version":"","buildTime":""}`))
3939
},
40-
fileServer: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
41-
if request.URL.Path != "/index.html" {
42-
writer.WriteHeader(404)
43-
}
44-
}),
4540
},
4641
{
4742
name: "request web app",
@@ -55,11 +50,6 @@ func TestHandler_FileServer(t *testing.T) {
5550
h,
5651
try.HasStatusCode(200))
5752
},
58-
fileServer: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
59-
if request.URL.Path != "/index.html" {
60-
writer.WriteHeader(404)
61-
}
62-
}),
6353
},
6454
{
6555
name: "request web app",
@@ -73,11 +63,6 @@ func TestHandler_FileServer(t *testing.T) {
7363
h,
7464
try.HasStatusCode(200), try.BodyContains(`<base href="/mokapi/dashboard/" />`))
7565
},
76-
fileServer: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
77-
if request.URL.Path != "/index.html" {
78-
writer.WriteHeader(404)
79-
}
80-
}),
8166
},
8267
{
8368
name: "request asset",
@@ -97,11 +82,6 @@ func TestHandler_FileServer(t *testing.T) {
9782
h,
9883
try.HasStatusCode(200))
9984
},
100-
fileServer: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
101-
if request.URL.Path != "/assets/index.js" {
102-
writer.WriteHeader(404)
103-
}
104-
}),
10585
},
10686
{
10787
name: "request svg",
@@ -115,11 +95,6 @@ func TestHandler_FileServer(t *testing.T) {
11595
h,
11696
try.HasStatusCode(200))
11797
},
118-
fileServer: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
119-
if request.URL.Path != "/logo.svg" {
120-
writer.WriteHeader(404)
121-
}
122-
}),
12398
},
12499
{
125100
name: "request png",
@@ -133,11 +108,19 @@ func TestHandler_FileServer(t *testing.T) {
133108
h,
134109
try.HasStatusCode(200))
135110
},
136-
fileServer: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
137-
if request.URL.Path != "/mail.png" {
138-
writer.WriteHeader(404)
139-
}
140-
}),
111+
},
112+
{
113+
name: "request jpg",
114+
config: static.Api{Path: "/mokapi/dashboard", Dashboard: true},
115+
fn: func(t *testing.T, h http.Handler) {
116+
try.Handler(t,
117+
http.MethodGet,
118+
"http://foo.api/mokapi/dashboard/petstore-rest-api-endpoints.jpg",
119+
nil,
120+
"",
121+
h,
122+
try.HasStatusCode(200))
123+
},
141124
},
142125
{
143126
name: "url rewrite (proxy)",
@@ -152,11 +135,6 @@ func TestHandler_FileServer(t *testing.T) {
152135
try.HasStatusCode(200),
153136
try.BodyContains(`<base href="/foo/mokapi/dashboard/" />`))
154137
},
155-
fileServer: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
156-
if request.URL.Path != "/index.html" {
157-
writer.WriteHeader(404)
158-
}
159-
}),
160138
},
161139
}
162140

api/handler_kafka_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
schematest2 "mokapi/providers/openapi/schema/schematest"
1313
"mokapi/runtime"
1414
"mokapi/runtime/monitor"
15-
"mokapi/schema/json/schematest"
15+
"mokapi/schema/json/schema/schematest"
1616
"mokapi/try"
1717
"net/http"
1818
"testing"

api/handler_schema.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func (h *handler) getExampleData(w http.ResponseWriter, r *http.Request) {
7474
case *schema.Ref:
7575
data, err = getRandomByOpenApi(re.Name, s, ct)
7676
case *avro.Schema:
77-
data, err = getRandomByJson(re.Name, &jsonSchema.Ref{Value: s.Convert()}, ct)
77+
data, err = getRandomByJson(re.Name, s.Convert(), ct)
7878
default:
79-
data, err = getRandomByJson(re.Name, s.(*jsonSchema.Ref), ct)
79+
data, err = getRandomByJson(re.Name, s.(*jsonSchema.Schema), ct)
8080
}
8181

8282
if err != nil {
@@ -103,7 +103,7 @@ func getRandomByOpenApi(name string, r *schema.Ref, ct media.ContentType) ([]byt
103103
return r.Marshal(data, ct)
104104
}
105105

106-
func getRandomByJson(name string, r *jsonSchema.Ref, ct media.ContentType) ([]byte, error) {
106+
func getRandomByJson(name string, r *jsonSchema.Schema, ct media.ContentType) ([]byte, error) {
107107
data, err := generator.New(&generator.Request{
108108
Path: generator.Path{
109109
&generator.PathElement{Name: name, Schema: r},
@@ -215,7 +215,7 @@ func unmarshal(raw json.RawMessage, format string) (interface{}, error) {
215215
err := json.Unmarshal(raw, &a)
216216
return a, err
217217
default:
218-
var r *jsonSchema.Ref
218+
var r *jsonSchema.Schema
219219
err := json.Unmarshal(raw, &r)
220220
return r, err
221221
}

api/handler_schema_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"mokapi/runtime"
99
"mokapi/runtime/monitor"
1010
avro "mokapi/schema/avro/schema"
11-
"mokapi/schema/json/ref"
1211
jsonSchema "mokapi/schema/json/schema"
1312
"mokapi/try"
1413
"net/http"
@@ -260,8 +259,8 @@ func TestSchemaInfo_UnmarshalJSON(t *testing.T) {
260259
test: func(t *testing.T, s *schemaInfo, err error) {
261260
require.NoError(t, err)
262261
require.NotNil(t, s.Schema)
263-
require.IsType(t, &jsonSchema.Ref{}, s.Schema)
264-
require.Equal(t, "object", s.Schema.(*jsonSchema.Ref).Value.Type[0])
262+
require.IsType(t, &jsonSchema.Schema{}, s.Schema)
263+
require.Equal(t, "object", s.Schema.(*jsonSchema.Schema).Type[0])
265264
},
266265
},
267266
{
@@ -271,8 +270,8 @@ func TestSchemaInfo_UnmarshalJSON(t *testing.T) {
271270
require.NoError(t, err)
272271
require.Equal(t, "foo", s.Format)
273272
require.NotNil(t, s.Schema)
274-
require.IsType(t, &jsonSchema.Ref{}, s.Schema)
275-
require.Equal(t, "object", s.Schema.(*jsonSchema.Ref).Value.Type[0])
273+
require.IsType(t, &jsonSchema.Schema{}, s.Schema)
274+
require.Equal(t, "object", s.Schema.(*jsonSchema.Schema).Type[0])
276275
},
277276
},
278277
}
@@ -314,21 +313,21 @@ func TestSchemaInfo_MarshalJSON(t *testing.T) {
314313
},
315314
{
316315
name: "json schema only ref",
317-
s: &schemaInfo{Schema: &jsonSchema.Ref{Reference: ref.Reference{Ref: "foo/bar"}}},
316+
s: &schemaInfo{Schema: &jsonSchema.Schema{Ref: "foo/bar"}},
318317
test: func(t *testing.T, s string, err error) {
319318
require.NoError(t, err)
320-
require.Equal(t, `{"schema":{"ref":"foo/bar"}}`, s)
319+
require.Equal(t, `{"schema":{"$ref":"foo/bar"}}`, s)
321320
},
322321
},
323322
{
324323
name: "json schema ref and value",
325-
s: &schemaInfo{Schema: &jsonSchema.Ref{
326-
Reference: ref.Reference{Ref: "foo/bar"},
327-
Value: &jsonSchema.Schema{Type: jsonSchema.Types{"string"}},
324+
s: &schemaInfo{Schema: &jsonSchema.Schema{
325+
Ref: "foo/bar",
326+
Type: jsonSchema.Types{"string"},
328327
}},
329328
test: func(t *testing.T, s string, err error) {
330329
require.NoError(t, err)
331-
require.Equal(t, `{"schema":{"ref":"foo/bar","type":"string"}}`, s)
330+
require.Equal(t, `{"schema":{"$ref":"foo/bar","type":"string"}}`, s)
332331
},
333332
},
334333
{

api/handler_schema_validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (h *handler) validate(w http.ResponseWriter, r *http.Request) {
3636
case *schema.Ref:
3737
err = parseByOpenApi([]byte(valReq.Data), s, ct)
3838
default:
39-
err = parseByJson([]byte(valReq.Data), s.(*jsonSchema.Ref), ct)
39+
err = parseByJson([]byte(valReq.Data), s.(*jsonSchema.Schema), ct)
4040
}
4141

4242
if err != nil {
@@ -94,7 +94,7 @@ func parseByOpenApi(data []byte, s *schema.Ref, ct media.ContentType) error {
9494
return err
9595
}
9696

97-
func parseByJson(data []byte, s *jsonSchema.Ref, ct media.ContentType) error {
97+
func parseByJson(data []byte, s *jsonSchema.Schema, ct media.ContentType) error {
9898
v, err := encoding.Decode(data, encoding.WithContentType(ct))
9999
if err != nil {
100100
return err

config/dynamic/asyncApi/asyncapitest/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func WithSchemas(name string, s *schema.Schema) ConfigOptions {
7272
c.Components.Schemas = map[string]*asyncapi3.SchemaRef{}
7373
}
7474
c.Components.Schemas[name] = &asyncapi3.SchemaRef{
75-
Value: &asyncapi3.MultiSchemaFormat{Schema: &schema.Ref{Value: s}},
75+
Value: &asyncapi3.MultiSchemaFormat{Schema: s},
7676
}
7777
}
7878
}

config/dynamic/asyncApi/asyncapitest/message.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func NewMessage(opts ...MessageOptions) *asyncApi.Message {
1818

1919
func WithPayload(s *schema.Schema) MessageOptions {
2020
return func(m *asyncApi.Message) {
21-
m.Payload = &asyncapi3.SchemaRef{Value: &asyncapi3.MultiSchemaFormat{Schema: &schema.Ref{Value: s}}}
21+
m.Payload = &asyncapi3.SchemaRef{Value: &asyncapi3.MultiSchemaFormat{Schema: s}}
2222
}
2323
}
2424

@@ -30,7 +30,7 @@ func WithContentType(s string) MessageOptions {
3030

3131
func WithKey(s *schema.Schema) MessageOptions {
3232
return func(m *asyncApi.Message) {
33-
m.Bindings.Kafka.Key = &asyncapi3.SchemaRef{Value: &asyncapi3.MultiSchemaFormat{Schema: &schema.Ref{Value: s}}}
33+
m.Bindings.Kafka.Key = &asyncapi3.SchemaRef{Value: &asyncapi3.MultiSchemaFormat{Schema: s}}
3434
}
3535
}
3636

0 commit comments

Comments
 (0)