Skip to content

How to setup middleware global? I tried but didn't work #2005

@ridwankustanto

Description

@ridwankustanto

Hi, I have another question about encore go, I was trying to implement basic global middleware, the structure and the code looks like this:

root_project
|_encore.app
|_healthcheck
|___healthcheck.go
|___middleware.go
|_middleware.go

./healthcheck/healthcheck.go

package healthcheck

import "context"

// CheckHealth returns overall application health status
//
//encore:api public method=GET path=/health tag:public
func (s *Service) CheckHealth(ctx context.Context) (*HealthResponse, error) {
	return &HealthResponse{
		Status: "healthy",
	}, nil
}

./healthcheck/middleware.go

package healthcheck

import (
	"log"

	"encore.dev/middleware"
)

//encore:middleware target=tag:public
func HealthMiddleware(req middleware.Request, next middleware.Next) middleware.Response {
	log.Println("HealthMiddleware HERE")
	return next(req)
}

./middleware.go

package main

import (
	"log"

	"encore.dev/middleware"
)

// PublicGlobalMiddleware applies global middleware to public endpoints
//
//encore:middleware global target=tag:public
func PublicGlobalMiddleware(req middleware.Request, next middleware.Next) middleware.Response {
	log.Println("PublicGlobalMiddleware HERE")
	return next(req)
}

middleware on service level worked fine, i can see log HealthMiddleware HERE when i hit the api. But on global level, when I hit the api with tag public (even tried with target all) it didn't work, i can't see any log. Is there any error on how I apply the middleware?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions