config: update more configuration options

This commit is contained in:
Tigor Hutasuhut 2024-04-09 23:17:23 +07:00
parent 9e71c3cbfa
commit 1b4e0c7669
4 changed files with 37 additions and 8 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/teivah/broadcast" "github.com/teivah/broadcast"
"github.com/tigorlazuardi/redmage/config"
"github.com/tigorlazuardi/redmage/db/queries" "github.com/tigorlazuardi/redmage/db/queries"
"github.com/tigorlazuardi/redmage/pkg/errs" "github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log" "github.com/tigorlazuardi/redmage/pkg/log"
@ -20,15 +21,18 @@ type API struct {
scheduleMap map[cron.EntryID]queries.Subreddit scheduleMap map[cron.EntryID]queries.Subreddit
downloadBroadcast *broadcast.Relay[DownloadStatusMessage] downloadBroadcast *broadcast.Relay[DownloadStatusMessage]
config *config.Config
} }
func New(q *queries.Queries, db *sql.DB) *API { func New(q *queries.Queries, db *sql.DB, cfg *config.Config) *API {
return &API{ return &API{
queries: q, queries: q,
db: db, db: db,
scheduler: cron.New(), scheduler: cron.New(),
scheduleMap: make(map[cron.EntryID]queries.Subreddit, 8), scheduleMap: make(map[cron.EntryID]queries.Subreddit, 8),
downloadBroadcast: broadcast.NewRelay[DownloadStatusMessage](), downloadBroadcast: broadcast.NewRelay[DownloadStatusMessage](),
config: cfg,
} }
} }

View file

@ -1,12 +1,27 @@
package api package api
import "context" import (
"context"
"errors"
"github.com/tigorlazuardi/redmage/db/queries"
"github.com/tigorlazuardi/redmage/pkg/errs"
)
type DownloadSubredditParams struct { type DownloadSubredditParams struct {
Countback int Countback int
NSFW bool NSFW bool
Devices []queries.Device
} }
var (
ErrNoDevices = errors.New("api: downloading subreddit images requires at least one device")
ErrDownloadDirNotSet = errors.New("api: downloading subreddit images require download directory to be set")
)
func (api *API) DownloadSubredditImages(ctx context.Context, subredditName string, params DownloadSubredditParams) error { func (api *API) DownloadSubredditImages(ctx context.Context, subredditName string, params DownloadSubredditParams) error {
if len(params.Devices) == 0 {
return errs.Wrap(ErrNoDevices)
}
return nil return nil
} }

View file

@ -28,7 +28,7 @@ var serveCmd = &cobra.Command{
queries := queries.New(db) queries := queries.New(db)
api := api.New(queries, db) api := api.New(queries, db, cfg)
server := server.New(cfg, api, PublicDir) server := server.New(cfg, api, PublicDir)

View file

@ -1,16 +1,26 @@
package config package config
var DefaultConfig = map[string]any{ var DefaultConfig = map[string]any{
"log.enable": true, "flags.containerized": false,
"log.source": true,
"log.format": "pretty", "log.enable": true,
"log.level": "info", "log.source": true,
"log.output": "stderr", "log.format": "pretty",
"log.level": "info",
"log.output": "stderr",
"log.file.enable": true,
"log.file.path": "redmage.log",
"db.driver": "sqlite3", "db.driver": "sqlite3",
"db.string": "data.db", "db.string": "data.db",
"db.automigrate": true, "db.automigrate": true,
"download.concurrency": 5,
"download.directory": "",
"download.timeout.firstbyte": "30s",
"download.timeout.idleconnection": "5s",
"download.timeout.idlespeed": 10 * 1024, // 10KB
"http.port": "8080", "http.port": "8080",
"http.host": "0.0.0.0", "http.host": "0.0.0.0",
"http.shutdown_timeout": "5s", "http.shutdown_timeout": "5s",