Skip to content

Commit b446f16

Browse files
committed
Refactor config defaults
1 parent 450ea1a commit b446f16

File tree

1 file changed

+74
-97
lines changed

1 file changed

+74
-97
lines changed

pkg/config/config.go

Lines changed: 74 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,42 @@ import (
1515
)
1616

1717
// See config.yml for comments about this struct
18+
19+
type Library struct {
20+
STOCK_DIR string `yaml:"STOCK"`
21+
TRASH_DIR string `yaml:"TRASH"`
22+
NEW_DIR string `yaml:"NEW"`
23+
}
24+
type Database struct {
25+
DSN string `yaml:"DSN"`
26+
POLL_DELAY int `yaml:"POLL_DELAY"`
27+
MAX_SCAN_THREADS int `yaml:"MAX_SCAN_THREADS"`
28+
BOOK_QUEUE_SIZE int `yaml:"BOOK_QUEUE_SIZE"`
29+
FILE_QUEUE_SIZE int `yaml:"FILE_QUEUE_SIZE"`
30+
MAX_BOOKS_IN_TX int `yaml:"MAX_BOOKS_IN_TX"`
31+
DEDUPLICATE_LEVEL string `yaml:"DEDUPLICATE_LEVEL"`
32+
}
33+
type Genres struct {
34+
TREE_FILE string `yaml:"TREE_FILE"`
35+
}
36+
type Logs struct {
37+
OPDS string `yaml:"OPDS"`
38+
SCAN string `yaml:"SCAN"`
39+
LEVEL string `yaml:"LEVEL"`
40+
}
41+
type OPDS struct {
42+
PORT int `yaml:"PORT"`
43+
TITLE string `yaml:"TITLE"`
44+
PAGE_SIZE int `yaml:"PAGE_SIZE"`
45+
LATEST_DAYS int `yaml:"LATEST_DAYS"`
46+
NO_CONVERSION bool `yaml:"NO_CONVERSION"`
47+
}
1848
type Config struct {
19-
Library struct {
20-
STOCK_DIR string `yaml:"STOCK"`
21-
TRASH_DIR string `yaml:"TRASH"`
22-
NEW_DIR string `yaml:"NEW"`
23-
}
24-
Database struct {
25-
DSN string `yaml:"DSN"`
26-
POLL_DELAY int `yaml:"POLL_DELAY"`
27-
MAX_SCAN_THREADS int `yaml:"MAX_SCAN_THREADS"`
28-
BOOK_QUEUE_SIZE int `yaml:"BOOK_QUEUE_SIZE"`
29-
FILE_QUEUE_SIZE int `yaml:"FILE_QUEUE_SIZE"`
30-
MAX_BOOKS_IN_TX int `yaml:"MAX_BOOKS_IN_TX"`
31-
DEDUPLICATE_LEVEL string `yaml:"DEDUPLICATE_LEVEL"`
32-
}
33-
Genres struct {
34-
TREE_FILE string `yaml:"TREE_FILE"`
35-
}
36-
Logs struct {
37-
OPDS string `yaml:"OPDS"`
38-
SCAN string `yaml:"SCAN"`
39-
LEVEL string `yaml:"LEVEL"`
40-
}
41-
OPDS struct {
42-
PORT int `yaml:"PORT"`
43-
TITLE string `yaml:"TITLE"`
44-
PAGE_SIZE int `yaml:"PAGE_SIZE"`
45-
LATEST_DAYS int `yaml:"LATEST_DAYS"`
46-
NO_CONVERSION bool `yaml:"NO_CONVERSION"`
47-
}
49+
Library Library `yaml:"library"`
50+
Database Database `yaml:"database"`
51+
Genres Genres `yaml:"genres"`
52+
Logs Logs `yaml:"logs"`
53+
OPDS OPDS `yaml:"opds"`
4854
locales.Locales
4955
}
5056

@@ -87,79 +93,50 @@ func LoadConfig(rootDir string) *Config {
8793
log.Fatal(err)
8894
}
8995
}
90-
c := &Config{}
96+
c := &Config{
97+
Library: Library{
98+
STOCK_DIR: "books/stock",
99+
TRASH_DIR: "books/trash",
100+
NEW_DIR: "",
101+
},
102+
Database: Database{
103+
DSN: "dbdata/books.db",
104+
POLL_DELAY: 30,
105+
MAX_SCAN_THREADS: 10,
106+
BOOK_QUEUE_SIZE: 20000,
107+
FILE_QUEUE_SIZE: 20000,
108+
MAX_BOOKS_IN_TX: 20000,
109+
DEDUPLICATE_LEVEL: "F",
110+
},
111+
Genres: Genres{
112+
TREE_FILE: "config/genres.xml",
113+
},
114+
Logs: Logs{
115+
OPDS: "logs/opds.log",
116+
SCAN: "logs/scan.log",
117+
LEVEL: "W",
118+
},
119+
OPDS: OPDS{
120+
PORT: 8085,
121+
TITLE: "FLib Go Go Go!!!",
122+
PAGE_SIZE: 30,
123+
LATEST_DAYS: 14,
124+
NO_CONVERSION: false,
125+
},
126+
Locales: locales.Locales{
127+
DIR: "config/locales",
128+
DEFAULT: "en",
129+
ACCEPTED: "en, ru, uk",
130+
},
131+
}
91132
if err := yaml.Unmarshal([]byte(b), c); err != nil {
92133
log.Fatal(err)
93134
}
94135

95-
if c.Library.STOCK_DIR == "" {
96-
c.Library.STOCK_DIR = "books/stock"
97-
}
98-
if c.Library.TRASH_DIR == "" {
99-
c.Library.TRASH_DIR = "books/trash"
100-
}
101-
if c.Database.DSN == "" {
102-
c.Database.DSN = "dbdata/books.db"
103-
}
104-
105-
if c.Genres.TREE_FILE == "" {
106-
c.Genres.TREE_FILE = "config/genres.xml"
107-
}
108-
109-
if c.Database.POLL_DELAY == 0 {
110-
c.Database.POLL_DELAY = 30
111-
}
112-
if c.Database.MAX_SCAN_THREADS == 0 {
113-
c.Database.MAX_SCAN_THREADS = 10
114-
}
115-
if c.Database.BOOK_QUEUE_SIZE == 0 {
116-
c.Database.BOOK_QUEUE_SIZE = 20000
117-
}
118-
if c.Database.FILE_QUEUE_SIZE == 0 {
119-
c.Database.FILE_QUEUE_SIZE = 20000
120-
}
121-
if c.Database.MAX_BOOKS_IN_TX == 0 {
122-
c.Database.MAX_BOOKS_IN_TX = 20000
123-
}
124-
if c.Database.DEDUPLICATE_LEVEL == "" {
125-
c.Database.DEDUPLICATE_LEVEL = "F"
126-
}
127-
128-
if c.Logs.OPDS == "" {
129-
c.Logs.OPDS = "logs/opds.log"
130-
}
131-
if c.Logs.SCAN == "" {
132-
c.Logs.SCAN = "logs/scan.log"
133-
}
134-
if c.Logs.LEVEL == "" {
135-
c.Logs.LEVEL = "W"
136-
}
137-
138-
if c.OPDS.PORT == 0 {
139-
c.OPDS.PORT = 8085
140-
}
141-
if c.OPDS.TITLE == "" {
142-
c.OPDS.TITLE = "FLib Go Go Go!!!"
143-
}
144-
if c.OPDS.PAGE_SIZE == 0 {
145-
c.OPDS.PAGE_SIZE = 30
146-
}
147-
if c.OPDS.LATEST_DAYS == 0 {
148-
c.OPDS.LATEST_DAYS = 14
149-
}
150-
151-
if c.Locales.DIR == "" {
152-
c.Locales.DIR = "config/locales"
153-
}
154-
if c.Locales.DEFAULT == "" {
155-
c.Locales.DEFAULT = "en"
156-
}
157-
if c.Locales.ACCEPTED == "" {
158-
c.Locales.ACCEPTED = "en, ru, uk"
159-
}
160-
161136
c.Library.STOCK_DIR = makeAbs(rootDir, c.Library.STOCK_DIR)
162-
c.Library.TRASH_DIR = makeAbs(rootDir, c.Library.TRASH_DIR)
137+
if len(c.Library.TRASH_DIR) > 0 {
138+
c.Library.TRASH_DIR = makeAbs(rootDir, c.Library.TRASH_DIR)
139+
}
163140
if len(c.Library.NEW_DIR) > 0 {
164141
c.Library.NEW_DIR = makeAbs(rootDir, c.Library.NEW_DIR)
165142
}

0 commit comments

Comments
 (0)