From 116a2fe687f5c37f2b10b47bfad47f2fa5ab257d Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sun, 1 Oct 2023 18:26:22 +0900 Subject: [PATCH] /v1/accounts/look /v1/accounts/{account_id} --- api/v1/accounts/account_id/handler.go | 39 ++++++++++++++++----------- api/v1/accounts/lookup/handler.go | 18 ++++++++----- go.mod | 13 +++++++++ go.sum | 38 ++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/api/v1/accounts/account_id/handler.go b/api/v1/accounts/account_id/handler.go index ff4ebf3..f77e348 100644 --- a/api/v1/accounts/account_id/handler.go +++ b/api/v1/accounts/account_id/handler.go @@ -3,23 +3,33 @@ package account_id import ( "net/http" + "sourcecode.social/reiver/go-pathmatch" + "sourcecode.social/reiver/go-mstdn" "sourcecode.social/reiver/go-mstdn/ent" ) -var _ http.Handler = Handler{} +var _ http.Handler = internalHandler{} const Path string = "/api/v1/accounts/{account_id}" -type Handler struct { - LoaderFunc LoaderFunc +var pattern *pathmatch.Pattern = pathmatch.MustCompile(Path) + +func Handler(fn LoaderFunc) http.Handler { + return internalHandler{ + loaderFunc:fn, + } } -func (Handler) Path() string { +type internalHandler struct { + loaderFunc LoaderFunc +} + +func (internalHandler) Path() string { return Path } -func (receiver Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { +func (receiver internalHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { if nil == resp { return } @@ -34,7 +44,7 @@ func (receiver Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { return } - fn := receiver.LoaderFunc + fn := receiver.loaderFunc if nil == fn { mstdn.InternalServerError(resp) return @@ -42,26 +52,23 @@ func (receiver Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { var accountid string { - const key string = "accountid" - url := req.URL if nil == url { mstdn.InternalServerError(resp) return } - query := url.Query() - if nil == query { - mstdn.InternalServerError(resp) - return - } + path := url.Path - if !query.Has(key) { + success, err := pattern.Find(path, &accountid) + if nil != err { + mstdn.BadRequest(resp) + return + } + if !success { mstdn.BadRequest(resp) return } - - accountid = query.Get("accountid") } var account ent.Account diff --git a/api/v1/accounts/lookup/handler.go b/api/v1/accounts/lookup/handler.go index 49dbe4f..99dc3f2 100644 --- a/api/v1/accounts/lookup/handler.go +++ b/api/v1/accounts/lookup/handler.go @@ -7,19 +7,25 @@ import ( "sourcecode.social/reiver/go-mstdn/ent" ) -var _ http.Handler = Handler{} +var _ http.Handler = internalHandler{} const Path string = "/api/v1/accounts/lookup" -type Handler struct { - LoaderFunc LoaderFunc +func Handler(fn LoaderFunc) http.Handler { + return internalHandler{ + loaderFunc:fn, + } } -func (Handler) Path() string { +type internalHandler struct { + loaderFunc LoaderFunc +} + +func (internalHandler) Path() string { return Path } -func (receiver Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { +func (receiver internalHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { if nil == resp { return } @@ -34,7 +40,7 @@ func (receiver Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { return } - fn := receiver.LoaderFunc + fn := receiver.loaderFunc if nil == fn { mstdn.InternalServerError(resp) return diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0272556 --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module sourcecode.social/reiver/go-mstdn + +go 1.20 + +require ( + sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 // indirect + sourcecode.social/reiver/go-jsonint v0.0.0-20230924212905-09e0626e3dd8 // indirect + sourcecode.social/reiver/go-jsonpp v0.0.0-20230927044614-2bd26d25b35e // indirect + sourcecode.social/reiver/go-jsonstr v0.0.0-20230927032837-cca9187c9798 // indirect + sourcecode.social/reiver/go-nul v0.0.0-20230927004815-8a8eeb3194fa // indirect + sourcecode.social/reiver/go-opt v0.0.0-20230927004316-272fd8451811 // indirect + sourcecode.social/reiver/go-pathmatch v0.0.0-20231001044920-1772a36eb6bd // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7d1fa93 --- /dev/null +++ b/go.sum @@ -0,0 +1,38 @@ +sourcecode.social/reiver/go-erorr v0.0.0-20230922194705-6dcd8d352220 h1:SOCgtgGppEFZZZNKMSMhALmtT9FD1ChUzS5JFlhNhBc= +sourcecode.social/reiver/go-erorr v0.0.0-20230922194705-6dcd8d352220/go.mod h1:NFtd7fzEf0r6A6R7JXYZfayRhPaJy0zt/18VWoLzrxA= +sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 h1:wpnz4JicQBLWrgGphYBls7DysIFCcnWgDz/vce/sY8E= +sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1/go.mod h1:NFtd7fzEf0r6A6R7JXYZfayRhPaJy0zt/18VWoLzrxA= +sourcecode.social/reiver/go-jsonint v0.0.0-20230924185016-b499a1e14425 h1:3hEByqgGPWYmh89OAuYA6k4rdkTF7fobPgHAjQOzoq4= +sourcecode.social/reiver/go-jsonint v0.0.0-20230924185016-b499a1e14425/go.mod h1:PpaukJtakk0O45CurQVADzvpu4/2y0n1tc0j1dG1bVw= +sourcecode.social/reiver/go-jsonint v0.0.0-20230924212905-09e0626e3dd8 h1:Qf3OP8Z89a6t4M6qwF7q4+DYhU93rPFPJ1sl+bxesME= +sourcecode.social/reiver/go-jsonint v0.0.0-20230924212905-09e0626e3dd8/go.mod h1:PpaukJtakk0O45CurQVADzvpu4/2y0n1tc0j1dG1bVw= +sourcecode.social/reiver/go-jsonpp v0.0.0-20230927044614-2bd26d25b35e h1:hZJldAi9A94yjuyu7YzTH1MuBWQuPMbdM/aPFfow+zk= +sourcecode.social/reiver/go-jsonpp v0.0.0-20230927044614-2bd26d25b35e/go.mod h1:Wk0eR28gjwBkTH+6yv6lKOjoyqCntSOgQJJksJE1xc8= +sourcecode.social/reiver/go-jsonstr v0.0.0-20230927001516-160f1f17699f h1:W6wVDR+GKH2PW4z0LuyMxD+v2R8+FlE9HHlF4aYdgeU= +sourcecode.social/reiver/go-jsonstr v0.0.0-20230927001516-160f1f17699f/go.mod h1:K97qQnuRcjjh2OjhT2BzliIZMm8hLp4qIx8arkV/tnU= +sourcecode.social/reiver/go-jsonstr v0.0.0-20230927032837-cca9187c9798 h1:yznt9jlfBogqkRvseZtzkeEA1BMktlveNVjDyoL0PSA= +sourcecode.social/reiver/go-jsonstr v0.0.0-20230927032837-cca9187c9798/go.mod h1:K97qQnuRcjjh2OjhT2BzliIZMm8hLp4qIx8arkV/tnU= +sourcecode.social/reiver/go-nul v0.0.0-20230924084858-46372d9e9949 h1:iAa2FSCR1CzOyWQgL6eUNIaII7ui87V0IOQTu4uYulU= +sourcecode.social/reiver/go-nul v0.0.0-20230924084858-46372d9e9949/go.mod h1:ZG+UaSVwpMEQr0faWJltKDXREDtzme3wCE2nutu7Tgw= +sourcecode.social/reiver/go-nul v0.0.0-20230924085205-da3d80e27a75 h1:u71gKZe6g4hPnSe7PkpkBd7zHKk6I/udCckdKG1xIiI= +sourcecode.social/reiver/go-nul v0.0.0-20230924085205-da3d80e27a75/go.mod h1:y59WFulHG1OigANht9QiVXSa5exV2bqt72RY3LmDstI= +sourcecode.social/reiver/go-nul v0.0.0-20230927004815-8a8eeb3194fa h1:ws1aQ5mMDvBbKEEk0UulTN4Mmszcs6pn4CgMcTLVkv0= +sourcecode.social/reiver/go-nul v0.0.0-20230927004815-8a8eeb3194fa/go.mod h1:4WTJneAzve27LLCE8+HRpFUIjuB3hgvZkNtlS43xbKw= +sourcecode.social/reiver/go-opt v0.0.0-20230922204021-df716e8fe048 h1:/MDewF+utMeOhLwYZmXA8GSy5Jma4/iq0fv1J+1vJT0= +sourcecode.social/reiver/go-opt v0.0.0-20230922204021-df716e8fe048/go.mod h1:SEWWqfbCkDQCL0mpqwZEJQQkqLzhJZs1k4EmJ9xPU0E= +sourcecode.social/reiver/go-opt v0.0.0-20230922223642-a33304548a94 h1:lk/e2W5McoYglMfWga0gk1YzNJYnqWA660Si4bnEwS4= +sourcecode.social/reiver/go-opt v0.0.0-20230922223642-a33304548a94/go.mod h1:SEWWqfbCkDQCL0mpqwZEJQQkqLzhJZs1k4EmJ9xPU0E= +sourcecode.social/reiver/go-opt v0.0.0-20230923002941-58bf264aa928 h1:FAQLe45K5yEDL/brV1DZN5TAJYHNgGq0EnSDFe4xTow= +sourcecode.social/reiver/go-opt v0.0.0-20230923002941-58bf264aa928/go.mod h1:SEWWqfbCkDQCL0mpqwZEJQQkqLzhJZs1k4EmJ9xPU0E= +sourcecode.social/reiver/go-opt v0.0.0-20230923212734-e38043bd8901 h1:mMuaCYrl0WklX8gDaA+WvPmXFMs2YhdbgYmW3E8A36M= +sourcecode.social/reiver/go-opt v0.0.0-20230923212734-e38043bd8901/go.mod h1:SEWWqfbCkDQCL0mpqwZEJQQkqLzhJZs1k4EmJ9xPU0E= +sourcecode.social/reiver/go-opt v0.0.0-20230924215954-cb749d3510af h1:1mjNkZavEsZJrxmCVWLFw/rMLXY1fV4nX3eXkSjeo4w= +sourcecode.social/reiver/go-opt v0.0.0-20230924215954-cb749d3510af/go.mod h1:SEWWqfbCkDQCL0mpqwZEJQQkqLzhJZs1k4EmJ9xPU0E= +sourcecode.social/reiver/go-opt v0.0.0-20230924220429-d6b2890eaa29 h1:93+N7APTX4WiA5f/1BtEYBN+uf4cFzhjudPOh5p1XJg= +sourcecode.social/reiver/go-opt v0.0.0-20230924220429-d6b2890eaa29/go.mod h1:SEWWqfbCkDQCL0mpqwZEJQQkqLzhJZs1k4EmJ9xPU0E= +sourcecode.social/reiver/go-opt v0.0.0-20230925023513-3e7cdb405e4f h1:Cp1Bz9qPPPMSQgcf79grTiPO2vnK+ILydgtBoI0HFu8= +sourcecode.social/reiver/go-opt v0.0.0-20230925023513-3e7cdb405e4f/go.mod h1:SEWWqfbCkDQCL0mpqwZEJQQkqLzhJZs1k4EmJ9xPU0E= +sourcecode.social/reiver/go-opt v0.0.0-20230927004316-272fd8451811 h1:T3ft05KUJpzkimaJXiNSpBkYJoRmyinvtrq8MbrfrFQ= +sourcecode.social/reiver/go-opt v0.0.0-20230927004316-272fd8451811/go.mod h1:O6WKM2UcKkheRT/dA6A2b1tW0m+WenSbxdcXE+idxzI= +sourcecode.social/reiver/go-pathmatch v0.0.0-20231001044920-1772a36eb6bd h1:6MIrZc2s6m4SaHnP2Aza6Cg4kMBiwpi+8mKdO9sjfhw= +sourcecode.social/reiver/go-pathmatch v0.0.0-20231001044920-1772a36eb6bd/go.mod h1:mw0k5AKQBwJ75SIP6sm9HWCSkEVxqn9UlHDH+MR8pjo=