|
@ -4,6 +4,8 @@ import ( |
|
|
"net/http" |
|
|
"net/http" |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"git.aiterp.net/gisle/wrouter/response" |
|
|
|
|
|
|
|
|
"git.aiterp.net/gisle/wrouter/auth" |
|
|
"git.aiterp.net/gisle/wrouter/auth" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
@ -16,10 +18,13 @@ type Resource struct { |
|
|
get IDFunc |
|
|
get IDFunc |
|
|
update IDFunc |
|
|
update IDFunc |
|
|
delete IDFunc |
|
|
delete IDFunc |
|
|
|
|
|
|
|
|
|
|
|
PrivateRead bool |
|
|
|
|
|
PrivateWrite bool |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func NewResource(list, create Func, get, update, delete IDFunc) *Resource { |
|
|
func NewResource(list, create Func, get, update, delete IDFunc) *Resource { |
|
|
return &Resource{list, create, get, update, delete} |
|
|
|
|
|
|
|
|
return &Resource{list, create, get, update, delete, false, false} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (resource *Resource) Handle(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { |
|
|
func (resource *Resource) Handle(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { |
|
@ -41,6 +46,11 @@ func (resource *Resource) Handle(path string, w http.ResponseWriter, req *http.R |
|
|
switch req.Method { |
|
|
switch req.Method { |
|
|
case "GET": |
|
|
case "GET": |
|
|
{ |
|
|
{ |
|
|
|
|
|
if resource.PrivateRead && user == nil { |
|
|
|
|
|
response.Text(w, 401, "Not logged in") |
|
|
|
|
|
return true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if subpath != "" { |
|
|
if subpath != "" { |
|
|
resource.get(w, req, subpath, user) |
|
|
resource.get(w, req, subpath, user) |
|
|
} else { |
|
|
} else { |
|
@ -49,10 +59,13 @@ func (resource *Resource) Handle(path string, w http.ResponseWriter, req *http.R |
|
|
} |
|
|
} |
|
|
case "POST": |
|
|
case "POST": |
|
|
{ |
|
|
{ |
|
|
|
|
|
if resource.PrivateWrite && user == nil { |
|
|
|
|
|
response.Text(w, 401, "Not logged in") |
|
|
|
|
|
return true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if subpath != "" { |
|
|
if subpath != "" { |
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
|
|
|
|
|
w.WriteHeader(400) |
|
|
|
|
|
w.Write([]byte("ID not allowed in POST")) |
|
|
|
|
|
|
|
|
response.Text(w, 400, "ID not allowed in POST") |
|
|
return true |
|
|
return true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -60,10 +73,20 @@ func (resource *Resource) Handle(path string, w http.ResponseWriter, req *http.R |
|
|
} |
|
|
} |
|
|
case "PATCH", "PUT": |
|
|
case "PATCH", "PUT": |
|
|
{ |
|
|
{ |
|
|
|
|
|
if resource.PrivateWrite && user == nil { |
|
|
|
|
|
response.Text(w, 401, "Not logged in") |
|
|
|
|
|
return true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
resource.update(w, req, subpath, user) |
|
|
resource.update(w, req, subpath, user) |
|
|
} |
|
|
} |
|
|
case "DELETE": |
|
|
case "DELETE": |
|
|
{ |
|
|
{ |
|
|
|
|
|
if resource.PrivateWrite && user == nil { |
|
|
|
|
|
response.Text(w, 401, "Not logged in") |
|
|
|
|
|
return true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
resource.delete(w, req, subpath, user) |
|
|
resource.delete(w, req, subpath, user) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|