github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/lib/routes.go (about)

     1  package lib
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gorilla/mux"
     7  	qhttp "github.com/qri-io/qri/lib/http"
     8  )
     9  
    10  // GiveAPIServer creates an API server that gives access to lib's registered methods
    11  func (inst *Instance) GiveAPIServer(middleware func(handler http.HandlerFunc) http.HandlerFunc, ignoreMethods []string) *mux.Router {
    12  	m := mux.NewRouter()
    13  	for methodName, call := range inst.regMethods.reg {
    14  		if arrayContainsString(ignoreMethods, methodName) {
    15  			continue
    16  		}
    17  		if call.Endpoint == qhttp.DenyHTTP {
    18  			continue
    19  		}
    20  		handler := middleware(NewHTTPRequestHandler(inst, methodName))
    21  		// All endpoints use POST verb
    22  		httpVerb := http.MethodPost
    23  		m.Handle(string(call.Endpoint), handler).Methods(httpVerb, http.MethodOptions)
    24  	}
    25  	return m
    26  }
    27  
    28  func arrayContainsString(searchSpace []string, target string) bool {
    29  	for _, elem := range searchSpace {
    30  		if elem == target {
    31  			return true
    32  		}
    33  	}
    34  	return false
    35  }