-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
125 lines (104 loc) · 3.19 KB
/
server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"database/sql"
"fmt"
"github.com/datatogether/core"
"github.com/datatogether/sql_datastore"
"github.com/datatogether/sqlutil"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
"net/http"
"os"
"time"
)
var (
// cfg is the global configuration for the server. It's read in at startup from
// the config.json file and enviornment variables, see config.go for more info.
cfg *config
// When was the last alert sent out?
// Use this value to avoid bombing alerts
// TODO - this is from an half-baked, unfinished alerts feature idea
lastAlertSent *time.Time
// log output
// logger = logger.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
log = logrus.New()
// application database connection
appDB = &sql.DB{}
// hoist default store
store = sql_datastore.DefaultStore
)
func init() {
log.Out = os.Stdout
log.Level = logrus.InfoLevel
log.Formatter = &logrus.TextFormatter{
ForceColors: true,
}
}
func main() {
var err error
cfg, err = initConfig(os.Getenv("GOLANG_ENV"))
if err != nil {
// panic if the server is missing a vital configuration detail
panic(fmt.Errorf("server configuration error: %s", err.Error()))
}
if cfg.Debug {
log.Level = logrus.DebugLevel
}
sqlutil.ConnectToDb("postgres", cfg.PostgresDbUrl, appDB)
sql_datastore.SetDB(appDB)
sql_datastore.Register(
&core.Url{},
&core.Link{},
)
// create any tables if they don't exist
sc, err := sqlutil.LoadSchemaCommands(packagePath("sql/schema.sql"))
if err != nil {
log.Infof("error loading schema file: %s", err)
} else {
created, err := sc.Create(appDB, "primers", "sources", "urls", "links", "metadata", "snapshots", "collections")
if err != nil {
log.Infof("error creating missing tables: %s", err)
} else if len(created) > 0 {
log.Info("created tables:", created)
}
}
// always crawl seeds
go startCrawlingSeeds()
if cfg.Crawl {
// what a wonderful phrase :)
go startCrawling()
}
// run cron every 5 hours for now
go StartCron(time.Hour * 5)
s := &http.Server{}
// connect mux to server
s.Handler = NewServerRoutes()
// print notable config settings
// printConfigInfo()
// fire it up!
fmt.Println("starting server on port", cfg.Port)
// start server wrapped in a log.Fatal. http.ListenAndServe will not
// return unless a fatal error occurs
log.Fatal(StartServer(cfg, s))
}
// NewServerRoutes returns a Muxer that has all API routes.
// This makes for easy testing using httptest, see server_test.go
func NewServerRoutes() *http.ServeMux {
m := http.NewServeMux()
m.HandleFunc("/.well-known/acme-challenge/", CertbotHandler)
m.Handle("/", middleware(HealthCheckHandler))
m.Handle("/healthcheck", middleware(HealthCheckHandler))
// Seed a url to the crawler
// r.POST("/seed", middleware(SeedUrlHandler))
// List domains
// m.Handle("/primers", middleware(ListPrimersHandler))
// Add a crawling domain
// r.POST("/primers", middleware(AddPrimerHandler))
m.Handle("/urls", middleware(UrlsHandler))
// m.Handle("/url", middleware(UrlHandler))
m.Handle("/sources", middleware(CrawlingSourcesHandler))
m.Handle("/mem", middleware(MemStatsHandler))
m.Handle("/que", middleware(QueHandler))
m.Handle("/shutdown", middleware(ShutdownHandler))
return m
}