go-mstdn/api/v1/accounts/account_id/handler.go

86 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-10-01 04:17:10 +00:00
package account_id
import (
"net/http"
"github.com/reiver/go-pathmatch"
"github.com/reiver/go-mstdn"
"github.com/reiver/go-mstdn/ent"
2023-10-01 04:17:10 +00:00
)
var _ http.Handler = internalHandler{}
2023-10-01 04:17:10 +00:00
const Path string = "/api/v1/accounts/{account_id}"
var pattern *pathmatch.Pattern = pathmatch.MustCompile(Path)
func Handler(fn LoaderFunc) http.Handler {
return internalHandler{
loaderFunc:fn,
}
}
type internalHandler struct {
loaderFunc LoaderFunc
2023-10-01 04:17:10 +00:00
}
func (internalHandler) Path() string {
2023-10-01 04:17:10 +00:00
return Path
}
func (receiver internalHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
2023-10-01 04:17:10 +00:00
if nil == resp {
return
}
if nil == req {
mstdn.InternalServerError(resp)
return
}
if http.MethodGet != req.Method {
mstdn.MethodNotAllowed(resp)
return
}
fn := receiver.loaderFunc
2023-10-01 04:17:10 +00:00
if nil == fn {
mstdn.InternalServerError(resp)
return
}
var accountid string
{
url := req.URL
if nil == url {
mstdn.InternalServerError(resp)
return
}
path := url.Path
success, err := pattern.Find(path, &accountid)
if nil != err {
mstdn.BadRequest(resp)
2023-10-01 04:17:10 +00:00
return
}
if !success {
2023-10-01 04:17:10 +00:00
mstdn.BadRequest(resp)
return
}
}
var account ent.Account
{
err := fn(&account, accountid)
if nil != err {
mstdn.Error(resp, err.ErrHTTP())
return
}
}
mstdn.OK(resp, account)
}