func login(c vodka.Context) error { username := c.FormValue("username") password := c.FormValue("password") if username == "jon" && password == "shhh!" { // Set custom claims claims := &jwtCustomClaims{ "Jon Snow", true, jwt.StandardClaims{ ExpiresAt: time.Now().Add(time.Hour * 72).Unix(), }, } // Create token with claims token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) // Generate encoded token and send it as response. t, err := token.SignedString([]byte("secret")) if err != nil { return err } return c.JSON(http.StatusOK, map[string]string{ "token": t, }) } return vodka.ErrUnauthorized }
func login(c vodka.Context) error { username := c.FormValue("username") password := c.FormValue("password") if username == "jon" && password == "shhh!" { // Create token token := jwt.New(jwt.SigningMethodHS256) // Set claims claims := token.Claims.(jwt.MapClaims) claims["name"] = "Jon Snow" claims["admin"] = true claims["exp"] = time.Now().Add(time.Hour * 72).Unix() // Generate encoded token and send it as response. t, err := token.SignedString([]byte("secret")) if err != nil { return err } return c.JSON(http.StatusOK, map[string]string{ "token": t, }) } return vodka.ErrUnauthorized }
func createUser(c vodka.Context) error { u := new(user) if err := c.Bind(u); err != nil { return err } users[u.ID] = *u return c.JSON(http.StatusCreated, u) }
func updateUser(c vodka.Context) error { u := new(user) if err := c.Bind(u); err != nil { return err } id, _ := strconv.Atoi(c.Param("id")) users[id].Name = u.Name return c.JSON(http.StatusOK, users[id]) }
func createUser(c vodka.Context) error { u := &user{ ID: seq, } if err := c.Bind(u); err != nil { return err } users[u.ID] = u seq++ return c.JSON(http.StatusCreated, u) }
func getUser(c vodka.Context) error { id, _ := strconv.Atoi(c.Param("id")) return c.JSON(http.StatusOK, users[id]) }
func getUser(c vodka.Context) error { return c.JSON(http.StatusOK, users[c.P(0)]) }
func getUsers(c vodka.Context) error { return c.JSON(http.StatusOK, users) }
// Handle is the endpoint to get stats. func (s *Stats) Handle(c vodka.Context) error { s.mutex.RLock() defer s.mutex.RUnlock() return c.JSON(http.StatusOK, s) }