package
0.0.0-20221201201751-bff38ad73e9e
Repository: https://github.com/scottcagno/go-scratch.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

When you write software, there are two
kinds of problems that you run into...

  • Problems that stretch your fundamental knowledge
    of how things work and as a result of solving them
    you become one step closer to unlocking the secrets
    to immortality and transcending beyond mere human
    limitations.
  • Exceedingly stupid typos that static analysis tools
    can't be taught how to catch and thus dooms humans
    to feel like they wasted so much time on something
    so trivial.
  • Off-by-one errors.
// APIServer can be used to create API's
type APIServer struct {
base string
resc map[string]Resource
m *http.ServeMux
}

// NewAPIServer creates and returns a new server instance
func NewAPIServer(base string) *APIServer {
return &APIServer{
base: base,
resc: make(map[string]Resource),
m: http.NewServeMux(),
}
}

// RegisterResource registers a `Resource` with the server
func (srv *APIServer) RegisterResource(name string, re Resource) {
srv.m.Handle(re.Path(), re)
srv.resc[name] = re
}

// ServeHTTP is the APIServer's default handler
func (srv *APIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// check for options request
if r.Method == http.MethodOptions {
handleOptions(w, r)
return
}
rh, found := lookupResourceHandler(r)
if !found {
handleNotFound(w, r)
return
}
switch apiRequestType(r) {
case apiReturnAll:
rh.returnAll(w, r)
case apiReturnOne:
rh.returnOne(w, r)
case apiInsertOne:
rh.insertOne(w, r)
case apiUpdateOne:
rh.updateOne(w, r)
case apiDeleteOne:
rh.deleteOne(w, r)
default:
handleBadRequest(w, r)
}
}

// ServeHTTP is the APIServer's default handler
func (srv *APIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// lookup resource handler
rh, found := lookupResourceHandler(r)
if !found {
handleNotFound(w, r)
return
}
// call default handler of the resource handler
rh.ServeHTTP(w, r)
return
}