1 回答

TA貢獻1877條經驗 獲得超1個贊
您可以對路由進行分組
package main
import (
"crypto/subtle"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Message struct {
Status string `json:"status"`
}
func main() {
e := echo.New()
g := e.Group("/")
g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if subtle.ConstantTimeCompare([]byte(username), []byte("username")) == 1 &&
subtle.ConstantTimeCompare([]byte(password), []byte("password")) == 1 {
return true, nil
}
return false, nil
}))
g.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.GET("/healthcheck", func(c echo.Context) error {
u := &Message{
Status: "OK",
}
return c.JSON(http.StatusOK, u)
})
e.Logger.Fatal(e.Start(":8080"))
}
- 1 回答
- 0 關注
- 128 瀏覽
添加回答
舉報