Created
October 15, 2019 18:55
-
-
Save edgardo001/d7adb2cb82fb6090ee34af249d5139a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
Instalar libreria mux para perticiones http | |
$> go get github.com/gorilla/mux | |
Para ejecutar como script | |
$> go run demo-api.go | |
Para compilar Windows: | |
$> go build -o demo-api.exe demo-api.go | |
$> demo-api.exe | |
Para compilar Linux | |
$> go build -o demo-api demo-api.go | |
$> ./demo-api | |
https://www.ardanlabs.com/blog/2013/10/cross-compile-your-go-programs.html | |
https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04 | |
https://medium.com/@utranand/building-golang-package-for-linux-from-windows-22fa23764808 | |
https://itnext.io/building-a-web-http-server-with-go-6554029b4079 | |
https://www.phpflow.com/golang/http-request-example-using-golang-mux/ | |
https://medium.com/@hugo.bjarred/rest-api-with-golang-and-mux-e934f581b8b5 | |
https://forum.golangbridge.org/t/how-to-run-a-golang-program-on-https/8136 | |
$GOOS $GOARCH | |
darwin 386 – 32 bit MacOSX | |
darwin amd64 – 64 bit MacOSX | |
freebsd 386 | |
freebsd amd64 | |
linux 386 – 32 bit Linux | |
linux amd64 – 64 bit Linux | |
linux arm – RISC Linux | |
netbsd 386 | |
netbsd amd64 | |
openbsd 386 | |
openbsd amd64 | |
plan9 386 | |
windows 386 – 32 bit Windows | |
windows amd64 – 64 bit Windows | |
$> go run demo-api.go | |
$> go env | |
Windows POWERSHELL | |
$> $env:GOOS = "windows"; $env:GOARCH = "amd64"; go build -o demo-api-windows.exe demo-api.go; | |
$> $env:GOOS = "linux"; $env:GOARCH = "arm"; go build -o demo-api-linux demo-api.go; | |
Linux Consola | |
$> export GOOS="windows" && export GOARCH="amd64" && go build -o demo-api-windows.exe demo-api.go | |
$> export GOOS="linux" && export GOARCH="arm" && go build -o demo-api-linux demo-api.go; | |
GOOS=darwin GOARCH=386 go build test.go | |
Permisos de ejecucion en linux | |
$> chmod +x demo-api-linux | |
$> ./demo-api-linux | |
https://www.ianlewis.org/en/http2-and-go | |
https://www.kaihag.com/https-and-go/ | |
https://github.com/denji/golang-tls | |
http.ListenAndServeTLS() | |
**/ | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"github.com/gorilla/mux" | |
) | |
// Person : Almacena informaciond de una persona | |
type Person struct { | |
ID string `json:"id,omitempty"` | |
FirstName string `json:"firstname,omitempty"` | |
LastName string `json:"lastname,omitempty"` | |
Address *Address `json:"address,omitempty"` | |
} | |
// Address : | |
type Address struct { | |
City string `json:"city,omitempty"` | |
State string `json:"state,omitempty"` | |
} | |
// Saludo : | |
type Saludo struct { | |
Message string //`Message:"message,omitempty"` | |
Datetime string //`Datetime:"datetime,omitempty"` | |
} | |
var people []Person | |
// ========= EndPoints =============== | |
// GetHelloEndpoint : | |
func GetHelloEndpoint(w http.ResponseWriter, req *http.Request) { | |
json.NewEncoder(w).Encode(Saludo{Message: "Hello", Datetime: time.Now().String()}) | |
} | |
// GetPersonEndpoint : | |
func GetPersonEndpoint(w http.ResponseWriter, req *http.Request) { | |
params := mux.Vars(req) | |
for _, item := range people { | |
if item.ID == params["id"] { | |
json.NewEncoder(w).Encode(item) | |
return | |
} | |
} | |
json.NewEncoder(w).Encode(&Person{}) | |
} | |
// GetPeopleEndpoint : | |
func GetPeopleEndpoint(w http.ResponseWriter, req *http.Request) { | |
json.NewEncoder(w).Encode(people) | |
} | |
// CreatePersonEndpoint : | |
func CreatePersonEndpoint(w http.ResponseWriter, req *http.Request) { | |
params := mux.Vars(req) | |
var person Person | |
_ = json.NewDecoder(req.Body).Decode(&person) | |
person.ID = params["id"] | |
people = append(people, person) | |
json.NewEncoder(w).Encode(people) | |
} | |
// DeletePersonEndpoint : | |
func DeletePersonEndpoint(w http.ResponseWriter, req *http.Request) { | |
params := mux.Vars(req) | |
for index, item := range people { | |
if item.ID == params["id"] { | |
people = append(people[:index], people[index+1:]...) | |
break | |
} | |
} | |
json.NewEncoder(w).Encode(people) | |
} | |
func main() { | |
port := 3002 | |
router := mux.NewRouter() | |
// adding example data | |
people = append(people, Person{ID: "1", FirstName: "Ryan", LastName: "Ray", Address: &Address{City: "Dubling", State: "California"}}) | |
people = append(people, Person{ID: "2", FirstName: "Maria", LastName: "Ray"}) | |
// endpoints | |
router.HandleFunc("/", GetHelloEndpoint).Methods("GET") | |
router.HandleFunc("/people", GetPeopleEndpoint).Methods("GET") | |
router.HandleFunc("/people/{id}", GetPersonEndpoint).Methods("GET") | |
router.HandleFunc("/people/{id}", CreatePersonEndpoint).Methods("POST") | |
router.HandleFunc("/people/{id}", DeletePersonEndpoint).Methods("DELETE") | |
portString := fmt.Sprintf("%s%d", ":", port) | |
fmt.Println(time.Now().String(), " --> Listen port->", portString) | |
log.Fatal(http.ListenAndServe(portString, router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment