diff --git a/.env.example b/.env.example index 67772a4..86c366e 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,12 @@ RPC_SECRET=wertyui PUID=$UID PGID=$GID +ARIA_URI=aria2c:6800 +# Database things +POSTGRES_USER=tideUser +POSTGRES_PASSWORD=tidePassword +POSTGRES_DB=tideDb +POSTGRES_SERVER=postgres +POSTGRES_PORT=5432 + # vi: ft=sh diff --git a/api/Dockerfile.dev b/api/Dockerfile.dev index 18b034e..d61f67c 100644 --- a/api/Dockerfile.dev +++ b/api/Dockerfile.dev @@ -1,6 +1,6 @@ FROM golang:1.20-alpine -RUN go install github.com/mitranim/gow@latest +RUN go install github.com/bokwoon95/wgo@latest WORKDIR /app EXPOSE 7890 -CMD gow -r=false run . +CMD wgo run . diff --git a/api/go.mod b/api/go.mod index 8512699..76fee6b 100644 --- a/api/go.mod +++ b/api/go.mod @@ -6,5 +6,6 @@ require ( github.com/cenkalti/hub v1.0.1-0.20160527103212-11382a9960d3 // indirect github.com/cenkalti/rpc2 v0.0.0-20180727162946-9642ea02d0aa // indirect github.com/gorilla/websocket v1.4.1 // indirect + github.com/lib/pq v1.10.9 // indirect github.com/siku2/arigo v0.2.0 // indirect ) diff --git a/api/go.sum b/api/go.sum index 6da0502..1c2f564 100644 --- a/api/go.sum +++ b/api/go.sum @@ -6,6 +6,8 @@ github.com/cenkalti/rpc2 v0.0.0-20180727162946-9642ea02d0aa/go.mod h1:v2npkhrXyk github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/siku2/arigo v0.2.0 h1:gB5zGgCNtRd83IrdeimL+Jp5Yhj9wKo/DhAvVXh/l4k= github.com/siku2/arigo v0.2.0/go.mod h1:/slSGOCL5awvY4Q9SlyR15wBjeGllHLrLE1E/2MztMQ= diff --git a/api/models/item.go b/api/models/item.go new file mode 100644 index 0000000..7ae7832 --- /dev/null +++ b/api/models/item.go @@ -0,0 +1,30 @@ +package models + +import "time"; + +type State string +const ( + Stale State = "stale" + Downloading State = "downloading" + Seeding State = "seeding" + Finished State = "finished" +) + +type File struct { + Name string + Priority int + Size uint64 + AvailableSize uint64 + Path string +} + +type Item struct { + Id string + Name string + State State + Size uint64 + AvailableSize uint64 + Path string + AddedDate time.Time + Files []File +} diff --git a/api/services/aria2.go b/api/services/aria2.go new file mode 100644 index 0000000..765cd4a --- /dev/null +++ b/api/services/aria2.go @@ -0,0 +1,38 @@ +package services + +import ( + "fmt" + "os" + models "tide/api/models" + + "github.com/siku2/arigo" +) + +type Service interface { + AddItem(item string) + List() []models.Item +} + +type Aria2 struct { + client *arigo.Client + token string +} + +func NewAria2() (*Aria2, error) { + p := new(Aria2) + p.token = os.Getenv("RPC_SECRET") + c, err := arigo.Dial(fmt.Sprintf("ws://%v/jsonrpc", os.Getenv("ARIA_URI")), p.token) + if err != nil { + return nil, err + } + p.client = &c + return p, nil +} + +func (x *Aria2) AddItem(item string) { + x.client.AddURI([]string{item}, nil) +} + +func (x *Aria2) List() []models.Item { + return make([]models.Item, 0) +} diff --git a/api/services/database.go b/api/services/database.go new file mode 100644 index 0000000..4f67c75 --- /dev/null +++ b/api/services/database.go @@ -0,0 +1,40 @@ +package services + +import ( + "database/sql" + "fmt" + "os" + "tide/api/models" + + _ "github.com/lib/pq" +) + +type Database struct { + Connection *sql.DB +} + +func NewDatabase() (*Database, error) { + d := new(Database) + con := fmt.Sprintf( + "postgresql://%v:%v@%v:%v/%v", + os.Getenv("POSTGRES_USER"), + os.Getenv("POSTGRES_PASSWORD"), + os.Getenv("POSTGRES_SERVER"), + os.Getenv("POSTGRES_PORT"), + os.Getenv("POSTGRES_DB"), + ) + db, err := sql.Open("posgres", con) + if err != nil { + return nil, err + } + d.Connection = db + return d, nil +} + +func (d *Database) AddItem(item *models.Item) (*models.Item, error) { + _, err := d.Connection.Exec("INSERT INTO items (id) VALUES (?)", item.Id) + if err != nil { + return nil, err + } + return item, nil +} diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 319640e..3125192 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -21,3 +21,16 @@ services: restart: on-failure env_file: - .env + + postgres: + image: postgres:15 + restart: on-failure + env_file: + - ./.env + volumes: + - db:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] + interval: 5s + timeout: 5s + retries: 5 diff --git a/docker-compose.yml b/docker-compose.yml index 61771f2..f32106c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,9 +9,19 @@ services: aria2: image: p3terx/aria2-pro - ports: - - "6888:6888" - - "6888:6888/udp" restart: on-failure env_file: - .env + + postgres: + image: postgres:15 + restart: on-failure + env_file: + - ./.env + volumes: + - db:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] + interval: 5s + timeout: 5s + retries: 5 diff --git a/shell.nix b/shell.nix index d83b1c2..b0ef626 100644 --- a/shell.nix +++ b/shell.nix @@ -2,6 +2,7 @@ pkgs.mkShell { packages = with pkgs; [ go + wgo aria ]; }