first commit

This commit is contained in:
Philipp 2021-09-20 14:57:24 +02:00
commit 7e62f366e7
8 changed files with 224 additions and 0 deletions

21
cmd/main.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"log"
"net/http"
"git.snrd.de/Spaenny/postgres"
"git.snrd.de/Spaenny/web"
)
func main() {
dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable"
store, err := postgres.NewStore(dsn)
if err != nil {
log.Fatal(err)
}
h := web.NewHandler(store)
http.ListenAndServe(":3000, h)
}

46
main.go Normal file
View File

@ -0,0 +1,46 @@
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)
}

View File

@ -0,0 +1,3 @@
DROP TABLE servers;
DROP TABLE regions;
DROP TABLE gamemodes;

View File

@ -0,0 +1,21 @@
CREATE TABLE servers (
id UUID PRIMARY KEY,
region_id UUID NOT NULL REFERENCES regions(id) ON DELETE CASCADE,
gamemode_id UUID NOT NULL REFERENCES gamemodes(id) ON DELETE CASCADE,
app_id INT NOT NULL,
name TEXT NOT NULL,
map TEXT NOT NULL,
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
);

34
postgres/server_store.go Normal file
View File

@ -0,0 +1,34 @@
package postgres
import (
"fmt"
"git.snrd.de/Spaenny/steamServer"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
type ServerStore struct {
*sqlx.DB
}
func (s *ServerStore) Server(id uuid.UUID) (steamServer.Server, error) {
var ser steamServer.Server
if err := s.Get(&ser, `SELECT * FROM servers WHERE id = $1`, id); err != nil {
return steamServer.Server{}, fmt.Errorf("error getting server: %w", err)
}
return ser, 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.MaxPlayers); err != nil {
return fmt.Errorf("error creating server: %w", err)
}
return nil
}

30
postgres/store.go Normal file
View File

@ -0,0 +1,30 @@
package postgres
import (
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
func NewStore(dataSourceName string) (*Store, error) {
db, err := sqlx.Open("postgres", dataSourceName)
if err != nil {
return nil, fmt.Errorf("error opening database %w", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("error connecting to database %w", err)
}
return &Store {
ServerStore: &ServerStore{DB: db},
}, nil
}
type Store struct {
*ServerStore
}

21
steamServer.go Normal file
View File

@ -0,0 +1,21 @@
package steamServer
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"`
}
type ServerStore interface {
Server(id uuid.UUID) (Server, error)
CreateServer(ser *Server) err
}
type Store interface {
ServerStore
}

48
web/handler.go Normal file
View File

@ -0,0 +1,48 @@
package web
import (
"html/template"
"net/http"
"git.snrd.de/Spaenny/steamServer"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func NewHandler(store steamServer.Store) *Handler {
h := &Handler {
Mux: chi.NewMux(),
store: store,
}
h.Use(middleware.Logger)
h.Get("/", h.Home())
return h
}
type Handler struct {
*chi.Mux
store steamServer.Store
}
func (h *Handler) Home() http.HandlerFunc {
type data struct {
Servers []steamServer.Servers
}
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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, data {
Servers: ss,
})
}
}