switch to postgres db

This commit is contained in:
AustrianToast 2024-05-18 14:36:44 +02:00
parent 35312f3c22
commit cb437f3bb6
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4
3 changed files with 71 additions and 72 deletions

8
go.mod
View File

@ -7,6 +7,14 @@ require (
github.com/jaswdr/faker v1.19.1
)
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
golang.org/x/sync v0.1.0 // indirect
)
require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect

11
go.sum
View File

@ -28,6 +28,14 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jaswdr/faker v1.19.1 h1:xBoz8/O6r0QAR8eEvKJZMdofxiRH+F0M/7MU9eNKhsM=
github.com/jaswdr/faker v1.19.1/go.mod h1:x7ZlyB1AZqwqKZgyQlnqEG8FDptmHlncA5u2zY/yi6w=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@ -74,6 +82,8 @@ golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
@ -86,6 +96,7 @@ google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFW
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

124
main.go
View File

@ -1,7 +1,7 @@
package main
import (
"database/sql"
"context"
"fmt"
"io"
"log"
@ -10,8 +10,8 @@ import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jaswdr/faker"
_ "github.com/mattn/go-sqlite3"
)
// This is taken from our DB model
@ -24,36 +24,58 @@ type video struct {
var videos []video
func main() {
// taken from https://github.com/mattn/go-sqlite3/blob/v1.14.22/_example/simple/simple.go
db := initDb()
if db == nil {
log.Fatal("Couldn't create DB")
return
}
defer db.Close()
defer os.Remove("./videos.db")
rows, err := db.Query("select * from videos")
dbpool, err := pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.2:5432/postgres")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
defer dbpool.Close()
sqlStmt := `
DROP TABLE IF EXISTS public.videos;
CREATE TABLE IF NOT EXISTS public.videos
(
id integer NOT NULL,
filepath text,
CONSTRAINT videos_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.videos
OWNER to postgres;
`
_, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil {
log.Fatalf("Unable to connection to database: %v\n", err)
}
faker := faker.New()
for i := 0; i < 10; i++ {
sqlStmt = fmt.Sprintf("insert into public.videos(id, filepath) values(%d, '%s')", i, faker.File().AbsoluteFilePathForUnix(2))
_, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil {
log.Fatalf("Unable to add videos: %v\n", err)
}
}
rows, _ := dbpool.Query(context.Background(), "select * from videos")
for rows.Next() {
var id int
var name string
err = rows.Scan(&id, &name)
if err != nil {
log.Fatal(err)
log.Fatalf("Unable to list videos: %v\n", err)
}
fmt.Println(id, name)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
log.Fatalf("Unable to list videos: %v\n", err)
}
// Legacy
faker := faker.New()
for i := 0; i < 10; i++ {
videos = append(videos, video{ID: i, Filepath: faker.File().AbsoluteFilePathForUnix(5)})
}
@ -72,49 +94,6 @@ func main() {
router.Run("localhost:8080")
}
func initDb() *sql.DB {
os.Remove("./videos.db")
db, err := sql.Open("sqlite3", "./videos.db")
if err != nil {
log.Fatal(err)
}
sqlStmt := `
create table videos (id integer not null primary key, filepath text);
delete from videos;
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return nil
}
faker := faker.New()
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("insert into videos(id, filepath) values(?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
for i := 0; i < 100; i++ {
_, err = stmt.Exec(i, faker.File().AbsoluteFilePathForUnix(5))
if err != nil {
log.Fatal(err)
}
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
return db
}
func remove(slice []video, s int) []video {
return append(slice[:s], slice[s+1:]...)
}
@ -130,20 +109,21 @@ func ReceiveFile(c *gin.Context) {
data, err := io.ReadAll(c.Request.Body)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Unknown Error")
} else {
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write(data); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
c.IndentedJSON(http.StatusOK, "Received chunk")
return
}
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write(data); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
c.IndentedJSON(http.StatusOK, "Received chunk")
}
// getvideos responds with the list of all videos as JSON.