web server displays queried servers now

This commit is contained in:
Philipp 2021-09-20 18:44:08 +02:00
parent 9a646cca88
commit 7243500197
5 changed files with 37 additions and 9 deletions

View file

@ -8,6 +8,7 @@ import (
"io/ioutil"
"encoding/json"
"git.snrd.de/Spaenny/steamServer"
"git.snrd.de/Spaenny/steamServer/postgres"
"git.snrd.de/Spaenny/steamServer/web"
"github.com/robfig/cron/v3"
@ -29,17 +30,25 @@ func main() {
log.Fatal(err)
}
serverString := "s.spenny.tf"
c := cron.New()
c.AddFunc("*/1 * * * *", func() { updateServers(serverString) })
c.Start()
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()
}
h := web.NewHandler(store)
http.ListenAndServe(":3000", h)
}
func updateServers(serverString string) {
func updateServers(serverString string, store steamServer.Store) {
url := "http://api.spenny.eu/server/" + serverString
reqClient := http.Client {
@ -66,8 +75,9 @@ func updateServers(serverString string) {
}
server := Server{}
server := steamServer.Server{}
json.Unmarshal([]byte(body), &server)
fmt.Println(server)
store.UpdateServerByQuery(&server)
fmt.Println(&server)
}

View file

@ -13,6 +13,7 @@ CREATE TABLE servers (
region_id UUID NOT NULL REFERENCES regions(id) ON DELETE CASCADE,
gamemode_id UUID NOT NULL REFERENCES gamemodes(id) ON DELETE CASCADE,
app_id INT NOT NULL,
server_ip TEXT NOT NULL,
name TEXT NOT NULL,
map TEXT NOT NULL,
players INT,

View file

@ -30,10 +30,13 @@ func (s *ServerStore) Servers() ([]steamServer.Server, error) {
}
func (s *ServerStore) CreateServer(ser *steamServer.Server) error {
if err := s.Get(ser, `INSERT INTO servers VALUES($1, $2, $3, $4, $5, $6) RETURNUNG *`,
if err := s.Get(ser, `INSERT INTO servers VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNUNG *`,
ser.ID,
ser.RegionID,
ser.GamemodeID,
ser.AppID,
ser.Name,
ser.ServerIP,
ser.Map,
ser.Players,
ser.MaxPlayers); err != nil {
@ -41,3 +44,15 @@ func (s *ServerStore) CreateServer(ser *steamServer.Server) error {
}
return nil
}
func (s *ServerStore) UpdateServerByQuery(ser *steamServer.Server) error {
fmt.Println(ser.Name + ser.Map)
if err := s.Get(s, `UPDATE "servers" SET "name" = $1, "map" = $2, "players" = $3, "max_players" = $4 WHERE "name" = $1 RETURNING *`,
ser.Name,
ser.Map,
ser.Players,
ser.MaxPlayers); err != nil {
return fmt.Errorf("error updating server: %w", err)
}
return nil
}

View file

@ -7,6 +7,7 @@ type Server struct {
RegionID uuid.UUID `db:"region_id"`
GamemodeID uuid.UUID `db:"gamemode_id"`
AppID int `db:"app_id"`
ServerIP string `db:"server_ip"`
Name string `db:"name"`
Map string `db:"map"`
Players int `db:"players"`
@ -17,6 +18,7 @@ type ServerStore interface {
Server(id uuid.UUID) (Server, error)
Servers() ([]Server, error)
CreateServer(ser *Server) error
UpdateServerByQuery(ser *Server) error
}
type Store interface {

View file

@ -64,7 +64,7 @@
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
<div class="flex items-center">
<div class="ml-3">
<a href="steam://connect/88.198.49.46:27015" class="text-gray-900 whitespace-no-wrap">{{.Name}}</a>
<a href="steam://connect/{{.ServerIP}}" class="text-gray-900 whitespace-no-wrap">{{.Name}}</a>
</div>
</div>
</td>