Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ func main() {

<a name="serve-with-impl"></a>

#### Listen And Serve with custom ServeMux

Replace http.DefaultServeMux with custom one if you need.

```go
func main() {
mux := http.NewServeMux()
draftSrv := draft.CreateWithMux(mux,
darft.Config{
DevMode: true, // Not production
},
)
userAPI := draft.Compose(
"User API",
new(UserEndpoint),
)

draftSrv.Add(userAPI)
draftSrv.ListenAndServe(srv)
}
```

<a name="serve-with-impl"></a>

#### Listen And Serve with implementation

⚠️ See second argument for `draftSrv.Add`
Expand Down
28 changes: 18 additions & 10 deletions draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func (api *APIService) GroupHR() {

// Handle -
func (api *APIService) Handle(endpoint EndpointAPI, handler http.Handler) {
api.HandleWithMux(http.DefaultServeMux, endpoint, handler)
}

func (api *APIService) HandleWithMux(mux *http.ServeMux, endpoint EndpointAPI, handler http.Handler) {
endpoint.InitEndpoint(endpoint)
pattern := endpoint.URL()

Expand All @@ -175,17 +179,17 @@ func (api *APIService) Handle(endpoint EndpointAPI, handler http.Handler) {

if api.config.MockMode == MockEnable {
if isHTTPHandler(handler) {
http.Handle(pattern, handler)
mux.Handle(pattern, handler)
} else if api.config.DevMode {
http.Handle(pattern, api)
mux.Handle(pattern, api)
}
}

if api.config.DevMode {
http.Handle("/godraft"+pattern, api)
http.Handle("/godraft:doc"+pattern, api)
http.Handle("/godraft:docs"+pattern, api)
http.Handle("/godraft:scheme"+pattern, api)
mux.Handle("/godraft"+pattern, api)
mux.Handle("/godraft:doc"+pattern, api)
mux.Handle("/godraft:docs"+pattern, api)
mux.Handle("/godraft:scheme"+pattern, api)
}
}

Expand Down Expand Up @@ -215,6 +219,10 @@ const (

// Create -
func Create(cfg Config) *APIService {
return CreateWithMux(http.DefaultServeMux, cfg)
}

func CreateWithMux(mux *http.ServeMux, cfg Config) *APIService {
root := createGroupEntry("G", "#root", "")
srv := &APIService{
config: cfg,
Expand All @@ -233,10 +241,10 @@ func Create(cfg Config) *APIService {

if cfg.DevMode && !draftHandled {
draftHandled = true
http.Handle("/godraft:doc/", srv)
http.Handle("/godraft:docs/", srv)
http.Handle("/godraft:scheme/", srv)
http.Handle("/godraft:request/", srv)
mux.Handle("/godraft:doc/", srv)
mux.Handle("/godraft:docs/", srv)
mux.Handle("/godraft:scheme/", srv)
mux.Handle("/godraft:request/", srv)
}

return srv
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ go 1.14
require (
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/russross/blackfriday v2.0.0+incompatible
github.com/sethvargo/go-password v0.1.3
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/stretchr/testify v1.5.1
)
7 changes: 1 addition & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFE
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday v2.0.0+incompatible h1:cBXrhZNUf9C+La9/YpS+UHpUT8YD6Td9ZMSU9APFcsk=
github.com/russross/blackfriday v2.0.0+incompatible/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sethvargo/go-password v0.1.3 h1:18KkbGDkw8SuzeohAbWqBLNSfRQblVwEHOLbPa0PvWM=
github.com/sethvargo/go-password v0.1.3/go.mod h1:2tyaaoHK/AlXwh5WWQDYjqQbHcq4cjPj5qb/ciYvu/Q=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=