rename database to postgressql

This commit is contained in:
gitGnome
2026-04-13 08:06:41 +02:00
parent 554b7c338a
commit 6435206683
6 changed files with 29 additions and 30 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ func GetUserById(ctx context.Context, userId uuid.UUID) (*User, error) {
user, err := CacheGetUserById(userId) user, err := CacheGetUserById(userId)
if err != nil { if err != nil {
user = &User{Id: userId} user = &User{Id: userId}
err = DbGetWholeUser(ctx, user) err = PgGetWholeUser(ctx, user)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+5 -5
View File
@@ -70,7 +70,7 @@ func HttpHandleDm(response http.ResponseWriter, request *http.Request) {
Event: message, Event: message,
}) })
err = DbConnectionMessageSave(ctx, message) err = PgConnectionMessageSave(ctx, message)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
@@ -133,7 +133,7 @@ func HttpHandleUserGetConnectionMessages(response http.ResponseWriter, request *
if validBufCount > 0 { if validBufCount > 0 {
cutoff = buffer[0].CreatedAt cutoff = buffer[0].CreatedAt
} }
dbMessages, err := DbConnectionGetMessagesBefore(ctx, cutoff, connectionId, remaining) dbMessages, err := PgConnectionGetMessagesBefore(ctx, cutoff, connectionId, remaining)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
@@ -198,7 +198,7 @@ func HttpHandleUserNewConnection(response http.ResponseWriter, request *http.Req
RecipientId: recipient.Id, RecipientId: recipient.Id,
State: ConnectionState.Stranger, State: ConnectionState.Stranger,
} }
err = DbConnectionSave(ctx, connection) err = PgConnectionSave(ctx, connection)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
@@ -258,7 +258,7 @@ func HttpHandleUserDeleteConnection(response http.ResponseWriter, request *http.
return return
} }
err = DbConnectionDelete(ctx, conn) err = PgConnectionDelete(ctx, conn)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
@@ -309,7 +309,7 @@ func HttpHandleUserElevateConnection(response http.ResponseWriter, request *http
return return
} }
err = DbConnectionUpdateState(ctx, conn) err = PgConnectionUpdateState(ctx, conn)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
+6 -6
View File
@@ -35,11 +35,11 @@ func HttpHandleUserNewToken(response http.ResponseWriter, request *http.Request)
user, err = CacheGetUserByName(username) user, err = CacheGetUserByName(username)
if err != nil { if err != nil {
user = &User{Name: username} user = &User{Name: username}
if err = DbUserGetStandardInfoByName(ctx, user); err != nil { if err = PgUserGetStandardInfoByName(ctx, user); err != nil {
http.Error(response, "bad login", http.StatusUnauthorized) http.Error(response, "bad login", http.StatusUnauthorized)
return return
} }
if err = DbGetWholeUser(ctx, user); err != nil { if err = PgGetWholeUser(ctx, user); err != nil {
http.Error(response, err.Error(), http.StatusInternalServerError) http.Error(response, err.Error(), http.StatusInternalServerError)
return return
} }
@@ -99,7 +99,7 @@ func HttpHandleUserNew(response http.ResponseWriter, request *http.Request) {
ctx := request.Context() ctx := request.Context()
err = DbUserSave(ctx, newUser) err = PgUserSave(ctx, newUser)
if err != nil { if err != nil {
http.Error(response, "name taken", http.StatusUnauthorized) http.Error(response, "name taken", http.StatusUnauthorized)
return return
@@ -120,7 +120,7 @@ func HttpHandleUserDelete(response http.ResponseWriter, request *http.Request) {
return return
} }
err = DbUserDelete(ctx, userId) err = PgUserDelete(ctx, userId)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
@@ -149,7 +149,7 @@ func HttpHandleUserModifyAppearance(response http.ResponseWriter, request *http.
return return
} }
user.Color = color user.Color = color
err = DbUserSetColor(ctx, user) err = PgUserSetColor(ctx, user)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
@@ -177,7 +177,7 @@ func HttpHandleUserModifyAbout(response http.ResponseWriter, request *http.Reque
} }
user.Pronouns = pronouns user.Pronouns = pronouns
err = DbUserSetPronouns(ctx, user) err = PgUserSetPronouns(ctx, user)
if err != nil { if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError) http.Error(response, "internal server error", http.StatusInternalServerError)
return return
+1 -1
View File
@@ -15,7 +15,7 @@ func withCORS(h http.HandlerFunc) http.HandlerFunc {
func main() { func main() {
ctx := context.Background() ctx := context.Background()
DbInit(ctx) PgInit(ctx)
http.HandleFunc("/new/user", withCORS(HttpHandleUserNew)) http.HandleFunc("/new/user", withCORS(HttpHandleUserNew))
http.HandleFunc("/new/connection", withCORS(HttpHandleUserNewConnection)) http.HandleFunc("/new/connection", withCORS(HttpHandleUserNewConnection))
+16 -16
View File
@@ -11,7 +11,7 @@ import (
var dbConn *pgxpool.Pool var dbConn *pgxpool.Pool
func DbInit(ctx context.Context) { func PgInit(ctx context.Context) {
var err error var err error
dbConn, err = pgxpool.New(ctx, "postgres://master:secret@localhost:5432") // TODO change to env in production dbConn, err = pgxpool.New(ctx, "postgres://master:secret@localhost:5432") // TODO change to env in production
if err != nil { if err != nil {
@@ -65,7 +65,7 @@ func DbInit(ctx context.Context) {
} }
func DbUserSave(ctx context.Context, user *User) error { func PgUserSave(ctx context.Context, user *User) error {
err := dbConn.QueryRow(ctx, ` err := dbConn.QueryRow(ctx, `
INSERT INTO users (name, pass_hash, pronouns, rgba, created_at) INSERT INTO users (name, pass_hash, pronouns, rgba, created_at)
VALUES ($1, $2, $3, $4, $5) VALUES ($1, $2, $3, $4, $5)
@@ -75,14 +75,14 @@ func DbUserSave(ctx context.Context, user *User) error {
return err return err
} }
func DbUserDelete(ctx context.Context, id uuid.UUID) error { func PgUserDelete(ctx context.Context, id uuid.UUID) error {
_, err := dbConn.Exec(ctx, ` _, err := dbConn.Exec(ctx, `
DELETE FROM users WHERE id = $1 DELETE FROM users WHERE id = $1
`, id) `, id)
return err return err
} }
func DbUserGetStandardInfoByName(ctx context.Context, user *User) error { func PgUserGetStandardInfoByName(ctx context.Context, user *User) error {
var rgba int64 var rgba int64
err := dbConn.QueryRow(ctx, ` err := dbConn.QueryRow(ctx, `
SELECT id, name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE name = $1 SELECT id, name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE name = $1
@@ -93,7 +93,7 @@ func DbUserGetStandardInfoByName(ctx context.Context, user *User) error {
return err return err
} }
func DbUserGetById(ctx context.Context, user *User) error { func PgUserGetById(ctx context.Context, user *User) error {
var rgba int64 var rgba int64
err := dbConn.QueryRow(ctx, ` err := dbConn.QueryRow(ctx, `
SELECT name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE id = $1 SELECT name, pass_hash, COALESCE(pronouns, ''), rgba, created_at FROM users WHERE id = $1
@@ -104,25 +104,25 @@ func DbUserGetById(ctx context.Context, user *User) error {
return err return err
} }
func DbUserSetColor(ctx context.Context, user *User) error { func PgUserSetColor(ctx context.Context, user *User) error {
_, err := dbConn.Exec(ctx, ` _, err := dbConn.Exec(ctx, `
UPDATE users SET rgba = $1 WHERE id = $2 UPDATE users SET rgba = $1 WHERE id = $2
`, RgbaToUint32(user.Color), user.Id) `, RgbaToUint32(user.Color), user.Id)
return err return err
} }
func DbUserSetPronouns(ctx context.Context, user *User) error { func PgUserSetPronouns(ctx context.Context, user *User) error {
_, err := dbConn.Exec(ctx, ` _, err := dbConn.Exec(ctx, `
UPDATE users SET pronouns = $1 WHERE id = $2 UPDATE users SET pronouns = $1 WHERE id = $2
`, user.Pronouns, user.Id) `, user.Pronouns, user.Id)
return err return err
} }
func DbGetWholeUser(ctx context.Context, user *User) error { func PgGetWholeUser(ctx context.Context, user *User) error {
if err := DbUserGetById(ctx, user); err != nil { if err := PgUserGetById(ctx, user); err != nil {
return err return err
} }
if err := DbConnectionsGetBelongingToUser(ctx, user); err != nil { if err := PgConnectionsGetBelongingToUser(ctx, user); err != nil {
return err return err
} }
@@ -130,21 +130,21 @@ func DbGetWholeUser(ctx context.Context, user *User) error {
return nil return nil
} }
func DbConnectionSave(ctx context.Context, conn *Connection) error { func PgConnectionSave(ctx context.Context, conn *Connection) error {
return dbConn.QueryRow(ctx, ` return dbConn.QueryRow(ctx, `
INSERT INTO user_connections (requestor_id, recipient_id, state, created_at) VALUES ($1, $2, $3, $4) INSERT INTO user_connections (requestor_id, recipient_id, state, created_at) VALUES ($1, $2, $3, $4)
RETURNING id RETURNING id
`, conn.RequestorId, conn.RecipientId, conn.State, conn.CreatedAt).Scan(&conn.Id) `, conn.RequestorId, conn.RecipientId, conn.State, conn.CreatedAt).Scan(&conn.Id)
} }
func DbConnectionDelete(ctx context.Context, conn *Connection) error { func PgConnectionDelete(ctx context.Context, conn *Connection) error {
_, err := dbConn.Exec(ctx, ` _, err := dbConn.Exec(ctx, `
DELETE FROM user_connections WHERE id = $1 DELETE FROM user_connections WHERE id = $1
`, conn.Id) `, conn.Id)
return err return err
} }
func DbConnectionsGetBelongingToUser(ctx context.Context, user *User) error { func PgConnectionsGetBelongingToUser(ctx context.Context, user *User) error {
rows, err := dbConn.Query(ctx, ` rows, err := dbConn.Query(ctx, `
SELECT id, requestor_id, recipient_id, state, created_at SELECT id, requestor_id, recipient_id, state, created_at
FROM user_connections FROM user_connections
@@ -169,14 +169,14 @@ func DbConnectionsGetBelongingToUser(ctx context.Context, user *User) error {
return rows.Err() return rows.Err()
} }
func DbConnectionUpdateState(ctx context.Context, conn *Connection) error { func PgConnectionUpdateState(ctx context.Context, conn *Connection) error {
_, err := dbConn.Exec(ctx, ` _, err := dbConn.Exec(ctx, `
UPDATE user_connections SET state = $1 WHERE id = $2 UPDATE user_connections SET state = $1 WHERE id = $2
`, conn.State, conn.Id) `, conn.State, conn.Id)
return err return err
} }
func DbConnectionMessageSave(ctx context.Context, message *Message) error { func PgConnectionMessageSave(ctx context.Context, message *Message) error {
if message.Id != (uuid.UUID{}) { if message.Id != (uuid.UUID{}) {
_, err := dbConn.Exec(ctx, ` _, err := dbConn.Exec(ctx, `
INSERT INTO direct_messages (id, sender_id, receiver_id, created_at, content) VALUES ($1, $2, $3, $4, $5) INSERT INTO direct_messages (id, sender_id, receiver_id, created_at, content) VALUES ($1, $2, $3, $4, $5)
@@ -189,7 +189,7 @@ func DbConnectionMessageSave(ctx context.Context, message *Message) error {
`, message.Sender, message.Receiver, message.CreatedAt, message.Content).Scan(&message.Id) `, message.Sender, message.Receiver, message.CreatedAt, message.Content).Scan(&message.Id)
} }
func DbConnectionGetMessagesBefore(ctx context.Context, before time.Time, connection uuid.UUID, cap uint32) ([]*Message, error) { func PgConnectionGetMessagesBefore(ctx context.Context, before time.Time, connection uuid.UUID, cap uint32) ([]*Message, error) {
rows, err := dbConn.Query(ctx, ` rows, err := dbConn.Query(ctx, `
SELECT id, sender_id, receiver_id, created_at, content SELECT id, sender_id, receiver_id, created_at, content
FROM ( FROM (
-1
View File
@@ -1,5 +1,4 @@
media support media support
more user customization (avatar, banners, desc) more user customization (avatar, banners, desc)
add hubs add hubs