Skip to content

Commit

Permalink
echo_server_golang.go
Browse files Browse the repository at this point in the history
  • Loading branch information
6769 authored Feb 11, 2020
1 parent 399864c commit b959f45
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions echo_server_golang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"net/http"
"os"
"log"
)
// https://github.com/aautar/go-http-echo
// DefaultPort is the default port to use if once is not specified by the SERVER_PORT environment variable
const DefaultPort = "7893";

func getServerPort() (string) {
port := os.Getenv("SERVER_PORT");
if port != "" {
return port;
}

return DefaultPort;
}

// EchoHandler echos back the request as a response
func EchoHandler(writer http.ResponseWriter, request *http.Request) {

log.Println("Echoing back request made to " + request.URL.Path + " to client (" + request.RemoteAddr + ")")

writer.Header().Set("Access-Control-Allow-Origin", "*")

// allow pre-flight headers
writer.Header().Set("Access-Control-Allow-Headers", "Content-Range, Content-Disposition, Content-Type, ETag")

request.Write(writer)
}

func main() {

log.Println("starting server, listening on port " + getServerPort())

http.HandleFunc("/", EchoHandler)
http.ListenAndServe(":" + getServerPort(), nil)
}

0 comments on commit b959f45

Please sign in to comment.