generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.go
71 lines (57 loc) · 1.31 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"net/http"
"os"
"strconv"
"time"
"github.com/johnwarden/hn"
retryablehttp "github.com/hashicorp/go-retryablehttp"
)
type app struct {
ndb newsDatabase
hnClient *hn.Client
httpClient *http.Client
logger leveledLogger
cacheSize int
}
func initApp() app {
var err error
var cacheSize int
{
s := os.Getenv("CACHE_SIZE")
if s != "" {
cacheSize, err = strconv.Atoi(s)
if err != nil {
panic("Couldn't parse CACHE_SIZE")
}
}
}
logLevelString := os.Getenv("LOG_LEVEL")
logFormatString := os.Getenv("LOG_FORMAT")
logger := newLogger(logLevelString, logFormatString)
sqliteDataDir := os.Getenv("SQLITE_DATA_DIR")
if sqliteDataDir == "" {
panic("SQLITE_DATA_DIR not set")
}
db, err := openNewsDatabase(sqliteDataDir)
if err != nil {
LogFatal(logger, "openNewsDatabase", err)
}
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3
retryClient.RetryWaitMin = 1 * time.Second
retryClient.RetryWaitMax = 5 * time.Second
retryClient.Logger = wrapLoggerForRetryableHTTPClient(logger)
httpClient := retryClient.StandardClient()
hnClient := hn.NewClient(httpClient)
return app{
httpClient: httpClient,
hnClient: hnClient,
logger: logger,
ndb: db,
cacheSize: cacheSize,
}
}
func (app app) cleanup() {
app.ndb.close()
}