package main import ( "fmt" "log" "bytes" "net/http" "net/url" "strings" "runtime" "github.com/rumblefrog/go-a2s" "encoding/json" ) func newServerQuerier(host string) ([]byte, error) { client, err := a2s.NewClient(host) if err != nil { log.Printf("Creation of client failed: %s", err) } defer client.Close() info, err := client.QueryInfo() if err != nil { log.Printf("Querying failed: %s", err) return []byte("\"error\": \"1\""), nil } log.Printf("%s - %s\n", host, info.Name) buf, err := json.Marshal(info) var indented bytes.Buffer json.Indent(&indented, buf, "\t", "\t") return []byte(indented.String()), err } func Log(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf("access: %s %s %s", r.RemoteAddr, r.Method, r.URL) handler.ServeHTTP(w, r) }) } func httpServer(w http.ResponseWriter, r *http.Request) { uriSegments := strings.Split(r.URL.String(), "/") host, _ := url.QueryUnescape(uriSegments[2]) var info, err = newServerQuerier(host) if err != nil { log.Printf("Got an error while receiving data: %s", err) } w.Header().Set("Content-Type", "application/json; charset=UTF-8") fmt.Fprintf(w, "%s", info) } func main() { runtime.GOMAXPROCS(runtime.NumCPU()) http.HandleFunc("/server/", httpServer) log.Fatal(http.ListenAndServe(":8080", Log(http.DefaultServeMux))) }