Advertisement
Guest User

Untitled

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