Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.66 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "log"
  6.     "fmt"
  7.     "net/http"
  8.     "github.com/gorilla/mux"
  9. )
  10.  
  11. // STRUCTS
  12.  
  13. type Endereco struct {
  14.     Cidade  string `json:"cidade,omitempty"`
  15.     Estado  string `json:"estado,omitempty"`
  16. }
  17.  
  18. type Pessoa struct {
  19.     ID          string    `json:"id,omitempty"`
  20.     Nome        string    `json:"nome,omitempty"`
  21.     Sobrenome   string    `json:"sobrenome,omitempty"`
  22.     Endereco    *Endereco `json:"endereco,omitempty"`
  23. }
  24.  
  25.  
  26. var pessoa []Pessoa
  27.  
  28. pessoa = append(pessoa, Pessoa{ID: "1", Nome: "Alex", Sobrenome: "Santos", Endereco: &Endereco{Cidade: "Cidade X", Estado: "Estado X"}})
  29. pessoa = append(pessoa, Pessoa{ID: "2", Nome: "Júlio", Sobrenome: "Filgueiras", Endereco: &Endereco{Cidade: "Cidade Z", Estado: "Estado Y"}})
  30. pessoa = append(pessoa, Pessoa{ID: "3", Nome: "Nadson", Sobrenome: "Souza"})
  31.  
  32. // FUNÇÕES
  33.  
  34. func configurarRotas() {
  35.     router := mux.NewRouter()
  36.     router.HandleFunc("/contato", GetTodasPessoas).Methods("GET")
  37.     router.HandleFunc("/contato/{id}", GetPessoa).Methods("GET")
  38.     router.HandleFunc("/contato/{id}", CreatePessoa).Methods("POST")
  39.     router.HandleFunc("/contato/{id}", DeletePessoa).Methods("DELETE")
  40. }
  41.  
  42. func subirServidor() {
  43.     configurarRotas()
  44.  
  45.     fmt.Println("O servidor está rodando na porta 8080")
  46.     log.Fatal(http.ListenAndServe(":8080", nil))
  47. }
  48.  
  49.  
  50. func GetTodasPessoas(w http.ResponseWriter, r *http.Request) {
  51.     json.NewEncoder(w).Encode(pessoa)
  52. }
  53. func GetPessoa(w http.ResponseWriter, r *http.Request) {}
  54. func CreatePessoa(w http.ResponseWriter, r *http.Request) {}
  55. func DeletePessoa(w http.ResponseWriter, r *http.Request) {}
  56.  
  57.  
  58. func main() {
  59.     subirServidor()
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement