2021-09-20 14:57:24 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2021-09-20 17:31:41 +02:00
|
|
|
"fmt"
|
2021-09-20 14:57:24 +02:00
|
|
|
"net/http"
|
2021-09-20 17:31:41 +02:00
|
|
|
"time"
|
|
|
|
"io/ioutil"
|
|
|
|
"encoding/json"
|
2021-09-20 14:57:24 +02:00
|
|
|
|
2021-09-20 18:44:08 +02:00
|
|
|
"git.snrd.de/Spaenny/steamServer"
|
2021-09-20 17:31:41 +02:00
|
|
|
"git.snrd.de/Spaenny/steamServer/postgres"
|
|
|
|
"git.snrd.de/Spaenny/steamServer/web"
|
|
|
|
"github.com/robfig/cron/v3"
|
2021-09-20 14:57:24 +02:00
|
|
|
)
|
|
|
|
|
2021-09-20 17:31:41 +02:00
|
|
|
type Server struct {
|
|
|
|
AppID int `json:"AppID"`
|
|
|
|
Name string `json:"Name"`
|
|
|
|
Map string `json:"Map"`
|
|
|
|
Players int `json:"Players"`
|
|
|
|
MaxPlayers int `json:"MaxPlayers"`
|
|
|
|
}
|
|
|
|
|
2021-09-20 14:57:24 +02:00
|
|
|
func main() {
|
|
|
|
dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable"
|
|
|
|
|
|
|
|
store, err := postgres.NewStore(dsn)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:31:41 +02:00
|
|
|
|
2021-09-20 18:44:08 +02:00
|
|
|
ss, err := store.Servers()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("There is no server in the list.")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range ss {
|
|
|
|
serverString := s.ServerIP
|
|
|
|
c := cron.New()
|
|
|
|
c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) })
|
|
|
|
c.Start()
|
|
|
|
}
|
2021-09-20 17:31:41 +02:00
|
|
|
|
2021-09-20 18:44:08 +02:00
|
|
|
|
2021-09-20 14:57:24 +02:00
|
|
|
h := web.NewHandler(store)
|
2021-09-20 17:31:41 +02:00
|
|
|
http.ListenAndServe(":3000", h)
|
2021-09-20 14:57:24 +02:00
|
|
|
}
|
2021-09-20 17:31:41 +02:00
|
|
|
|
2021-09-20 18:44:08 +02:00
|
|
|
func updateServers(serverString string, store steamServer.Store) {
|
2021-09-20 17:31:41 +02:00
|
|
|
url := "http://api.spenny.eu/server/" + serverString
|
|
|
|
|
|
|
|
reqClient := http.Client {
|
|
|
|
Timeout: time.Second * 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := reqClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Body != nil {
|
|
|
|
defer res.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-20 18:44:08 +02:00
|
|
|
server := steamServer.Server{}
|
2021-09-20 17:31:41 +02:00
|
|
|
json.Unmarshal([]byte(body), &server)
|
2021-09-20 18:44:08 +02:00
|
|
|
store.UpdateServerByQuery(&server)
|
|
|
|
fmt.Println(&server)
|
2021-09-20 17:31:41 +02:00
|
|
|
}
|
|
|
|
|