first push

This commit is contained in:
Philipp 2021-09-20 12:15:44 +02:00
commit cd90a16071
3 changed files with 74 additions and 0 deletions

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module git.snrd.de/Spaenny/a2s-steam-server
go 1.17
require github.com/rumblefrog/go-a2s v1.0.1

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/rumblefrog/go-a2s v1.0.1 h1:0M4QUDB1Tz00tCz36r/AWB4YFO2ajksvPnd7Abzz9SE=
github.com/rumblefrog/go-a2s v1.0.1/go.mod h1:JwbTgMTRGZcWzr3T2MUfDusrJU5Bdg8biEeZzPtN0So=

67
main.go Normal file
View file

@ -0,0 +1,67 @@
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)))
}