diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a21ba53 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +.PHONY: postgres adminer migrate + +postgres: + docker run --rm -ti --network host -e POSTGRES_PASSWORD=secret postgres + +adminer: + docker run --rm -ti --network host adminer + +reflex: + reflex -s go run cmd/goddit/main.go + +migrate: + migrate -source file://migrations \ + -database postgres://postgres:secret@localhost/postgres?sslmode=disable up + +migrate-down: + migrate -source file://migrations \ + -database postgres://postgres:secret@localhost/postgres?sslmode=disable down diff --git a/cmd/main.go b/cmd/main.go index 08008be..ca403be 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,12 +2,25 @@ package main import ( "log" + "fmt" "net/http" + "time" + "io/ioutil" + "encoding/json" - "git.snrd.de/Spaenny/postgres" - "git.snrd.de/Spaenny/web" + "git.snrd.de/Spaenny/steamServer/postgres" + "git.snrd.de/Spaenny/steamServer/web" + "github.com/robfig/cron/v3" ) +type Server struct { + AppID int `json:"AppID"` + Name string `json:"Name"` + Map string `json:"Map"` + Players int `json:"Players"` + MaxPlayers int `json:"MaxPlayers"` +} + func main() { dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable" @@ -16,6 +29,45 @@ func main() { log.Fatal(err) } + serverString := "s.spenny.tf" + + c := cron.New() + c.AddFunc("*/1 * * * *", func() { updateServers(serverString) }) + c.Start() + h := web.NewHandler(store) - http.ListenAndServe(":3000, h) + http.ListenAndServe(":3000", h) } + +func updateServers(serverString string) { + 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) + } + + + server := Server{} + json.Unmarshal([]byte(body), &server) + fmt.Println(server) +} + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d8024f9 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module git.snrd.de/Spaenny/steamServer + +go 1.17 + +require ( + git.snrd.de/Spaenny/steamserver v0.0.0-20210920125724-7e62f366e7e0 + github.com/go-chi/chi v1.5.4 + github.com/google/uuid v1.3.0 + github.com/jmoiron/sqlx v1.3.4 + github.com/lib/pq v1.10.3 + github.com/robfig/cron/v3 v3.0.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..551aac0 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +git.snrd.de/Spaenny/steamserver v0.0.0-20210920125724-7e62f366e7e0 h1:b43+U83rjT0A+1k6MP5Muu/crGVzciaszaPGIFBXNow= +git.snrd.de/Spaenny/steamserver v0.0.0-20210920125724-7e62f366e7e0/go.mod h1:rl0ho92HJaP8HYnk2Fwqq4fWnFrVaolgiadz5iPqRWk= +github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs= +github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w= +github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg= +github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= diff --git a/main.go b/main.go deleted file mode 100644 index 36f7dcc..0000000 --- a/main.go +++ /dev/null @@ -1,46 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" -) - - -type Server struct { - AppID int `json:"AppID"` - Name string `json:"Name"` - Map string `json:"Map"` - Players int `json:"Players"` - MaxPlayers int `json:"MaxPlayers"` -} - -func main() { - serverJSON := `{ - "Protocol": 17, - "Name": "Spenny.TF | DM #1", - "Map": "cp_process_final", - "Folder": "tf", - "Game": "TFTrue deathmatch ", - "AppID": 440, - "Players": 0, - "MaxPlayers": 8, - "Bots": 0, - "ServerType": 1, - "ServerOS": 1, - "Visibility": false, - "VAC": true, - "Version": "6774624", - "EDF": 177, - "ExtendedServerInfo": { - "Port": 27015, - "SteamID": 90151367621494793, - "Keywords": "cp,nocrits", - "GameID": 440 - } - }` - - var server Server - json.Unmarshal([]byte(serverJSON), &server) - fmt.Println(server) -} - diff --git a/migrations/1_create_tables.up.sql b/migrations/1_create_tables.up.sql index acb90e3..6675f0d 100644 --- a/migrations/1_create_tables.up.sql +++ b/migrations/1_create_tables.up.sql @@ -1,3 +1,13 @@ +CREATE TABLE regions ( + id UUID PRIMARY KEY, + name TEXT NOT NULL +); + +CREATE TABLE gamemodes ( + id UUID PRIMARY KEY, + name TEXT NOT NULL +); + CREATE TABLE servers ( id UUID PRIMARY KEY, region_id UUID NOT NULL REFERENCES regions(id) ON DELETE CASCADE, @@ -8,14 +18,3 @@ CREATE TABLE servers ( players INT, max_players INT ); - -CREATE TABLE regions ( - id UUID PRIMARY KEY, - name TEXT NOT NULL -); - -CREATE TABLE gamemodes ( - id UUID PRIMARY KEY, - name TEXT NOT NULL, - description NOT NULL -); diff --git a/postgres/server_store.go b/postgres/server_store.go index 185a32d..1aff299 100644 --- a/postgres/server_store.go +++ b/postgres/server_store.go @@ -20,13 +20,22 @@ func (s *ServerStore) Server(id uuid.UUID) (steamServer.Server, error) { return ser, nil } +func (s *ServerStore) Servers() ([]steamServer.Server, error) { + var ss []steamServer.Server + var query = `SELECT * FROM servers` + if err := s.Select(&ss, query); err != nil { + return []steamServer.Server{}, fmt.Errorf("error gettings servers: %w", err) + } + return ss, nil +} + func (s *ServerStore) CreateServer(ser *steamServer.Server) error { if err := s.Get(ser, `INSERT INTO servers VALUES($1, $2, $3, $4, $5, $6) RETURNUNG *`, ser.ID, ser.AppID, ser.Name, ser.Map, - ser.Player, + ser.Players, ser.MaxPlayers); err != nil { return fmt.Errorf("error creating server: %w", err) } diff --git a/steamServer.go b/steamServer.go index 7f7068f..574fcf3 100644 --- a/steamServer.go +++ b/steamServer.go @@ -4,16 +4,19 @@ import "github.com/google/uuid" type Server struct { ID uuid.UUID `db:"id"` - AppID int `json:"AppID"` - Name string `json:"Name"` - Map string `json:"Map"` - Players int `json:"Players"` - MaxPlayers int `json:"MaxPlayers"` + RegionID uuid.UUID `db:"region_id"` + GamemodeID uuid.UUID `db:"gamemode_id"` + AppID int `db:"app_id"` + Name string `db:"name"` + Map string `db:"map"` + Players int `db:"players"` + MaxPlayers int `db:"max_players"` } type ServerStore interface { Server(id uuid.UUID) (Server, error) - CreateServer(ser *Server) err + Servers() ([]Server, error) + CreateServer(ser *Server) error } type Store interface { diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..08fc2f9 --- /dev/null +++ b/templates/home.html @@ -0,0 +1,58 @@ +
+ + + + + + + ++ Servername + | ++ Map + | ++ Players + | + +
---|---|---|
+
+
+
+
+
+ {{.Name}} + |
+
+ {{.Map}} + |
+
+ {{.Players}}/{{.MaxPlayers}} + |
+