Skip to content

Instantly share code, notes, and snippets.

Created July 25, 2021 13:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Averiguar host de petición HTTP con Go
package main
import (
const PuertoEscuchaServidor = ":8080"
func main() {
Leer puerto si es que lo pasaron en la línea de comandos
puerto := flag.String("puerto", PuertoEscuchaServidor, "Puerto de escucha del servidor")
puertoReal := *puerto
// Agregar los dos puntos si es necesario
if strings.Index(puertoReal, ":") == -1 {
puertoReal = ":" + puertoReal
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Cors
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
responderHttpConExito(fmt.Sprintf("El Origin es: '%s'", r.Header.Get("Origin")), w)
log.Printf("El Origin es: '%s'", r.Header.Get("Origin"))
log.Fatal(http.ListenAndServe(puertoReal, nil))
func responderHttpConExito(data interface{}, w http.ResponseWriter) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment