added template, fixed typos, added interval querying
This commit is contained in:
parent
7e62f366e7
commit
ae0b2b2890
10 changed files with 191 additions and 69 deletions
18
Makefile
Normal file
18
Makefile
Normal file
|
@ -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
|
58
cmd/main.go
58
cmd/main.go
|
@ -2,12 +2,25 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
"io/ioutil"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"git.snrd.de/Spaenny/postgres"
|
"git.snrd.de/Spaenny/steamServer/postgres"
|
||||||
"git.snrd.de/Spaenny/web"
|
"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() {
|
func main() {
|
||||||
dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable"
|
dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable"
|
||||||
|
|
||||||
|
@ -16,6 +29,45 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serverString := "s.spenny.tf"
|
||||||
|
|
||||||
|
c := cron.New()
|
||||||
|
c.AddFunc("*/1 * * * *", func() { updateServers(serverString) })
|
||||||
|
c.Start()
|
||||||
|
|
||||||
h := web.NewHandler(store)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
12
go.mod
Normal file
12
go.mod
Normal file
|
@ -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
|
||||||
|
)
|
17
go.sum
Normal file
17
go.sum
Normal file
|
@ -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=
|
46
main.go
46
main.go
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
|
@ -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 (
|
CREATE TABLE servers (
|
||||||
id UUID PRIMARY KEY,
|
id UUID PRIMARY KEY,
|
||||||
region_id UUID NOT NULL REFERENCES regions(id) ON DELETE CASCADE,
|
region_id UUID NOT NULL REFERENCES regions(id) ON DELETE CASCADE,
|
||||||
|
@ -8,14 +18,3 @@ CREATE TABLE servers (
|
||||||
players INT,
|
players INT,
|
||||||
max_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
|
|
||||||
);
|
|
||||||
|
|
|
@ -20,13 +20,22 @@ func (s *ServerStore) Server(id uuid.UUID) (steamServer.Server, error) {
|
||||||
return ser, nil
|
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 {
|
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) RETURNUNG *`,
|
||||||
ser.ID,
|
ser.ID,
|
||||||
ser.AppID,
|
ser.AppID,
|
||||||
ser.Name,
|
ser.Name,
|
||||||
ser.Map,
|
ser.Map,
|
||||||
ser.Player,
|
ser.Players,
|
||||||
ser.MaxPlayers); err != nil {
|
ser.MaxPlayers); err != nil {
|
||||||
return fmt.Errorf("error creating server: %w", err)
|
return fmt.Errorf("error creating server: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,16 +4,19 @@ import "github.com/google/uuid"
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
ID uuid.UUID `db:"id"`
|
ID uuid.UUID `db:"id"`
|
||||||
AppID int `json:"AppID"`
|
RegionID uuid.UUID `db:"region_id"`
|
||||||
Name string `json:"Name"`
|
GamemodeID uuid.UUID `db:"gamemode_id"`
|
||||||
Map string `json:"Map"`
|
AppID int `db:"app_id"`
|
||||||
Players int `json:"Players"`
|
Name string `db:"name"`
|
||||||
MaxPlayers int `json:"MaxPlayers"`
|
Map string `db:"map"`
|
||||||
|
Players int `db:"players"`
|
||||||
|
MaxPlayers int `db:"max_players"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerStore interface {
|
type ServerStore interface {
|
||||||
Server(id uuid.UUID) (Server, error)
|
Server(id uuid.UUID) (Server, error)
|
||||||
CreateServer(ser *Server) err
|
Servers() ([]Server, error)
|
||||||
|
CreateServer(ser *Server) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Store interface {
|
type Store interface {
|
||||||
|
|
58
templates/home.html
Normal file
58
templates/home.html
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
<html lang="en"><head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link href="https://unpkg.com/tailwindcss@1.0.4/dist/tailwind.min.css" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body class="h-screen overflow-hidden flex items-center justify-center" style="background: #edf2f7;">
|
||||||
|
|
||||||
|
<div class="container mx-auto px-4 sm:px-8">
|
||||||
|
<div class="py-8">
|
||||||
|
<div>
|
||||||
|
<h2 class="text-2xl font-semibold leading-tight">Serverlist</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="-mx-4 sm:-mx-8 px-4 sm:px-8 py-4 overflow-x-auto">
|
||||||
|
<div class="inline-block min-w-full shadow rounded-lg overflow-hidden">
|
||||||
|
<table class="min-w-full leading-normal">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
|
||||||
|
Servername
|
||||||
|
</th>
|
||||||
|
<th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
|
||||||
|
Map
|
||||||
|
</th>
|
||||||
|
<th class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
|
||||||
|
Players
|
||||||
|
</th>
|
||||||
|
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{range .Servers}}
|
||||||
|
<tr>
|
||||||
|
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
|
||||||
|
<div class="flex items-center">
|
||||||
|
|
||||||
|
<div class="ml-3">
|
||||||
|
<p class="text-gray-900 whitespace-no-wrap">{{.Name}}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
|
||||||
|
<p class="text-gray-900 whitespace-no-wrap">{{.Map}}</p>
|
||||||
|
</td>
|
||||||
|
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
|
||||||
|
<p class="text-gray-900 whitespace-no-wrap">{{.Players}}/{{.MaxPlayers}}</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -30,10 +30,10 @@ type Handler struct {
|
||||||
|
|
||||||
func (h *Handler) Home() http.HandlerFunc {
|
func (h *Handler) Home() http.HandlerFunc {
|
||||||
type data struct {
|
type data struct {
|
||||||
Servers []steamServer.Servers
|
Servers []steamServer.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/home.html"))
|
tmpl := template.Must(template.ParseFiles("templates/home.html"))
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
ss, err := h.store.Servers()
|
ss, err := h.store.Servers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Reference in a new issue