diff --git a/Dockerfile b/Dockerfile index fc625a4..647be41 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ RUN go mod download COPY . . # 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 FROM alpine:latest @@ -30,10 +30,10 @@ RUN mkdir /app WORKDIR /app/ # Copy the Pre-built binary file from the previous stage -COPY --from=builder /app/main . +COPY --from=builder /app . -# Expose port 8080 -EXPOSE 8080 +# Expose port 3000 +EXPOSE 3000 # Run Executable CMD ["./main"] diff --git a/cmd/main.go b/cmd/main.go index b2ba823..2a145cc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "log" - "fmt" "net/http" "time" "io/ioutil" @@ -23,7 +22,7 @@ type Server struct { } func main() { - dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable" + dsn := "postgres://postgres:secret@postgres:5432/postgres?sslmode=disable" store, err := postgres.NewStore(dsn) if err != nil { @@ -49,7 +48,7 @@ func main() { } 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 { Timeout: time.Second * 2, @@ -77,7 +76,6 @@ func updateServers(serverString string, store steamServer.Store) { server := steamServer.Server{} json.Unmarshal([]byte(body), &server) - store.UpdateServerByQuery(&server) - fmt.Println(&server) + store.UpdateServerByQuery(&server, serverString) } diff --git a/postgres/region_store.go b/postgres/region_store.go new file mode 100644 index 0000000..26959d3 --- /dev/null +++ b/postgres/region_store.go @@ -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 +} diff --git a/postgres/server_store.go b/postgres/server_store.go index 7a73bfe..8771d7f 100644 --- a/postgres/server_store.go +++ b/postgres/server_store.go @@ -29,6 +29,27 @@ func (s *ServerStore) Servers() ([]steamServer.Server, error) { 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 { if err := s.Get(ser, `INSERT INTO servers VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNUNG *`, ser.ID, @@ -45,13 +66,13 @@ func (s *ServerStore) CreateServer(ser *steamServer.Server) error { 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 *`, +func (s *ServerStore) UpdateServerByQuery(ser *steamServer.Server, server_ip string) error { + if err := s.Get(s, `UPDATE "servers" SET "name" = $1, "map" = $2, "players" = $3, "max_players" = $4 WHERE "server_ip" = $5 RETURNING *`, ser.Name, ser.Map, ser.Players, - ser.MaxPlayers); err != nil { + ser.MaxPlayers, + server_ip); err != nil { return fmt.Errorf("error updating server: %w", err) } return nil diff --git a/postgres/store.go b/postgres/store.go index 91244d5..c1e7dc2 100644 --- a/postgres/store.go +++ b/postgres/store.go @@ -20,11 +20,13 @@ func NewStore(dataSourceName string) (*Store, error) { return &Store { ServerStore: &ServerStore{DB: db}, + RegionStore: &RegionStore{DB: db}, }, nil } type Store struct { *ServerStore + *RegionStore } diff --git a/steamServer.go b/steamServer.go index c46b974..e067aeb 100644 --- a/steamServer.go +++ b/steamServer.go @@ -14,13 +14,25 @@ type Server struct { MaxPlayers int `db:"max_players"` } +type Region struct { + ID uuid.UUID `db:"id"` + Name string `db:"name"` +} + type ServerStore interface { Server(id uuid.UUID) (Server, error) Servers() ([]Server, error) + ServersByRegion(regionID uuid.UUID) ([]Server, error) + ServersByGamemode(gamemodeID uuid.UUID) ([]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 { ServerStore + RegionStore } diff --git a/templates/home.html b/templates/home.html index c78421e..ad28666 100644 --- a/templates/home.html +++ b/templates/home.html @@ -7,14 +7,14 @@
Europe