Advertisement
Guest User

Untitled

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