Skip to content

Commit 2f68197

Browse files
authored
Merge pull request #659 from marle3003/develop
2 parents 44dcad0 + 697dbf6 commit 2f68197

File tree

28 files changed

+890
-266
lines changed

28 files changed

+890
-266
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ For example, you can dynamically change the response based on query parameters:
7878
import { on } from 'mokapi';
7979

8080
export default function() {
81-
on('http', (request, response): boolean => {
81+
on('http', (request, response) => {
8282
switch (request.path.petId) {
8383
case 2:
8484
response.data.name = 'Betty';
85-
return true;
8685
case 9:
8786
response.statusCode = 404;
8887
}
89-
return false;
9088
});
9189
}
9290
```

acceptance/cmd_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package acceptance
22

33
import (
44
"context"
5-
"github.com/pkg/errors"
6-
log "github.com/sirupsen/logrus"
75
"mokapi/api"
86
"mokapi/config/dynamic"
97
"mokapi/config/dynamic/asyncApi"
@@ -21,6 +19,9 @@ import (
2119
"mokapi/server"
2220
"mokapi/server/cert"
2321
"mokapi/version"
22+
23+
"github.com/pkg/errors"
24+
log "github.com/sirupsen/logrus"
2425
)
2526

2627
type Cmd struct {
@@ -67,7 +68,7 @@ func Start(cfg *static.Config) (*Cmd, error) {
6768
})
6869

6970
if u, err := api.BuildUrl(cfg.Api); err == nil {
70-
err = http.AddService("api", u, api.New(app, cfg.Api), true)
71+
err = http.AddInternalService("api", u, api.New(app, cfg.Api))
7172
if err != nil {
7273
return nil, err
7374
}

cmd/mokapi/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"context"
55
"fmt"
6-
log "github.com/sirupsen/logrus"
76
stdlog "log"
87
"mokapi/api"
98
"mokapi/config/decoders"
@@ -28,6 +27,8 @@ import (
2827
"os/signal"
2928
"strings"
3029
"syscall"
30+
31+
log "github.com/sirupsen/logrus"
3132
)
3233

3334
const logo = "888b d888 888 d8888 d8b \n8888b d8888 888 d88888 Y8P \n88888b.d88888 888 d88P888 \n888Y88888P888 .d88b. 888 888 d88P 888 88888b. 888 \n888 Y888P 888 d88\"\"88b 888 .88P d88P 888 888 \"88b 888 \n888 Y8P 888 888 888 888888K d88P 888 888 888 888 \n888 \" 888 Y88..88P 888 \"88b d8888888888 888 d88P 888 \n888 888 \"Y88P\" 888 888 d88P 888 88888P\" 888 \n v%s by Marcel Lehmann%s 888 \n https://mokapi.io 888 \n 888 \n"
@@ -126,7 +127,7 @@ func createServer(cfg *static.Config) (*server.Server, error) {
126127
})
127128

128129
if u, err := api.BuildUrl(cfg.Api); err == nil {
129-
err = http.AddService("api", u, api.New(app, cfg.Api), true)
130+
err = http.AddInternalService("api", u, api.New(app, cfg.Api))
130131
if err != nil {
131132
return nil, err
132133
}

config/dynamic/config.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package dynamic
22

33
import (
44
"bytes"
5-
"github.com/Masterminds/sprig"
65
"mokapi/sortedmap"
76
"strings"
87
"sync"
98
"text/template"
9+
10+
"github.com/Masterminds/sprig"
1011
)
1112

1213
type Event int
@@ -31,13 +32,21 @@ type Validator interface {
3132
Validate() error
3233
}
3334

35+
type SourceType int
36+
37+
const (
38+
SourceMain SourceType = iota
39+
SourceReference
40+
)
41+
3442
type Config struct {
35-
Info ConfigInfo
36-
Raw []byte
37-
Data interface{}
38-
Refs Refs
39-
Listeners Listeners
40-
Scope Scope
43+
Info ConfigInfo
44+
Raw []byte
45+
Data interface{}
46+
Refs Refs
47+
Listeners Listeners
48+
Scope Scope
49+
SourceType SourceType
4150
}
4251

4352
type Refs struct {

config/dynamic/provider/file/file.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"context"
66
"crypto/sha256"
77
"fmt"
8-
"github.com/fsnotify/fsnotify"
9-
log "github.com/sirupsen/logrus"
108
"io"
119
"io/fs"
1210
"math"
@@ -20,6 +18,9 @@ import (
2018
"strings"
2119
"sync"
2220
"time"
21+
22+
"github.com/fsnotify/fsnotify"
23+
log "github.com/sirupsen/logrus"
2324
)
2425

2526
const mokapiIgnoreFile = ".mokapiignore"
@@ -82,6 +83,7 @@ func (p *Provider) Read(u *url.URL) (*dynamic.Config, error) {
8283
if err != nil {
8384
return nil, err
8485
}
86+
config.SourceType = dynamic.SourceReference
8587

8688
p.watchPath(file)
8789
return config, nil

config/dynamic/provider/http/http.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"fmt"
8-
log "github.com/sirupsen/logrus"
98
"hash/fnv"
109
"io"
1110
"mokapi/config/dynamic"
@@ -16,6 +15,8 @@ import (
1615
"os"
1716
"sync"
1817
"time"
18+
19+
log "github.com/sirupsen/logrus"
1920
)
2021

2122
type Provider struct {
@@ -81,6 +82,9 @@ func New(config static.HttpProvider) *Provider {
8182

8283
func (p *Provider) Read(u *url.URL) (*dynamic.Config, error) {
8384
c, _, err := p.readUrl(u)
85+
if c != nil {
86+
c.SourceType = dynamic.SourceReference
87+
}
8488
return c, err
8589
}
8690

config/dynamic/provider/npm/npm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package npm
33
import (
44
"context"
55
"fmt"
6-
log "github.com/sirupsen/logrus"
76
"mokapi/config/dynamic"
87
"mokapi/config/dynamic/provider/file"
98
"mokapi/config/static"
109
"mokapi/safe"
1110
"net/url"
1211
"path/filepath"
1312
"strings"
13+
14+
log "github.com/sirupsen/logrus"
1415
)
1516

1617
type Provider struct {

lib/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func GetUrl(r *http.Request) string {
1010
return r.URL.String()
1111
}
1212
var sb strings.Builder
13-
if strings.HasPrefix(r.Proto, "HTTPS") {
13+
if r.TLS != nil {
1414
sb.WriteString("https://")
1515
} else {
1616
sb.WriteString("http://")

providers/openapi/error.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,36 @@ import (
55
"net/http"
66
)
77

8-
type httpError struct {
8+
type HttpError struct {
99
StatusCode int
1010
Header http.Header
11-
message string
11+
Message string
1212
}
1313

14-
func (h *httpError) Error() string {
15-
return h.message
14+
func (h *HttpError) Error() string {
15+
return h.Message
1616
}
1717

18-
func newHttpError(status int, message string) *httpError {
19-
return &httpError{
18+
func newHttpError(status int, message string) *HttpError {
19+
return &HttpError{
2020
StatusCode: status,
21-
message: message,
21+
Message: message,
2222
}
2323
}
2424

25-
func newHttpErrorf(status int, format string, args ...interface{}) *httpError {
26-
return &httpError{
25+
func newHttpErrorf(status int, format string, args ...interface{}) *HttpError {
26+
return &HttpError{
2727
StatusCode: status,
28-
message: fmt.Sprintf(format, args...),
28+
Message: fmt.Sprintf(format, args...),
2929
}
3030
}
3131

32-
func newMethodNotAllowedErrorf(status int, methods []string, format string, args ...interface{}) *httpError {
33-
return &httpError{
34-
StatusCode: status,
32+
func newMethodNotAllowedErrorf(methods []string, format string, args ...interface{}) *HttpError {
33+
return &HttpError{
34+
StatusCode: http.StatusMethodNotAllowed,
3535
Header: http.Header{
3636
"Allow": methods,
3737
},
38-
message: fmt.Sprintf(format, args...),
38+
Message: fmt.Sprintf(format, args...),
3939
}
4040
}

providers/openapi/event_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package openapi_test
22

33
import (
4-
"github.com/sirupsen/logrus/hooks/test"
5-
"github.com/stretchr/testify/require"
64
"mokapi/engine/enginetest"
75
"mokapi/media"
86
"mokapi/providers/openapi"
@@ -11,13 +9,16 @@ import (
119
"net/http"
1210
"net/http/httptest"
1311
"testing"
12+
13+
"github.com/sirupsen/logrus/hooks/test"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestEvent(t *testing.T) {
1718
testcases := []struct {
1819
name string
1920
config *openapi.Config
20-
test func(t *testing.T, h http.Handler)
21+
test func(t *testing.T, h openapi.Handler)
2122
}{
2223
{
2324
name: "use response example (deprecated)",
@@ -35,12 +36,13 @@ func TestEvent(t *testing.T) {
3536
)),
3637
)),
3738
),
38-
test: func(t *testing.T, h http.Handler) {
39+
test: func(t *testing.T, h openapi.Handler) {
3940
r := httptest.NewRequest("get", "http://localhost/foo", nil)
4041
r.Header.Set("Content-Type", "application/json")
4142
rr := httptest.NewRecorder()
4243

43-
h.ServeHTTP(rr, r)
44+
err := h.ServeHTTP(rr, r)
45+
require.Nil(t, err)
4446
require.Equal(t, http.StatusOK, rr.Code)
4547
require.Equal(t, `"foo"`, rr.Body.String())
4648
},
@@ -71,12 +73,13 @@ func TestEvent(t *testing.T) {
7173
)),
7274
)),
7375
),
74-
test: func(t *testing.T, h http.Handler) {
76+
test: func(t *testing.T, h openapi.Handler) {
7577
r := httptest.NewRequest("get", "http://localhost/foo", nil)
7678
r.Header.Set("Content-Type", "application/json")
7779
rr := httptest.NewRecorder()
7880

79-
h.ServeHTTP(rr, r)
81+
err := h.ServeHTTP(rr, r)
82+
require.Nil(t, err)
8083
require.Equal(t, http.StatusOK, rr.Code)
8184
require.Equal(t, `"foo"`, rr.Body.String())
8285
},

0 commit comments

Comments
 (0)