Skip to content

Instantly share code, notes, and snippets.

@lotusirous
Last active February 10, 2019 08:33
Show Gist options
  • Select an option

  • Save lotusirous/9b93baf841f2b03352b1f918decce5ab to your computer and use it in GitHub Desktop.

Select an option

Save lotusirous/9b93baf841f2b03352b1f918decce5ab to your computer and use it in GitHub Desktop.
Simple web server returns hostname
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
var (
listenAddr string
)
func main() {
flag.StringVar(&listenAddr, "addr", ":8080", "server listen address")
flag.Parse()
log.Printf("Server started at %s ", listenAddr)
hostname, err := os.Hostname()
if err != nil {
log.Panic(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Your hostname is: %s", hostname)
})
server := &http.Server{
Addr: listenAddr,
ReadTimeout: time.Second,
WriteTimeout: time.Second,
IdleTimeout: time.Second,
}
// let's the hunt begin
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment