route/cmd/helloserver/main.go

35 lines
676 B
Go
Raw Normal View History

2017-01-20 00:31:22 +00:00
package main
import (
"flag"
"fmt"
2017-01-23 15:58:28 +00:00
"log"
2017-01-20 00:31:22 +00:00
"net/http"
2017-01-23 15:58:28 +00:00
"os"
"runtime"
"github.com/kr/pretty"
2017-01-23 15:58:28 +00:00
"go.uber.org/atomic"
2017-01-20 00:31:22 +00:00
)
var (
port = flag.String("port", "9090", "HTTP port to listen on")
2017-01-23 15:58:28 +00:00
hits *atomic.Int64
2017-01-20 00:31:22 +00:00
)
func main() {
flag.Parse()
2017-01-23 15:58:28 +00:00
hits = atomic.NewInt64(0)
2017-01-20 00:31:22 +00:00
http.ListenAndServe(":"+*port, http.HandlerFunc(handle))
}
func handle(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Route is go!")
fmt.Fprintf(w, "%s\n", pretty.Sprint(r.Header))
2017-01-23 15:58:28 +00:00
hn, _ := os.Hostname()
fmt.Fprintf(w, "Served by %s running %s\n", hn, runtime.GOOS)
fmt.Fprintf(w, "Hit count: %d", hits.Inc())
log.Printf("Hit from %s: %s", r.Header.Get("X-Remote-Ip"), r.RequestURI)
2017-01-20 00:31:22 +00:00
}