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

85 lines
1.2 KiB
Go
Raw Normal View History

2023-10-01 04:18:05 +00:00
package lookup
import (
"net/http"
"github.com/reiver/go-mstdn"
"github.com/reiver/go-mstdn/ent"
2023-10-01 04:18:05 +00:00
)
var _ http.Handler = internalHandler{}
2023-10-01 04:18:05 +00:00
const Path string = "/api/v1/accounts/lookup"
func Handler(fn LoaderFunc) http.Handler {
return internalHandler{
loaderFunc:fn,
}
}
type internalHandler struct {
loaderFunc LoaderFunc
2023-10-01 04:18:05 +00:00
}
func (internalHandler) Path() string {
2023-10-01 04:18:05 +00:00
return Path
}
func (receiver internalHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
2023-10-01 04:18:05 +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:18:05 +00:00
if nil == fn {
mstdn.InternalServerError(resp)
return
}
var acct string
{
const key string = "acct"
url := req.URL
if nil == url {
mstdn.InternalServerError(resp)
return
}
query := url.Query()
if nil == query {
mstdn.InternalServerError(resp)
return
}
if !query.Has(key) {
mstdn.BadRequest(resp)
return
}
acct = query.Get("acct")
}
var account ent.Account
{
err := fn(&account, acct)
if nil != err {
mstdn.Error(resp, err.ErrHTTP())
return
}
}
mstdn.OK(resp, account)
}