snap/service.go

95 lines
2.0 KiB
Go
Raw Permalink Normal View History

2023-05-29 20:21:08 +00:00
package snap
import (
"io"
"net/http"
"github.com/gorilla/mux"
)
type ServiceStatus struct {
Message string
}
type ServiceResponse struct {
Code int
Message []byte
}
func (s *ServiceResponse) WriteResponse(w http.ResponseWriter) {
w.WriteHeader(s.Code)
w.Write(s.Message)
}
func (s *Server) serviceWrapper(handle func(c *ServiceRequest) *ServiceResponse) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := &ServiceRequest{
r: r,
}
if s.auth != nil {
if rec, code := s.auth.DoAuth(w, r); code == http.StatusOK {
c.auth = rec
}
}
resp := handle(c)
// discard the rest of the body content
2023-08-18 03:30:47 +00:00
io.Copy(io.Discard, r.Body)
2023-05-29 20:21:08 +00:00
defer r.Body.Close()
resp.WriteResponse(w)
}
}
func (s *Server) serviceWrapperAuthenticated(handle func(c *ServiceRequest) *ServiceResponse) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
2023-08-18 03:30:47 +00:00
io.Copy(io.Discard, r.Body)
2023-05-29 20:21:08 +00:00
r.Body.Close()
}()
if s.auth == nil {
resp := ServiceResponse{
Code: http.StatusForbidden,
Message: []byte("Not Authenticated"),
}
resp.WriteResponse(w)
return
}
rec, code := s.auth.DoAuth(w, r)
switch code {
case http.StatusOK:
c := &ServiceRequest{
r: r,
auth: rec,
}
resp := handle(c)
resp.WriteResponse(w)
return
case http.StatusUnauthorized:
resp := ServiceResponse{
Code: http.StatusForbidden,
Message: []byte("Not Authenticated"),
}
resp.WriteResponse(w)
return
default:
resp := ServiceResponse{
Code: code,
Message: []byte(http.StatusText(code)),
}
resp.WriteResponse(w)
return
}
}
}
func (s *Server) HandleServiceFunc(path string, f func(c *ServiceRequest) *ServiceResponse) *mux.Route {
return s.router.HandleFunc(path, s.serviceWrapper(f))
}
func (s *Server) HandleServiceFuncWithAuth(path string, f func(c *ServiceRequest) *ServiceResponse) *mux.Route {
return s.router.HandleFunc(path, s.serviceWrapperAuthenticated(f))
}