#native routing with the http package
13 messages · Page 1 of 1 (latest)
Avoid sending an image for code, it's less readable and difficult for users to copy and modify to better help you.
Instead, paste the code using DIscord's markdown format:
```go
// your code here
```
Note: these are backticks (`) and not single quotes (')
http://idownvotedbecau.se/imageofcode
oh sorry!!
heres the code:
func Init() http.Handler {
defer func() {
if r := recover(); r != nil {
logger.Error(fmt.Sprintf("Failed to initialize routes: %v", r))
}
}()
logger.Info("Initializing routes")
var (
bcryptAdapter crypto.Crypto = crypto.NewBcryptAdapter()
postgresAdapter database.Database = database.NewPostgresAdapter()
jwtAdapter jwt.JWT = jwt.NewJWTAdapter()
loggingMiddleware middlewares.Middleware = middlewares.LoggingMiddleware
bearerAuthMiddleware middlewares.Middleware = middlewares.BearerMiddleware(jwtAdapter)
basicAuthMiddleware middlewares.Middleware = middlewares.BasicMiddleware
)
authRepository := repository.NewAuthRepository(postgresAdapter)
authService := service.NewAuthService(
authRepository,
bcryptAdapter,
)
authController := controllers.NewAuthController(authService)
r := http.NewServeMux()
{
r.Handle("/auth/signin", middlewares.CreateStack([]middlewares.Middleware{basicAuthMiddleware}, http.HandlerFunc(authController.SignIn)))
r.Handle("/auth/refresh-token", middlewares.CreateStack([]middlewares.Middleware{bearerAuthMiddleware}, http.HandlerFunc(authController.RefreshToken)))
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
r.Handle("/", http.NotFoundHandler())
}
return loggingMiddleware(r)
}
what's the extra {} block for (the r.Handle one) ?
Also why not use := normally instead of the semi odd var () block? inside a function that's rarely used
And I don't think you really want that recover... if one of your handler panics... the server is probably not functional and should abort
that createstack seems unecessary painful too for a single element
i was trying to subroute with the documentation and various http.newServeMux but wasnt able to achieve the subrouting, so thats why it has {} (it was, for better readability reasons, separating all the multiplexers), gonna delete it RN
the use of var i thought it was gonna be more readable but actually makes sense to just use the normal :=
thanks for the advice of panic, trying to understand them and when to use the best I can at the moment
yeah it was actually an idea of implementing middlewares, how do you generally handle it? I mean, maybe I could just do generics passing either an array of middlewares or one single middleware, idk
I manually wire/chain them personally (just net/http) but I think chi/echo/... make it somewhat easier
@alpine patio in this specific project im trying to use as few external libs/frameworks as possible so I think chaining them would be the best case
done some projects with Gin gonic before and, because the community says that this is "not idiomatic" im using native package to see if it works just as well as Gin
NGL im pretty liking it, its a good look on how things work behind the scenes haha
you mean to do something just like this, am I correct?
r.Handle("/auth/signin", basicAuthMiddleware(http.HandlerFunc(authController.SignIn)))
r.Handle("/auth/refresh-token", bearerAuthMiddleware(http.HandlerFunc(authController.RefreshToken)))
yes (cept for indent)
oh sure it was a mistake when I copied it haha
thanks a lot for your advices!! I saw your github acc and congratulations for your work, I have two years as a developer here in Brazil and just hope I could also make awesome projects like you someday.