-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): GiveAPIServer attachs all routes to api
- Loading branch information
Showing
2 changed files
with
32 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package lib | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
// GiveAPIServer creates an API server that gives access to lib's registered methods | ||
func (inst *Instance) GiveAPIServer(middleware func(handler http.HandlerFunc) http.HandlerFunc, ignoreMethods []string) *mux.Router { | ||
m := mux.NewRouter() | ||
for methodName, call := range inst.regMethods.reg { | ||
if arrayContainsString(ignoreMethods, methodName) { | ||
continue | ||
} | ||
handler := middleware(NewHTTPRequestHandler(inst, methodName)) | ||
// All endpoints use POST verb | ||
httpVerb := http.MethodPost | ||
m.Handle(string(call.Endpoint), handler).Methods(httpVerb) | ||
} | ||
return m | ||
} | ||
|
||
func arrayContainsString(searchSpace []string, target string) bool { | ||
for _, elem := range searchSpace { | ||
if elem == target { | ||
return true | ||
} | ||
} | ||
return false | ||
} |