🎨 Format of the source code

This commit is contained in:
Maxim Lebedev 2023-08-05 08:50:16 +06:00
parent afb801b6fa
commit 98576889a2
Signed by: toby3d
GPG key ID: 1F14E25B7C119FC5
14 changed files with 72 additions and 92 deletions

View file

@ -45,7 +45,7 @@ func NewHandler(opts NewHandlerOptions) *Handler {
}
}
func (h *Handler) Handler() http.Handler {
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
chain := middleware.Chain{
middleware.CSRFWithConfig(middleware.CSRFConfig{
Skipper: func(_ http.ResponseWriter, r *http.Request) bool {
@ -68,8 +68,7 @@ func (h *Handler) Handler() http.Handler {
Skipper: func(_ http.ResponseWriter, r *http.Request) bool {
head, _ := urlutil.ShiftPath(r.URL.Path)
return r.Method != http.MethodPost ||
head != "verify" ||
return r.Method != http.MethodPost || head != "verify" ||
r.PostFormValue("authorize") == "deny"
},
Validator: func(_ http.ResponseWriter, _ *http.Request, login, password string) (bool, error) {
@ -84,31 +83,29 @@ func (h *Handler) Handler() http.Handler {
}),
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
head, _ := urlutil.ShiftPath(r.URL.Path)
head, _ := urlutil.ShiftPath(r.URL.Path)
switch r.Method {
default:
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
case http.MethodGet, "":
if head != "" {
http.NotFound(w, r)
switch r.Method {
default:
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
case http.MethodGet, "":
if head != "" {
http.NotFound(w, r)
return
}
chain.Handler(h.handleAuthorize).ServeHTTP(w, r)
case http.MethodPost:
switch head {
default:
http.NotFound(w, r)
case "":
chain.Handler(h.handleExchange).ServeHTTP(w, r)
case "verify":
chain.Handler(h.handleVerify).ServeHTTP(w, r)
}
return
}
})
chain.Handler(h.handleAuthorize).ServeHTTP(w, r)
case http.MethodPost:
switch head {
default:
http.NotFound(w, r)
case "":
chain.Handler(h.handleExchange).ServeHTTP(w, r)
case "verify":
chain.Handler(h.handleVerify).ServeHTTP(w, r)
}
}
}
func (h *Handler) handleAuthorize(w http.ResponseWriter, r *http.Request) {

View file

@ -85,7 +85,7 @@ func TestAuthorize(t *testing.T) {
Clients: deps.clientService,
Config: *deps.config,
Matcher: deps.matcher,
}).Handler().ServeHTTP(w, req)
}).ServeHTTP(w, req)
resp := w.Result()

View file

@ -39,26 +39,24 @@ func NewHandler(opts NewHandlerOptions) *Handler {
}
}
func (h *Handler) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "" && r.Method != http.MethodGet {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "" && r.Method != http.MethodGet {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
return
}
var head string
head, r.URL.Path = urlutil.ShiftPath(r.URL.Path)
var head string
head, r.URL.Path = urlutil.ShiftPath(r.URL.Path)
switch head {
default:
http.NotFound(w, r)
case "":
h.handleRender(w, r)
case "callback":
h.handleCallback(w, r)
}
})
switch head {
default:
http.NotFound(w, r)
case "":
h.handleRender(w, r)
case "callback":
h.handleCallback(w, r)
}
}
func (h *Handler) handleRender(w http.ResponseWriter, r *http.Request) {

View file

@ -41,7 +41,7 @@ func TestRead(t *testing.T) {
Config: *deps.config,
Matcher: deps.matcher,
Tokens: deps.tokenService,
}).Handler().ServeHTTP(w, req)
}).ServeHTTP(w, req)
if resp := w.Result(); resp.StatusCode != http.StatusOK {
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode, http.StatusOK)

View file

@ -13,11 +13,7 @@ func NewHandler() *Handler {
return &Handler{}
}
func (h *Handler) Handler() http.Handler {
return http.HandlerFunc(h.handleFunc)
}
func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(common.HeaderContentType, common.MIMETextPlainCharsetUTF8)
fmt.Fprint(w, `👌`)
w.WriteHeader(http.StatusOK)

View file

@ -16,7 +16,6 @@ func TestRequestHandler(t *testing.T) {
w := httptest.NewRecorder()
delivery.NewHandler().
Handler().
ServeHTTP(w, req)
resp := w.Result()

View file

@ -19,11 +19,7 @@ func NewHandler(metadata *domain.Metadata) *Handler {
}
}
func (h *Handler) Handler() http.Handler {
return http.HandlerFunc(h.handleFunc)
}
func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "" && r.Method != http.MethodGet {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)

View file

@ -20,7 +20,6 @@ func TestMetadata(t *testing.T) {
w := httptest.NewRecorder()
delivery.NewHandler(metadata).
Handler().
ServeHTTP(w, req)
resp := w.Result()

View file

@ -32,7 +32,7 @@ func NewHandler(tickets ticket.UseCase, matcher language.Matcher, config domain.
}
}
func (h *Handler) Handler() http.Handler {
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//nolint:exhaustivestruct
chain := middleware.Chain{
middleware.CSRFWithConfig(middleware.CSRFConfig{
@ -70,7 +70,7 @@ func (h *Handler) Handler() http.Handler {
}),
}
return chain.Handler(h.handleFunc)
chain.Handler(h.handleFunc).ServeHTTP(w, r)
}
func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {

View file

@ -28,7 +28,7 @@ func NewHandler(tokens token.UseCase, tickets ticket.UseCase, config *domain.Con
}
}
func (h *Handler) Handler() http.Handler {
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
chain := middleware.Chain{
//nolint:exhaustivestruct
middleware.JWTWithConfig(middleware.JWTConfig{
@ -45,27 +45,25 @@ func (h *Handler) Handler() http.Handler {
}),
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
return
}
var head string
head, r.URL.Path = urlutil.ShiftPath(r.URL.Path)
var head string
head, r.URL.Path = urlutil.ShiftPath(r.URL.Path)
switch head {
default:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
case "token":
chain.Handler(h.handleAction).ServeHTTP(w, r)
case "introspect":
chain.Handler(h.handleIntrospect).ServeHTTP(w, r)
case "revocation":
chain.Handler(h.handleRevokation).ServeHTTP(w, r)
}
})
switch head {
default:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
case "token":
chain.Handler(h.handleAction).ServeHTTP(w, r)
case "introspect":
chain.Handler(h.handleIntrospect).ServeHTTP(w, r)
case "revocation":
chain.Handler(h.handleRevokation).ServeHTTP(w, r)
}
}
func (h *Handler) handleIntrospect(w http.ResponseWriter, r *http.Request) {

View file

@ -56,7 +56,6 @@ func TestIntrospection(t *testing.T) {
w := httptest.NewRecorder()
delivery.NewHandler(deps.tokenService, deps.ticketService, deps.config).
Handler().
ServeHTTP(w, req)
resp := w.Result()
@ -91,7 +90,6 @@ func TestRevocation(t *testing.T) {
w := httptest.NewRecorder()
delivery.NewHandler(deps.tokenService, deps.ticketService, deps.config).
Handler().
ServeHTTP(w, req)
resp := w.Result()

View file

@ -25,7 +25,7 @@ func NewHandler(tokens token.UseCase, config *domain.Config) *Handler {
}
}
func (h *Handler) Handler() http.Handler {
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
chain := middleware.Chain{
//nolint:exhaustivestruct
middleware.JWTWithConfig(middleware.JWTConfig{
@ -38,7 +38,7 @@ func (h *Handler) Handler() http.Handler {
}),
}
return chain.Handler(h.handleFunc)
chain.Handler(h.handleFunc).ServeHTTP(w, r)
}
func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {

View file

@ -44,7 +44,6 @@ func TestUserInfo(t *testing.T) {
w := httptest.NewRecorder()
delivery.NewHandler(deps.tokenService, deps.config).
Handler().
ServeHTTP(w, req)
resp := w.Result()

20
main.go
View file

@ -324,37 +324,37 @@ func (app *App) Handler() http.Handler {
domain.CodeChallengeMethodS512,
},
AuthorizationResponseIssParameterSupported: true,
}).Handler()
health := healthhttpdelivery.NewHandler().Handler()
})
health := healthhttpdelivery.NewHandler()
auth := authhttpdelivery.NewHandler(authhttpdelivery.NewHandlerOptions{
Auth: app.auth,
Clients: app.clients,
Config: *config,
Matcher: app.matcher,
Profiles: app.profiles,
}).Handler()
token := tokenhttpdelivery.NewHandler(app.tokens, app.tickets, config).Handler()
})
token := tokenhttpdelivery.NewHandler(app.tokens, app.tickets, config)
client := clienthttpdelivery.NewHandler(clienthttpdelivery.NewHandlerOptions{
Client: *indieAuthClient,
Config: *config,
Matcher: app.matcher,
Tokens: app.tokens,
}).Handler()
user := userhttpdelivery.NewHandler(app.tokens, config).Handler()
ticket := tickethttpdelivery.NewHandler(app.tickets, app.matcher, *config).Handler()
})
user := userhttpdelivery.NewHandler(app.tokens, config)
ticket := tickethttpdelivery.NewHandler(app.tickets, app.matcher, *config)
staticHandler := http.FileServer(http.FS(app.static))
return http.HandlerFunc(middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
head, tail := urlutil.ShiftPath(r.URL.Path)
switch head {
default:
default: // NOTE(toby3d): static or 404
staticHandler.ServeHTTP(w, r)
case "", "callback":
case "", "callback": // NOTE(toby3d): self-client
client.ServeHTTP(w, r)
case "token", "introspect", "revocation":
token.ServeHTTP(w, r)
case ".well-known":
case ".well-known": // NOTE(toby3d): public server config
r.URL.Path = tail
if head, _ = urlutil.ShiftPath(r.URL.Path); head == "oauth-authorization-server" {