This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
153 lines (123 loc) · 3.93 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"database/sql"
"fmt"
"net/http"
"os"
"github.com/datatogether/core"
"github.com/datatogether/sql_datastore"
"github.com/datatogether/sqlutil"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
)
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
// log output handled by logrus package
log = logrus.New()
// application database connection
appDB = &sql.DB{}
// elevate default store
store = sql_datastore.DefaultStore
)
func init() {
// configure log
log.Out = os.Stderr
log.Level = logrus.InfoLevel
log.Formatter = &logrus.TextFormatter{
ForceColors: true,
}
}
// main app entry point
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()))
}
go initPostgres()
// base server
s := &http.Server{}
// connect mux routes to server
s.Handler = NewServerRoutes()
// print notable config settings
printConfigInfo()
// fire it up!
log.Infoln("starting server on port", cfg.Port)
// start server wrapped in a log.Fatal b/c http.ListenAndServe will not
// return unless there's an error
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.Handle("/", middleware(NotFoundHandler))
m.Handle("/healthcheck", middleware(HealthCheckHandler))
// serve static content for api documentation
m.Handle("/docs/", http.StripPrefix("/docs/", http.FileServer(http.Dir(packagePath("docs")))))
// m.Handle("/javascripts", http.FileServer(http.Dir("public/javascripts")))
// m.Handle("/stylesheets", http.FileServer(http.Dir("public/stylesheets")))
m.Handle("/users", middleware(UsersHandler))
m.Handle("/users/", middleware(UserHandler))
m.Handle("/primers", middleware(PrimersHandler))
m.Handle("/primers/", middleware(PrimerHandler))
m.Handle("/sources", middleware(SourcesHandler))
m.Handle("/sources/", middleware(SourceHandler))
m.Handle("/urls", middleware(UrlsHandler))
m.Handle("/urls/", middleware(UrlHandler))
// m.Handle("/links", middleware(LinksHandler))
// m.Handle("/links/", middleware(LinkHandler))
// m.Handle("/snapshots", middleware(SnapshotsHandler))
m.Handle("/coverage", middleware(CoverageHandler))
m.Handle("/repositories", middleware(RepositoriesHandler))
m.Handle("/repositories/", middleware(RepositoryHandler))
// m.Handle("/content", middleware())
// m.Handle("/content/", middleware())
// m.Handle("/metadata", middleware())
// m.Handle("/metadata/", middleware())
m.Handle("/collections", middleware(CollectionsHandler))
m.Handle("/collections/", middleware(CollectionHandler))
m.Handle("/uncrawlables", middleware(UncrawlablesHandler))
m.Handle("/uncrawlables/", middleware(UncrawlableHandler))
m.Handle("/customcrawls", middleware(CustomCrawlsHandler))
m.Handle("/customcrawls/", middleware(CustomCrawlHandler))
m.HandleFunc("/.well-known/acme-challenge/", CertbotHandler)
return m
}
func initPostgres() {
log.Infoln("connecting to postgres db")
if err := sqlutil.ConnectToDb("postgres", cfg.PostgresDbUrl, appDB); err != nil {
panic(err)
}
log.Infoln("connecteded to postgres db")
created, err := sqlutil.EnsureTables(appDB, packagePath("sql/schema.sql"),
"primers",
"sources",
"urls",
"links",
"metadata",
"snapshots",
"collections",
"collection_contents",
"uncrawlables",
"archive_requests")
if err != nil {
log.Infoln(err)
}
if len(created) > 0 {
log.Infoln("created tables:", created)
}
sql_datastore.SetDB(appDB)
sql_datastore.Register(
&core.Collection{},
&core.Link{},
&core.Primer{},
&core.Source{},
&core.Uncrawlable{},
&core.CustomCrawl{},
&core.Url{},
)
}