-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (52 loc) · 1.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"database/sql"
"fmt"
"net/url"
"github.com/gofiber/fiber/v2"
_ "github.com/jackc/pgx/v4/stdlib"
Routes "playlist.media/backend/Routes"
)
func GetFromDB(db *sql.DB) {
// Get from database
row := db.QueryRowContext(context.Background(), "SELECT password FROM users WHERE name='steve';")
if err := row.Err(); err != nil {
fmt.Println("db.QueryRowContext", err)
return
}
var password string
if err := row.Scan(&password); err != nil {
fmt.Println("row.Scan", err)
return
}
}
func main() {
app := fiber.New()
Routes.SetupRoutes(app)
dsn := url.URL{
Scheme: "postgres",
Host: "localhost:5432",
User: url.User("postgres"),
Path: "postgres",
}
q := dsn.Query()
q.Add("sslmode", "disable")
dsn.RawQuery = q.Encode()
db, err := sql.Open("pgx", dsn.String())
if err != nil {
fmt.Println("sql.Open", err)
return
}
defer func() {
_ = db.Close()
fmt.Println("Closed connection to database.")
}()
// Set in database
if _, err :=
db.ExecContext(context.Background(), "INSERT INTO users(name, password) VALUES('steve', 'his password');"); err != nil {
fmt.Println("db.ExecContext", err)
}
GetFromDB(db)
app.Listen(":3000")
}