Convert to GIN
This commit is contained in:
parent
48eea7fb37
commit
c5d8e858a4
69
main.go
69
main.go
@ -5,9 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/engine/standard"
|
||||
"github.com/labstack/echo/middleware"
|
||||
"gopkg.in/gin-gonic/gin.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -16,69 +14,68 @@ const (
|
||||
)
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
router := gin.Default()
|
||||
|
||||
// Middleware
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.Static("static"))
|
||||
//e.Use(middleware.Logger())
|
||||
//e.Use(middleware.Recover())
|
||||
//e.Use(middleware.Static("static"))
|
||||
|
||||
a := e.Group("/api")
|
||||
a.POST("/login", login)
|
||||
a := router.Group("/api/v1")
|
||||
{
|
||||
a.POST("/login", loginPost)
|
||||
|
||||
// Unauthenticated routes
|
||||
a.GET("/check", accessible)
|
||||
a.GET("/hello", func(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!")
|
||||
a.GET("/check", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "Accessible")
|
||||
})
|
||||
a.GET("/hello", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "Hello, World!")
|
||||
})
|
||||
}
|
||||
|
||||
// Restricted group
|
||||
r := a.Group("/restricted")
|
||||
r.Use(middleware.JWT([]byte(secret)))
|
||||
{
|
||||
//r.Use(middleware.JWT([]byte(secret)))
|
||||
r.GET("", restricted)
|
||||
}
|
||||
|
||||
e.Run(standard.New(":1323"))
|
||||
router.Run(":1323")
|
||||
}
|
||||
|
||||
func accessible(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "Accessible")
|
||||
func restricted(c *gin.Context) {
|
||||
//user, _ := c.Get("user") //.(*jwt.Token)
|
||||
//name := user.Claims["name"].(string)
|
||||
name := "jan"
|
||||
c.String(http.StatusOK, "Welcome "+name+"!")
|
||||
}
|
||||
|
||||
func restricted(c echo.Context) error {
|
||||
user := c.Get("user").(*jwt.Token)
|
||||
name := user.Claims["name"].(string)
|
||||
return c.String(http.StatusOK, "Welcome "+name+"!")
|
||||
}
|
||||
|
||||
func login(c echo.Context) error {
|
||||
username := c.FormValue("username")
|
||||
password := c.FormValue("password")
|
||||
func loginPost(c *gin.Context) {
|
||||
username, _ := c.GetPostForm("username")
|
||||
password, _ := c.GetPostForm("password")
|
||||
|
||||
if username == "jan" && password == "passwort" {
|
||||
// Create token
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
|
||||
// Set claims
|
||||
token.Claims["name"] = "Jan Bader"
|
||||
token.Claims["admin"] = true
|
||||
token.Claims["exp"] = time.Now().Add(time.Hour * expiration).Unix()
|
||||
//token.Claims["name"] = "Jan Bader"
|
||||
//token.Claims["admin"] = true
|
||||
//token.Claims["exp"] = time.Now().Add(time.Hour * expiration).Unix()
|
||||
|
||||
// Generate encoded token and send it as response.
|
||||
t, err := token.SignedString([]byte(secret))
|
||||
if err != nil {
|
||||
return err
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
cookie := new(echo.Cookie)
|
||||
cookie.SetName("authentication")
|
||||
cookie.SetValue(t)
|
||||
cookie.SetExpires(time.Now().Add(expiration * time.Hour))
|
||||
c.SetCookie(cookie)
|
||||
c.SetCookie("authentication", t, (int)((expiration * time.Hour).Seconds()), "/", "localhost:8080", true, false)
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
c.JSON(http.StatusOK, map[string]string{
|
||||
"token": t,
|
||||
})
|
||||
}
|
||||
|
||||
return echo.ErrUnauthorized
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user