Bel LaPointe 2024-12-14 20:45:29 -07:00
parent 20db498168
commit aa330dffea
3 changed files with 39 additions and 2 deletions

View File

@ -1,8 +1,11 @@
package main
import (
"context"
"database/sql"
"flag"
"os"
"time"
)
type Config struct {
@ -20,3 +23,36 @@ func NewConfig() (Config, error) {
err := fs.Parse(os.Args[1:])
return c, err
}
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 sql != nil {
sql.Close()
}
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
}

View File

@ -30,7 +30,8 @@ func (s *S) serveHTTP(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
}

View File

@ -13,7 +13,7 @@ func isV1(r *http.Request) bool {
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).ID + "/party":
case "PUT/v1/state/" + s.Session(r.Context()).ID + "/party":
return s.serveV1PutParty(w, r)
default:
http.NotFound(w, r)