mirror of
https://github.com/zoriya/tide.git
synced 2026-05-31 01:36:21 +00:00
Setup postgresql
This commit is contained in:
+2
-2
@@ -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 .
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user