0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Go Echo でミドルウェアを入れようとした時のつまずき

Posted at

1. Echo で 以下のようにミドルウェアを設定

func main() {
	e := echo.New()

	// ミドルウェア
	e.Use(middleware.Recover())
	e.Use(middleware.Logger())

2. jwt-go が必要と言われたので入れた

echo-test-app-1  | ../pkg/mod/github.com/labstack/echo@v3.3.10+incompatible/middleware/jwt.go:9:2: no required module provides package github.com/dgrijalva/jwt-go; to add it:
echo-test-app-1  |      go get github.com/dgrijalva/jwt-go
echo-test-app-1 exited with code 1
go get github.com/dgrijalva/jwt-go

3. MiddlewareFunc である必要があるとのこと

echo-test-app-1  | go: downloading github.com/dgrijalva/jwt-go v3.2.0+incompatible
echo-test-app-1  | # command-line-arguments
echo-test-app-1  | ./server.go:14:8: cannot use middleware.Recover() (value of type "github.com/labstack/echo".MiddlewareFunc) as type "github.com/labstack/echo/v4".MiddlewareFunc in argument to e.Use
echo-test-app-1  | ./server.go:15:8: cannot use middleware.Logger() (value of type "github.com/labstack/echo".MiddlewareFunc) as type "github.com/labstack/echo/v4".MiddlewareFunc in argument to e.Use
echo-test-app-1 exited with code 2

以下記事の通り、 import の記述を変える
https://qiita.com/niwa3/items/642236261b16ef1a78dd

import (
- 	"github.com/labstack/echo/middleware"
	"github.com/labstack/echo/v4"
+ 	"github.com/labstack/echo/v4/middleware"
)

4. go get で v4/middleware を取得し、無事起動できた

echo-test-app-1  | ../pkg/mod/github.com/labstack/echo/v4@v4.7.2/middleware/jwt.go:9:2: missing go.sum entry for module providing package github.com/golang-jwt/jwt (imported by github.com/labstack/echo/v4/middleware); to add:echo-test-app-1  |      go get github.com/labstack/echo/v4/middleware@v4.7.2
echo-test-app-1  | ../pkg/mod/github.com/labstack/echo/v4@v4.7.2/middleware/rate_limiter.go:9:2: missing go.sum entry for module providing package golang.org/x/time/rate (imported by github.com/labstack/echo/v4/middleware); to add:
echo-test-app-1  |      go get github.com/labstack/echo/v4/middleware@v4.7.2
echo-test-app-1 exited with code 1
go get github.com/labstack/echo/v4/middleware@v4.7.2

無事立ち上がり、ログも出力された。

Attaching to echo-test-app-1
echo-test-app-1  | go: downloading github.com/golang-jwt/jwt v3.2.2+incompatible
echo-test-app-1  | go: downloading golang.org/x/time v0.0.0-20201208040808-7e3f01d25324
echo-test-app-1  |
echo-test-app-1  |    ____    __
echo-test-app-1  |   / __/___/ /  ___
echo-test-app-1  |  / _// __/ _ \/ _ \
echo-test-app-1  | /___/\__/_//_/\___/ v4.7.2
echo-test-app-1  | High performance, minimalist Go web framework
echo-test-app-1  | https://echo.labstack.com
echo-test-app-1  | ____________________________________O/_______
echo-test-app-1  |                                     O\
echo-test-app-1  | ⇨ http server started on [::]:1323
echo-test-app-1  | {"time":"2022-08-05T23:39:06.262116217Z","id":"","remote_ip":"172.19.0.1","host":"localhost:1323","method":"GET","uri":"/users/14","user_agent":"PostmanRuntime/7.29.2","status":200,"error":"","latency":12143792,"latency_human":"12.143792ms","bytes_in":0,"bytes_out":169}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?