2023-10-01 04:17:10 +00:00
|
|
|
package account_id
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-08-02 00:50:27 +00:00
|
|
|
"github.com/reiver/go-pathmatch"
|
2023-10-01 09:26:22 +00:00
|
|
|
|
2024-08-01 22:15:34 +00:00
|
|
|
"github.com/reiver/go-mstdn"
|
|
|
|
"github.com/reiver/go-mstdn/ent"
|
2023-10-01 04:17:10 +00:00
|
|
|
)
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
var _ http.Handler = internalHandler{}
|
2023-10-01 04:17:10 +00:00
|
|
|
|
|
|
|
const Path string = "/api/v1/accounts/{account_id}"
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
func (internalHandler) Path() string {
|
2023-10-01 04:17:10 +00:00
|
|
|
return Path
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:26:22 +00:00
|
|
|
path := url.Path
|
|
|
|
|
|
|
|
success, err := pattern.Find(path, &accountid)
|
|
|
|
if nil != err {
|
|
|
|
mstdn.BadRequest(resp)
|
2023-10-01 04:17:10 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-01 09:26:22 +00:00
|
|
|
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)
|
|
|
|
}
|