added region support, fixed minor bugs
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Philipp 2021-09-20 22:55:12 +02:00
parent 4807355add
commit 77b88325cf
8 changed files with 121 additions and 16 deletions

View File

@ -18,7 +18,7 @@ RUN go mod download
COPY . . COPY . .
# Build the Go app # Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd/main.go
# GO Repo base repo # GO Repo base repo
FROM alpine:latest FROM alpine:latest
@ -30,10 +30,10 @@ RUN mkdir /app
WORKDIR /app/ WORKDIR /app/
# Copy the Pre-built binary file from the previous stage # Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/main . COPY --from=builder /app .
# Expose port 8080 # Expose port 3000
EXPOSE 8080 EXPOSE 3000
# Run Executable # Run Executable
CMD ["./main"] CMD ["./main"]

View File

@ -2,7 +2,6 @@ package main
import ( import (
"log" "log"
"fmt"
"net/http" "net/http"
"time" "time"
"io/ioutil" "io/ioutil"
@ -23,7 +22,7 @@ type Server struct {
} }
func main() { func main() {
dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable" dsn := "postgres://postgres:secret@postgres:5432/postgres?sslmode=disable"
store, err := postgres.NewStore(dsn) store, err := postgres.NewStore(dsn)
if err != nil { if err != nil {
@ -49,7 +48,7 @@ func main() {
} }
func updateServers(serverString string, store steamServer.Store) { func updateServers(serverString string, store steamServer.Store) {
url := "http://api.spenny.eu/server/" + serverString url := "http://steam-server-api:8080/server/" + serverString
reqClient := http.Client { reqClient := http.Client {
Timeout: time.Second * 2, Timeout: time.Second * 2,
@ -77,7 +76,6 @@ func updateServers(serverString string, store steamServer.Store) {
server := steamServer.Server{} server := steamServer.Server{}
json.Unmarshal([]byte(body), &server) json.Unmarshal([]byte(body), &server)
store.UpdateServerByQuery(&server) store.UpdateServerByQuery(&server, serverString)
fmt.Println(&server)
} }

20
postgres/region_store.go Normal file
View File

@ -0,0 +1,20 @@
package postgres
import (
"fmt"
"git.snrd.de/Spaenny/steamServer"
"github.com/jmoiron/sqlx"
)
type RegionStore struct {
*sqlx.DB
}
func (s *RegionStore) Region(name string) (steamServer.Region, error) {
var r steamServer.Region
if err := s.Get(&r, `SELECT * FROM regions WHERE name = $1`, name); err != nil {
return steamServer.Region{}, fmt.Errorf("error getting region id: %w", err)
}
return r, nil
}

View File

@ -29,6 +29,27 @@ func (s *ServerStore) Servers() ([]steamServer.Server, error) {
return ss, nil return ss, nil
} }
func (s *ServerStore) ServersByRegion(regionID uuid.UUID) ([]steamServer.Server, error) {
var ss []steamServer.Server
var query = `
SELECT * FROM servers WHERE region_id = $1`
if err := s.Select(&ss, query, regionID); err != nil {
return []steamServer.Server{}, fmt.Errorf("error gettings region servers: %w", err)
}
return ss, nil
}
func (s *ServerStore) ServersByGamemode(gamemodeID uuid.UUID) ([]steamServer.Server, error) {
var ss []steamServer.Server
var query = `SELECT * FROM servers WHERE gamemode_id = $1`
if err := s.Select(&ss, query, gamemodeID); err != nil {
return []steamServer.Server{}, fmt.Errorf("error gettings gamemode 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, $7, $8, $9) RETURNUNG *`, if err := s.Get(ser, `INSERT INTO servers VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNUNG *`,
ser.ID, ser.ID,
@ -45,13 +66,13 @@ func (s *ServerStore) CreateServer(ser *steamServer.Server) error {
return nil return nil
} }
func (s *ServerStore) UpdateServerByQuery(ser *steamServer.Server) error { func (s *ServerStore) UpdateServerByQuery(ser *steamServer.Server, server_ip string) 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 "server_ip" = $5 RETURNING *`,
if err := s.Get(s, `UPDATE "servers" SET "name" = $1, "map" = $2, "players" = $3, "max_players" = $4 WHERE "name" = $1 RETURNING *`,
ser.Name, ser.Name,
ser.Map, ser.Map,
ser.Players, ser.Players,
ser.MaxPlayers); err != nil { ser.MaxPlayers,
server_ip); err != nil {
return fmt.Errorf("error updating server: %w", err) return fmt.Errorf("error updating server: %w", err)
} }
return nil return nil

View File

@ -20,11 +20,13 @@ func NewStore(dataSourceName string) (*Store, error) {
return &Store { return &Store {
ServerStore: &ServerStore{DB: db}, ServerStore: &ServerStore{DB: db},
RegionStore: &RegionStore{DB: db},
}, nil }, nil
} }
type Store struct { type Store struct {
*ServerStore *ServerStore
*RegionStore
} }

View File

@ -14,13 +14,25 @@ type Server struct {
MaxPlayers int `db:"max_players"` MaxPlayers int `db:"max_players"`
} }
type Region struct {
ID uuid.UUID `db:"id"`
Name string `db:"name"`
}
type ServerStore interface { type ServerStore interface {
Server(id uuid.UUID) (Server, error) Server(id uuid.UUID) (Server, error)
Servers() ([]Server, error) Servers() ([]Server, error)
ServersByRegion(regionID uuid.UUID) ([]Server, error)
ServersByGamemode(gamemodeID uuid.UUID) ([]Server, error)
CreateServer(ser *Server) error CreateServer(ser *Server) error
UpdateServerByQuery(ser *Server) error UpdateServerByQuery(ser *Server, server_ip string) error
}
type RegionStore interface {
Region(name string) (Region, error)
} }
type Store interface { type Store interface {
ServerStore ServerStore
RegionStore
} }

View File

@ -7,14 +7,14 @@
<body class="h-screen overflow-hidden flex items-center justify-center" style="background: #edf2f7;"> <body class="h-screen overflow-hidden flex items-center justify-center" style="background: #edf2f7;">
<section class="container mx-auto px-8 my-1 flex flex-wrap -m-4"> <section class="container mx-auto px-8 my-1 flex flex-wrap -m-4">
<div class="p-2 md:w-40"> <div class="p-2 md:w-40">
<a href="#" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100"> <a href="/america" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div> <div>
<p class=" text-xs font-medium ">North America</p> <p class=" text-xs font-medium ">North America</p>
</div> </div>
</a> </a>
</div> </div>
<div class="p-2 md:w-40"> <div class="p-2 md:w-40">
<a href="#" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100"> <a href="/europe" class="flex items-center p-4 bg-blue-200 rounded-lg shadow-xs cursor-pointer hover:bg-blue-500 hover:text-gray-100">
<div> <div>
<p class=" text-xs font-medium ">Europe</p> <p class=" text-xs font-medium ">Europe</p>
</div> </div>

View File

@ -18,6 +18,8 @@ func NewHandler(store steamServer.Store) *Handler {
h.Use(middleware.Logger) h.Use(middleware.Logger)
h.Get("/", h.Home()) h.Get("/", h.Home())
h.Get("/europe", h.Europe())
h.Get("/america", h.America())
return h return h
} }
@ -46,3 +48,53 @@ func (h *Handler) Home() http.HandlerFunc {
}) })
} }
} }
func (h *Handler) Europe() http.HandlerFunc {
type data struct {
Servers []steamServer.Server
}
tmpl := template.Must(template.ParseFiles("templates/home.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("Europe")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ss, err := h.store.ServersByRegion(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, data {
Servers: ss,
})
}
}
func (h *Handler) America() http.HandlerFunc {
type data struct {
Servers []steamServer.Server
}
tmpl := template.Must(template.ParseFiles("templates/home.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("North America")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ss, err := h.store.ServersByRegion(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, data {
Servers: ss,
})
}
}