forked from mtusi2026/mtusi2026lab3
Lab3 baseline: HAProxy, replicated stateless API, shared PostgreSQL/Valkey, idempotent messages
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// Package model holds the public JSON shapes of the lab3 contract. They are
|
||||
// deliberately separate from the database scan helpers so an accidental new
|
||||
// column cannot silently become an API field.
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// Time marshals an RFC 3339 UTC timestamp with exactly six fractional digits.
|
||||
// Go's default time.Time JSON encoder trims trailing zeros, which produces
|
||||
// 3-5 digit fractions that some Python 3.9 datetime parsers (used by the
|
||||
// public test client) reject. A fixed microsecond precision matches
|
||||
// PostgreSQL timestamptz and stays valid RFC 3339.
|
||||
type Time struct{ time.Time }
|
||||
|
||||
const timestampLayout = "2006-01-02T15:04:05.000000Z"
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"` + t.UTC().Format(timestampLayout) + `"`), nil
|
||||
}
|
||||
|
||||
// User is the public user shape. It never contains a password hash or session
|
||||
// data.
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// Chat is the public chat shape. MemberIDs preserves the order supplied at
|
||||
// creation; membership is immutable in the mandatory lab3 API.
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// Message is the public message shape. SenderID always comes from the server
|
||||
// session, never from the request body.
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// ChatPage is one keyset page of accessible chats.
|
||||
type ChatPage struct {
|
||||
Items []Chat `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// MessagePage is one keyset page of a chat history.
|
||||
type MessagePage struct {
|
||||
Items []Message `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// Session is the login response. Token is only ever written to the response
|
||||
// body, never to logs or PostgreSQL.
|
||||
type Session struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt Time `json:"expires_at"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
Reference in New Issue
Block a user