Telegram Group & Telegram Channel
📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/golang_books/975
Create:
Last Update:

📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books

BY Golang Books


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_books/975

View MORE
Open in Telegram


Golang Books Telegram | DID YOU KNOW?

Date: |

Telegram today rolling out an update which brings with it several new features.The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations.

Golang Books from fr


Telegram Golang Books
FROM USA