Skip to content

Commit 767fe04

Browse files
committed
Backend: add a file for globals in the db package.
1 parent 506d193 commit 767fe04

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

backend/db/globals.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package db
2+
3+
import "database/sql"
4+
5+
var db *sql.DB
6+
7+
type UserID = int

backend/db/main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package db
22

33
import (
4-
"database/sql"
54
"flag"
65
"fmt"
76
"log"
@@ -10,8 +9,6 @@ import (
109
"flowey/utils"
1110
)
1211

13-
var db *sql.DB
14-
1512
func Main(args []string) {
1613
defaultPath, err := utils.GetDefaultPath()
1714
if err != nil {

backend/db/session.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Credentials struct {
2222
}
2323

2424
func AuthenticateByCredentials(credentials Credentials) (int, error) {
25-
var userID int
25+
var userID UserID
2626
var hashedPassword string
2727

2828
query := `SELECT id, password FROM users WHERE username = ?`
@@ -44,7 +44,7 @@ func AuthenticateByCredentials(credentials Credentials) (int, error) {
4444
}
4545

4646
func AuthenticateBySessionKey(sessionKey string) (int, error) {
47-
var userID int
47+
var userID UserID
4848

4949
query := `SELECT user_id FROM sessions WHERE session_key = ?`
5050
err := db.QueryRow(query, sessionKey).Scan(&userID)
@@ -59,7 +59,7 @@ func AuthenticateBySessionKey(sessionKey string) (int, error) {
5959
return userID, nil
6060
}
6161

62-
func CreateSessionKey(userID int) (string, error) {
62+
func CreateSessionKey(userID UserID) (string, error) {
6363
byteSessionKey := make([]byte, 40)
6464
if _, err := rand.Read(byteSessionKey); err != nil {
6565
log.Println(err)

0 commit comments

Comments
 (0)