Last active
November 15, 2017 18:02
-
-
Save meneguite/fee5fbd285f399166da8453675605ad6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"golang.org/x/crypto/bcrypt" | |
"github.com/labstack/echo" | |
) | |
type User struct { | |
Email string `json:"email,omitempty" form:"email"` | |
Password string `json:"-" form:"password"` | |
Name string `json:"name,omitempty" form:"name"` | |
Role string `json:"role,omitempty" form:"role"` | |
} | |
var users []User | |
// Retorna a listagem de todos os usuários cadastrados | |
func getAllUsers (c echo.Context) error { | |
return c.JSON(http.StatusOK, users) | |
} | |
// Retorna os dados de um usuário | |
func getUser (c echo.Context) error { | |
email := c.Param("user") | |
user := getUserByEmail(email) | |
return c.JSON(http.StatusOK, user) | |
} | |
// Cadastra um novo usuário | |
func createUser (c echo.Context) error { | |
password := c.FormValue("password") | |
hashPassword, _ := bcrypt.GenerateFromPassword([]byte(password), 10) | |
user := User{Email: c.FormValue("email"), Password: string(hashPassword), Name: c.FormValue("name"), Role: c.FormValue("role")} | |
users = append(users, user) | |
return c.JSON(http.StatusCreated, user) | |
} | |
// Atualiza um usuário existente | |
func updateUser (c echo.Context) error { | |
email := c.Param("user") | |
for index, user := range users { | |
if (user.Email == email) { | |
name := c.FormValue("name"); | |
if ( name != "") { | |
users[index].Name = name | |
} | |
role := c.FormValue("role"); | |
if ( role != "") { | |
users[index].Role = role | |
} | |
password := c.FormValue("password"); | |
if ( password != "") { | |
hashPassword, _ := bcrypt.GenerateFromPassword([]byte(password), 10) | |
users[index].Password = string(hashPassword) | |
} | |
return c.JSON(http.StatusOK, users[index]) | |
} | |
} | |
return c.JSONBlob(http.StatusBadRequest, []byte("{\"status\": \"error\"}")) | |
} | |
// Exclui um usuário existente | |
func deleteUser (c echo.Context) error { | |
email := c.Param("user") | |
for index, user := range users { | |
if (user.Email == email) { | |
users = append(users[:index], users[index+1:]...) | |
return c.JSONBlob(http.StatusOK, []byte("{\"status\": \"success\"}")) | |
} | |
} | |
return c.JSONBlob(http.StatusBadRequest, []byte("{\"status\": \"error\"}")) | |
} | |
// Faz a autenticação de um usuário | |
func authUser (c echo.Context) error { | |
email := c.FormValue("email") | |
password := c.FormValue("password") | |
user := getUserByEmail(email) | |
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) | |
if (err == nil) { | |
return c.JSONBlob(http.StatusOK, []byte("{\"status\": \"success\"}")) | |
} | |
return c.JSONBlob(http.StatusUnauthorized, []byte("{\"status\": \"error\"}")) | |
} | |
// Retonar instancia do usuário apartir de um e-mail | |
func getUserByEmail(email string) User { | |
var user User; | |
for _, user = range users { | |
if (user.Email == email) { | |
return user | |
} | |
} | |
return user; | |
} | |
func main() { | |
// Inicia usuário | |
password, _ := bcrypt.GenerateFromPassword([]byte("123456"), 10) | |
users = append(users, User{Email:"[email protected]", Password: string(password), Name:"Ronaldo Meneguite", Role:"Admin"}) | |
router := echo.New() | |
v1 := router.Group("/v1") | |
// User API | |
v1.GET("/users", getAllUsers) | |
v1.GET("/users/:user", getUser) | |
v1.POST("/users", createUser) | |
v1.PUT("/users/:user", updateUser) | |
v1.DELETE("/users/:user", deleteUser) | |
// Auth API | |
v1.POST("/auth", authUser) | |
router.Logger.Fatal(router.Start(":8080")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment