aicra/http.go

108 lines
2.3 KiB
Go
Raw Normal View History

package aicra
import (
"log"
"net/http"
"git.xdrm.io/go/aicra/api"
"git.xdrm.io/go/aicra/internal/reqdata"
)
// httpServer wraps the aicra server to allow handling http requests
type httpServer Server
// ServeHTTP implements http.Handler and has to be called on each request
2020-03-21 13:49:36 +00:00
func (server httpServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
// 1. find a matching service in the config
service := server.config.Find(req)
if service == nil {
response := api.NewResponse(api.ErrorUnknownService())
response.ServeHTTP(res, req)
logError(response)
return
}
2020-03-21 13:49:36 +00:00
// 2. build input parameter receiver
dataset := reqdata.New(service)
2020-03-21 13:49:36 +00:00
// 3. extract URI data
err := dataset.ExtractURI(req)
if err != nil {
response := api.NewResponse(api.ErrorMissingParam())
response.ServeHTTP(res, req)
logError(response)
return
}
2020-03-21 13:49:36 +00:00
// 4. extract query data
err = dataset.ExtractQuery(req)
if err != nil {
response := api.NewResponse(api.ErrorMissingParam())
response.ServeHTTP(res, req)
logError(response)
return
}
2020-03-21 13:49:36 +00:00
// 5. extract form/json data
err = dataset.ExtractForm(req)
if err != nil {
response := api.NewResponse(api.ErrorMissingParam())
response.ServeHTTP(res, req)
logError(response)
return
}
2020-03-21 13:49:36 +00:00
// 6. find a matching handler
var foundHandler *api.Handler
var found bool
for _, handler := range server.handlers {
2020-03-21 13:49:36 +00:00
if handler.GetMethod() == service.Method && handler.GetPath() == service.Pattern {
found = true
}
}
2020-03-21 13:49:36 +00:00
// 7. fail if found no handler
if foundHandler == nil {
if found {
2020-03-21 13:49:36 +00:00
r := api.NewResponse()
r.SetError(api.ErrorUncallableService(), service.Method, service.Pattern)
r.ServeHTTP(res, req)
logError(r)
return
}
2020-03-21 13:49:36 +00:00
r := api.NewResponse()
r.SetError(api.ErrorUnknownService(), service.Method, service.Pattern)
r.ServeHTTP(res, req)
logError(r)
return
}
2020-03-21 13:49:36 +00:00
// 8. build api.Request from http.Request
apireq, err := api.NewRequest(req)
if err != nil {
log.Fatal(err)
}
2020-03-21 13:49:36 +00:00
// 9. feed request with scope & parameters
apireq.Scope = service.Scope
apireq.Param = dataset.Data
2020-03-21 13:49:36 +00:00
// 10. execute
response := api.NewResponse()
foundHandler.Handle(*apireq, response)
// 11. apply headers
for key, values := range response.Headers {
for _, value := range values {
2020-03-21 13:49:36 +00:00
res.Header().Add(key, value)
}
}
2020-03-21 13:49:36 +00:00
// 12. write to response
response.ServeHTTP(res, req)
}