hw/main.go

34 lines
630 B
Go
Raw Normal View History

2018-01-09 16:26:12 -08:00
package main
import (
"encoding/json"
"log"
"net/http"
2018-01-10 09:49:42 -08:00
"os"
2018-01-09 16:26:12 -08:00
)
type v struct {
2018-01-10 09:49:42 -08:00
Hostname string `json:"hostname"`
V string `json:"version"`
2018-01-09 16:26:12 -08:00
}
func main() {
2018-01-10 09:49:42 -08:00
hn, err := os.Hostname()
if err != nil {
log.Fatalf("hostname: %+v", err)
}
2018-01-09 16:26:12 -08:00
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
2018-01-10 09:49:42 -08:00
r := v{
Hostname: hn,
2018-01-10 09:51:02 -08:00
V: "v0.0.3",
2018-01-10 09:49:42 -08:00
}
2018-01-09 16:26:12 -08:00
if err := json.NewEncoder(w).Encode(r); err != nil {
log.Printf("json: %+v", err)
}
})
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("listen and serve: %v", err)
}
}