Compare commits
5 Commits
0db285d4d4
...
1e42085ce6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e42085ce6 | ||
|
|
2b958fafe9 | ||
|
|
aa330dffea | ||
|
|
20db498168 | ||
|
|
0b1900a7e0 |
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -8,15 +9,28 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Port int
|
Port int
|
||||||
Root string
|
Root string
|
||||||
|
DB struct {
|
||||||
|
Scheme string
|
||||||
|
Conn string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() (Config, error) {
|
func NewConfig(ctx context.Context) (Config, error) {
|
||||||
var c Config
|
var c Config
|
||||||
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||||
|
|
||||||
fs.IntVar(&c.Port, "p", 8080, "port")
|
fs.IntVar(&c.Port, "p", 8080, "port")
|
||||||
fs.StringVar(&c.Root, "r", "/tmp/", "static file root")
|
fs.StringVar(&c.Root, "r", "/tmp/", "static file root")
|
||||||
|
fs.StringVar(&c.DB.Scheme, "db-scheme", "sqlite", "sql scheme")
|
||||||
|
fs.StringVar(&c.DB.Conn, "db-conn", "/tmp/out.sql", "sql conn string")
|
||||||
|
|
||||||
err := fs.Parse(os.Args[1:])
|
if err := fs.Parse(os.Args[1:]); err != nil {
|
||||||
return c, err
|
return Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := NewDB(ctx, c.DB.Scheme, c.DB.Conn); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|||||||
67
cmd/server/db.go
Normal file
67
cmd/server/db.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/glebarez/sqlite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DB struct {
|
||||||
|
scheme string
|
||||||
|
conn string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDB(ctx context.Context, scheme, conn string) (DB, error) {
|
||||||
|
ctx, can := context.WithTimeout(ctx, time.Second*10)
|
||||||
|
defer can()
|
||||||
|
|
||||||
|
db := DB{
|
||||||
|
scheme: scheme,
|
||||||
|
conn: conn,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql, err := db.dial(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return DB{}, err
|
||||||
|
}
|
||||||
|
defer sql.Close()
|
||||||
|
|
||||||
|
if _, err := sql.ExecContext(ctx, `
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
uuid TEXT,
|
||||||
|
name TEXT
|
||||||
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS games (
|
||||||
|
uuid TEXT
|
||||||
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS players (
|
||||||
|
user_uuid TEXT,
|
||||||
|
game_uuid TEXT
|
||||||
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS events (
|
||||||
|
game_uuid TEXT
|
||||||
|
);
|
||||||
|
`); err != nil {
|
||||||
|
return DB{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db DB) dial(ctx context.Context) (*sql.DB, error) {
|
||||||
|
c, err := sql.Open(db.scheme, db.conn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := c.PingContext(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db DB) GetParty(id string) (string, error) {
|
||||||
|
return "", io.EOF
|
||||||
|
}
|
||||||
@@ -22,7 +22,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run(ctx context.Context) error {
|
func run(ctx context.Context) error {
|
||||||
config, err := NewConfig()
|
config, err := NewConfig(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,16 +29,9 @@ func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
|||||||
return s.serveStatic(w, r)
|
return s.serveStatic(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isV1(r *http.Request) bool {
|
|
||||||
return strings.HasPrefix(r.URL.Path, "/v1/")
|
|
||||||
}
|
|
||||||
|
|
||||||
func isWS(r *http.Request) bool {
|
|
||||||
return r.URL.Path == "/ws" || strings.HasPrefix(r.URL.Path, "/ws/")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *S) serveStatic(w http.ResponseWriter, r *http.Request) error {
|
func (s *S) serveStatic(w http.ResponseWriter, r *http.Request) error {
|
||||||
http.FS(http.Dir(s.config.Root)).ServeHTTP(w, r)
|
fs := http.Dir(s.config.Root)
|
||||||
|
http.FileServer(fs).ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,23 +74,3 @@ func (s *S) Session(ctx context.Context) Session {
|
|||||||
v, _ := ctx.Value("session").(Session)
|
v, _ := ctx.Value("session").(Session)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *S) serveWS(httpw http.ResponseWriter, httpr *http.Request) error {
|
|
||||||
ctx := httpr.Context()
|
|
||||||
|
|
||||||
c, err := websocket.Accept(httpw, httpr, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer c.CloseNow()
|
|
||||||
|
|
||||||
if err := c.Write(ctx, 1, []byte("hello world")); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("not impl")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
|
|
||||||
return fmt.Errorf("not impl: v1")
|
|
||||||
}
|
|
||||||
|
|||||||
26
cmd/server/v1.go
Normal file
26
cmd/server/v1.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func isV1(r *http.Request) bool {
|
||||||
|
return strings.HasPrefix(r.URL.Path, "/v1/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
switch path.Join(r.Method, r.URL.Path) {
|
||||||
|
case "PUT/v1/state/" + s.Session(r.Context()).ID + "/party":
|
||||||
|
return s.serveV1PutParty(w, r)
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S) serveV1PutParty(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
return fmt.Errorf("not impl")
|
||||||
|
}
|
||||||
29
cmd/server/ws.go
Normal file
29
cmd/server/ws.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func isWS(r *http.Request) bool {
|
||||||
|
return r.URL.Path == "/ws" || strings.HasPrefix(r.URL.Path, "/ws/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *S) serveWS(httpw http.ResponseWriter, httpr *http.Request) error {
|
||||||
|
ctx := httpr.Context()
|
||||||
|
|
||||||
|
c, err := websocket.Accept(httpw, httpr, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer c.CloseNow()
|
||||||
|
|
||||||
|
if err := c.Write(ctx, 1, []byte("hello world")); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("not impl")
|
||||||
|
}
|
||||||
18
go.mod
18
go.mod
@@ -4,4 +4,20 @@ go 1.22.3
|
|||||||
|
|
||||||
require golang.org/x/time v0.8.0
|
require golang.org/x/time v0.8.0
|
||||||
|
|
||||||
require github.com/coder/websocket v1.8.12 // indirect
|
require (
|
||||||
|
github.com/coder/websocket v1.8.12 // indirect
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||||
|
github.com/glebarez/sqlite v1.11.0 // indirect
|
||||||
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
golang.org/x/sys v0.7.0 // indirect
|
||||||
|
gorm.io/gorm v1.25.7 // indirect
|
||||||
|
modernc.org/libc v1.22.5 // indirect
|
||||||
|
modernc.org/mathutil v1.5.0 // indirect
|
||||||
|
modernc.org/memory v1.5.0 // indirect
|
||||||
|
modernc.org/sqlite v1.23.1 // indirect
|
||||||
|
)
|
||||||
|
|||||||
30
go.sum
30
go.sum
@@ -1,4 +1,34 @@
|
|||||||
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
|
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
|
||||||
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
|
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
|
||||||
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
|
||||||
|
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
||||||
|
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
|
||||||
|
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
|
||||||
|
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/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||||
|
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||||
|
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||||
|
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
|
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
|
||||||
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||||
|
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
|
||||||
|
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||||
|
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
|
||||||
|
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
|
||||||
|
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
|
||||||
|
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
|
||||||
|
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
|
||||||
|
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
|
||||||
|
modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM=
|
||||||
|
modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
|
||||||
|
|||||||
Reference in New Issue
Block a user