diff --git a/cmd/main.go b/cmd/main.go index dbf72ec..6fd7a3c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,7 @@ package main import ( + "time" "log" "net/http" @@ -24,20 +25,49 @@ func main() { log.Fatal("There is an issue with the databse: %w", err) } + var addedServers []string + for _, server := range addedServers { + println(server) + } for _, s := range ss { serverString := s.ServerIP - c := cron.New() - c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) }) - c.Start() + if !stringInSlice(s.ServerIP, addedServers) { + addedServers = append(addedServers, s.ServerIP) + c := cron.New() + c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) }) + c.Start() + } } + cronjob := cron.New() + cronjob.AddFunc("*/5 * * * *", func() { + for _, s := range ss { + serverString := s.ServerIP + if !stringInSlice(s.ServerIP, addedServers) { + addedServers = append(addedServers, s.ServerIP) + c := cron.New() + c.AddFunc("*/1 * * * *", func() { updateServers(serverString, store) }) + c.Start() + } + } + }) + cronjob.Start() h := web.NewHandler(store) http.ListenAndServe(":3000", h) } +func stringInSlice(a string, list []string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +} + func updateServers(host string, store steamServer.Store) { - client, err := a2s.NewClient(host) + client, err := a2s.NewClient(host, a2s.TimeoutOption(time.Second * 10)) if err != nil { log.Fatal("Creation of client failed: %w", err) @@ -50,6 +80,13 @@ func updateServers(host string, store steamServer.Store) { if err != nil { log.Printf("Querying failed: %s", err) + server := steamServer.Server { + Name: host, + Map: "Server didn't reply.", + Players: 0, + } + + store.UpdateServerByQuery(&server, host) return } diff --git a/postgres/gamemode_store.go b/postgres/gamemode_store.go index 17071d3..1b66a14 100644 --- a/postgres/gamemode_store.go +++ b/postgres/gamemode_store.go @@ -22,7 +22,7 @@ func (s *GamemodeStore) Gamemode(name string) (steamServer.Gamemode, error) { func (s *GamemodeStore) Gamemodes() ([]steamServer.Gamemode, error) { var gg []steamServer.Gamemode - var query = `SELECT * FROM gamemodes ORDER BY name ASC` + var query = `SELECT * FROM public.gamemodes ORDER BY CASE name WHEN 'Deathmatch' THEN 1 WHEN 'MGE' THEN 2 WHEN 'Jumpserver' THEN 3 WHEN 'Casual' THEN 4 END` if err := s.Select(&gg, query); err != nil { return []steamServer.Gamemode{}, fmt.Errorf("error gettings servers: %w", err) } diff --git a/templates/home.html b/templates/home.html index ff210f5..4e696a9 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,125 +1,70 @@ - - - Serverlist :: {{.Region}} - - - - - - - - - - -
-
- -
-

All Regions

-
-
-
-
- -
-

North America

-
-
-
-
- -
-

Europe

-
-
-
-
- -
-

Australia

-
-
-
-
- -
-

Asia

-
-
-
+{{define "title"}} +Serverlist :: {{.Region}} +{{end}} - {{ $gamemodes := .Gamemodes}} - {{ $servers := .Servers}} - {{ $flags := .Flags}} -
-
-
-

{{.Region}}

-
- {{range $gamemode := $gamemodes}} -
-
- - - - - - - - - - - - {{range $server := $servers}} - {{if eq $gamemode.ID $server.GamemodeID}} - - - - - - - {{end}} - {{end}} - -
- - - {{$gamemode.Name}} - - Map - - Players -
- {{range $flag := $flags}} - {{if eq $server.ServerIP $flag.ServerIP}} - - {{end}} - {{end}} - - - -

{{$server.Map}}

-
-

{{$server.Players}}/{{$server.MaxPlayers}}

-
-
-
- {{end}} +{{define "content"}} +{{ $gamemodes := .Gamemodes}} +{{ $servers := .Servers}} +{{ $flags := .Flags}} +
+
+
+

{{.Region}}

+ {{range $gamemode := $gamemodes}} +
+
+ + + + + + + + + + + + {{range $server := $servers}} + {{if eq $gamemode.ID $server.GamemodeID}} + + + + + + + {{end}} + {{end}} + +
+ + + {{$gamemode.Name}} + + Map + + Players +
+ {{range $flag := $flags}} + {{if eq $server.ServerIP $flag.ServerIP}} + + {{end}} + {{end}} + + + +

{{$server.Map}}

+
+

{{$server.Players}}/{{$server.MaxPlayers}}

+
+
+
+ {{end}}
- - +
+
+{{end}} diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..9f19158 --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,92 @@ + + + {{block "title" .}}{{end}} + + + + + + + + + + +
+
+ +
+

All Regions

+
+
+
+
+ +
+

North America

+
+
+
+
+ +
+

Europe

+
+
+
+
+ +
+

Australia

+
+
+
+
+ +
+

Asia

+
+
+
+ {{block "content" .}}{{end}} + + +
+
+ +

+ © 2021 Serverlist.tf +

+
+
+ diff --git a/templates/region.html b/templates/region.html index d6247bb..799adb1 100644 --- a/templates/region.html +++ b/templates/region.html @@ -1,130 +1,75 @@ - - - Serverlist :: {{.Region}} - - - - - - - - - - -
- - - - - +{{define "title"}} +Serverlist :: {{.Region}} +{{end}} - {{ $gamemodes := .Gamemodes}} - {{ $gamemodesCount := .GamemodeCount}} - {{ $servers := .Servers}} - {{ $flags := .Flags}} -
-
-
-

{{.Region}}

-
- {{range $gamemode := $gamemodes}} - {{range $gamemodeCount := $gamemodesCount}} - {{if eq $gamemode.ID $gamemodeCount.ID}} -
-
- - - - - - - - - - - - {{range $server := $servers}} - {{if eq $gamemode.ID $server.GamemodeID}} - - - - - - - {{end}} - {{end}} - -
- - - {{$gamemode.Name}} - - Map - - Players -
- {{range $flag := $flags}} - {{if eq $server.ServerIP $flag.ServerIP}} - - {{end}} - {{end}} - - - -

{{$server.Map}}

-
-

{{$server.Players}}/{{$server.MaxPlayers}}

-
-
-
- {{end}} - {{end}} - {{end}} +{{define "content"}} +{{ $gamemodes := .Gamemodes}} +{{ $gamemodesCount := .GamemodeCount}} +{{ $servers := .Servers}} +{{ $flags := .Flags}} +
+
+
+

{{.Region}}

+ {{range $gamemode := $gamemodes}} + {{range $gamemodeCount := $gamemodesCount}} + {{if eq $gamemode.ID $gamemodeCount.ID}} +
+
+ + + + + + + + + + + + {{range $server := $servers}} + {{if eq $gamemode.ID $server.GamemodeID}} + + + + + + + {{end}} + {{end}} + +
+ + + {{$gamemode.Name}} + + Map + + Players +
+ {{range $flag := $flags}} + {{if eq $server.ServerIP $flag.ServerIP}} + + {{end}} + {{end}} + + + +

{{$server.Map}}

+
+

{{$server.Players}}/{{$server.MaxPlayers}}

+
+
+
+ {{end}} + {{end}} + {{end}}
- - +
+
+{{end}} diff --git a/web/handler.go b/web/handler.go index 7530834..505906e 100644 --- a/web/handler.go +++ b/web/handler.go @@ -40,7 +40,7 @@ func (h *Handler) Home() http.HandlerFunc { Flags []steamServer.Flag } - tmpl := template.Must(template.ParseFiles("templates/home.html")) + tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/home.html")) return func(w http.ResponseWriter, r *http.Request) { ss, err := h.store.Servers() if err != nil { @@ -77,7 +77,7 @@ func (h *Handler) Europe() http.HandlerFunc { Flags []steamServer.Flag } - tmpl := template.Must(template.ParseFiles("templates/region.html")) + tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/region.html")) return func(w http.ResponseWriter, r *http.Request) { reg, err := h.store.Region("Europe") if err != nil { @@ -125,7 +125,7 @@ func (h *Handler) America() http.HandlerFunc { Flags []steamServer.Flag } - tmpl := template.Must(template.ParseFiles("templates/region.html")) + tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/region.html")) return func(w http.ResponseWriter, r *http.Request) { reg, err := h.store.Region("North America") if err != nil { @@ -173,7 +173,7 @@ func (h *Handler) Asia() http.HandlerFunc { Flags []steamServer.Flag } - tmpl := template.Must(template.ParseFiles("templates/region.html")) + tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/region.html")) return func(w http.ResponseWriter, r *http.Request) { reg, err := h.store.Region("Asia") if err != nil { @@ -221,7 +221,7 @@ func (h *Handler) Australia() http.HandlerFunc { Flags []steamServer.Flag } - tmpl := template.Must(template.ParseFiles("templates/region.html")) + tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/region.html")) return func(w http.ResponseWriter, r *http.Request) { reg, err := h.store.Region("Australia") if err != nil {