Last active
December 28, 2015 10:29
-
-
Save andreadipersio/7486443 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"fmt" | |
"log" | |
"flag" | |
"os" | |
"time" | |
"net/http" | |
"encoding/json" | |
) | |
var ( | |
port int | |
) | |
func rootHandler(w http.ResponseWriter, r *http.Request) { | |
// http://golang.org/pkg/encoding/json/#Encoder | |
encoder := json.NewEncoder(w) | |
respData := struct { | |
LocalTime time.Time | |
Hostname string | |
} {} | |
respData.LocalTime = time.Now() | |
respData.Hostname, _ = os.Hostname() | |
if err := encoder.Encode(&respData); err != nil { | |
http.Error(w, fmt.Sprintf("Cannot encode response data: %v", err), 500) | |
} | |
} | |
func init() { | |
flag.IntVar(&port, "port", 8080, "HTTP Server Port") | |
flag.Parse() | |
} | |
func main() { | |
httpAddr := fmt.Sprintf(":%v", port) | |
log.Printf("Listening to %v", httpAddr) | |
http.HandleFunc("/", rootHandler) | |
log.Fatal(http.ListenAndServe(httpAddr, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment