route/cmd/helloserver/main.go

39 lines
713 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!")
2017-03-26 20:47:42 +00:00
fmt.Fprintf(w, "%s\n", pretty.Sprintf("%s", 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())
ip := r.Header.Get("X-Remote-Ip")
if ip != "" {
log.Printf("Hit from %s: %s", ip, r.RequestURI)
}
2017-01-20 00:31:22 +00:00
}