Initial commit

This commit is contained in:
2026-05-12 15:40:22 -06:00
parent 5b279865a1
commit 1eac72b3cd
31 changed files with 1192 additions and 45 deletions
+47
View File
@@ -0,0 +1,47 @@
package db
import (
"context"
"time"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// Client wraps the mongo client and exposes typed collection accessors.
type Client struct {
client *mongo.Client
db *mongo.Database
}
func Connect(ctx context.Context, uri string) (*Client, error) {
opts := options.Client().ApplyURI(uri)
c, err := mongo.Connect(opts)
if err != nil {
return nil, err
}
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := c.Ping(pingCtx, nil); err != nil {
return nil, err
}
return &Client{client: c, db: c.Database("logjensticks")}, nil
}
func (c *Client) Disconnect(ctx context.Context) error {
return c.client.Disconnect(ctx)
}
func (c *Client) Users() *mongo.Collection {
return c.db.Collection("users")
}
func (c *Client) Sessions() *mongo.Collection {
return c.db.Collection("sessions")
}
func (c *Client) Lanes() *mongo.Collection {
return c.db.Collection("lanes")
}
+62
View File
@@ -0,0 +1,62 @@
package db
import (
"context"
"time"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// EnsureIndexes creates all required indexes. Safe to call on every startup
// (mongo ignores duplicate index creation).
func (c *Client) EnsureIndexes(ctx context.Context) error {
if err := c.ensureUserIndexes(ctx); err != nil {
return err
}
if err := c.ensureSessionIndexes(ctx); err != nil {
return err
}
return c.ensureLaneIndexes(ctx)
}
func (c *Client) ensureLaneIndexes(ctx context.Context) error {
_, err := c.Lanes().Indexes().CreateOne(ctx, mongo.IndexModel{
Keys: bson.D{{Key: "created_by", Value: 1}},
})
return err
}
func (c *Client) ensureUserIndexes(ctx context.Context) error {
_, err := c.Users().Indexes().CreateOne(ctx, mongo.IndexModel{
Keys: bson.D{{Key: "username", Value: 1}},
Options: options.Index().SetUnique(true),
})
return err
}
func (c *Client) ensureSessionIndexes(ctx context.Context) error {
// Unique lookup index on the session token.
if _, err := c.Sessions().Indexes().CreateOne(ctx, mongo.IndexModel{
Keys: bson.D{{Key: "token", Value: 1}},
Options: options.Index().SetUnique(true),
}); err != nil {
return err
}
// TTL index: MongoDB automatically deletes session documents after
// expires_at has passed (checked roughly every 60 s by the reaper).
expireAfter := int32(0)
if _, err := c.Sessions().Indexes().CreateOne(ctx, mongo.IndexModel{
Keys: bson.D{{Key: "expires_at", Value: 1}},
Options: options.Index().SetExpireAfterSeconds(expireAfter),
}); err != nil {
return err
}
return nil
}
// SessionTTL is how long a session stays valid.
const SessionTTL = 24 * time.Hour
+41
View File
@@ -0,0 +1,41 @@
package db
import (
"context"
"log"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"golang.org/x/crypto/bcrypt"
)
// SeedBroker inserts a default broker account if no users exist.
// Credentials are taken from environment variables SEED_USERNAME and
// SEED_PASSWORD; if unset they fall back to dev defaults and a warning
// is printed. This function must not run in production without those vars set.
func (c *Client) SeedBroker(ctx context.Context, username, password string) error {
count, err := c.Users().CountDocuments(ctx, bson.D{})
if err != nil {
return err
}
if count > 0 {
return nil // already seeded
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
_, err = c.Users().InsertOne(ctx, bson.D{
{Key: "username", Value: username},
{Key: "password_hash", Value: string(hash)},
{Key: "role", Value: "broker"},
})
if err != nil && !mongo.IsDuplicateKeyError(err) {
return err
}
log.Printf("db: seeded broker user %q", username)
return nil
}