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" "io/ioutil"
"encoding/json" "encoding/json"
"git.snrd.de/Spaenny/steamServer"
"git.snrd.de/Spaenny/steamServer/postgres" "git.snrd.de/Spaenny/steamServer/postgres"
"git.snrd.de/Spaenny/steamServer/web" "git.snrd.de/Spaenny/steamServer/web"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
@ -29,17 +30,25 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
serverString := "s.spenny.tf"
c := cron.New() ss, err := store.Servers()
c.AddFunc("*/1 * * * *", func() { updateServers(serverString) }) if err != nil {
c.Start() 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) h := web.NewHandler(store)
http.ListenAndServe(":3000", h) http.ListenAndServe(":3000", h)
} }
func updateServers(serverString string) { func updateServers(serverString string, store steamServer.Store) {
url := "http://api.spenny.eu/server/" + serverString url := "http://api.spenny.eu/server/" + serverString
reqClient := http.Client { reqClient := http.Client {
@ -66,8 +75,9 @@ func updateServers(serverString string) {
} }
server := Server{} server := steamServer.Server{}
json.Unmarshal([]byte(body), &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, region_id UUID NOT NULL REFERENCES regions(id) ON DELETE CASCADE,
gamemode_id UUID NOT NULL REFERENCES gamemodes(id) ON DELETE CASCADE, gamemode_id UUID NOT NULL REFERENCES gamemodes(id) ON DELETE CASCADE,
app_id INT NOT NULL, app_id INT NOT NULL,
server_ip TEXT NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
map TEXT NOT NULL, map TEXT NOT NULL,
players INT, players INT,

View file

@ -30,10 +30,13 @@ func (s *ServerStore) Servers() ([]steamServer.Server, error) {
} }
func (s *ServerStore) CreateServer(ser *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.ID,
ser.RegionID,
ser.GamemodeID,
ser.AppID, ser.AppID,
ser.Name, ser.Name,
ser.ServerIP,
ser.Map, ser.Map,
ser.Players, ser.Players,
ser.MaxPlayers); err != nil { ser.MaxPlayers); err != nil {
@ -41,3 +44,15 @@ func (s *ServerStore) CreateServer(ser *steamServer.Server) error {
} }
return nil 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"` RegionID uuid.UUID `db:"region_id"`
GamemodeID uuid.UUID `db:"gamemode_id"` GamemodeID uuid.UUID `db:"gamemode_id"`
AppID int `db:"app_id"` AppID int `db:"app_id"`
ServerIP string `db:"server_ip"`
Name string `db:"name"` Name string `db:"name"`
Map string `db:"map"` Map string `db:"map"`
Players int `db:"players"` Players int `db:"players"`
@ -17,6 +18,7 @@ type ServerStore interface {
Server(id uuid.UUID) (Server, error) Server(id uuid.UUID) (Server, error)
Servers() ([]Server, error) Servers() ([]Server, error)
CreateServer(ser *Server) error CreateServer(ser *Server) error
UpdateServerByQuery(ser *Server) error
} }
type Store interface { type Store interface {

View file

@ -64,7 +64,7 @@
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm"> <td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
<div class="flex items-center"> <div class="flex items-center">
<div class="ml-3"> <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>
</div> </div>
</td> </td>