-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (73 loc) · 1.76 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"io/ioutil"
"os"
"time"
_ "github.com/astaxie/beego/config/yaml"
"github.com/josephroberts/esqimo/reader"
"github.com/josephroberts/esqimo/server"
"github.com/josephroberts/esqimo/store"
Swagger "github.com/josephroberts/esqimo/swagger"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"gopkg.in/yaml.v2"
)
var defaultFeeds = []string{
"http://feeds.bbci.co.uk/news/uk/rss.xml",
}
const defaultPort = ":1323"
type config struct {
Port string `yaml:"port"`
Feeds []string `yaml:"feeds"`
}
func main() {
// Load config
config := loadConfig("config.yaml")
log.Infof("Feeds: %v\n", config.Feeds)
log.Infof("Port: %v\n", config.Port)
// Reader instance
reader := reader.RSS{
URLS: config.Feeds,
}
// Store instance
store := store.New(reader, 10*time.Minute)
// Server instance
server := &server.Server{
Store: store,
}
// Echo instance
echo := echo.New()
// Echo middleware
echo.Use(middleware.Logger())
echo.Use(middleware.Recover())
// Register handlers
Swagger.RegisterHandlers(echo, server)
// Start server
echo.Logger.Fatal(echo.Start(config.Port))
}
func loadConfig(filename string) *config {
// Set default configs
config := &config{
Port: defaultPort,
Feeds: defaultFeeds,
}
// Check if file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
log.Errorf("unable to find config file: %v\n", err)
return config
}
// Read the config file
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Errorf("unable to read config file: %v\n", err)
return config
}
// Unmarshall the config file
err = yaml.Unmarshal(b, &config)
if err != nil {
log.Errorf("unable to unmarshal config file: %v\n", err)
return config
}
return config
}