-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (60 loc) · 1.81 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
// Copyright 2019 the Drone Authors. All rights reserved.
// Use of this source code is governed by the Blue Oak Model License
// that can be found in the LICENSE file.
package main
import (
"database/sql"
"net/http"
"github.com/BrianU/my-metrics/handler"
"github.com/BrianU/my-metrics/metrics"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
"gopkg.in/gorp.v1"
)
type spec struct {
Bind string `envconfig:"DRONE_BIND" default:":3001"`
Debug bool `envconfig:"DRONE_DEBUG"`
Token string `envconfig:"DRONE_TOKEN"`
Database struct {
Datasource string `envconfig:"DRONE_DATABASE_DATASOURCE"`
Driver string `envconfig:"DRONE_DATABASE_DRIVER"`
}
}
func main() {
spec := new(spec)
err := envconfig.Process("", spec)
if err != nil {
logrus.WithError(err).
Fatalln("cannot read configuration from environment")
}
if spec.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
// opens a connection with the database.
db, err := sql.Open(spec.Database.Driver, spec.Database.Datasource)
if err != nil {
logrus.WithError(err).
WithField("database", spec.Database.Driver).
WithField("datasource", spec.Database.Datasource).
Fatalln("cannot open database connection")
}
// creates an orm instance and configures the dialect
// based on the database driver.
dbm := &gorp.DbMap{Db: db}
switch spec.Database.Driver {
case "sqlite3":
dbm.Dialect = gorp.SqliteDialect{}
case "mysql":
dbm.Dialect = gorp.MySQLDialect{}
default:
dbm.Dialect = gorp.PostgresDialect{}
}
// registers all prometheus metrics.
metrics.Register(dbm)
// registers the promethes handler.
http.Handle("/", handler.New(spec.Token))
logrus.Infof("server listening on address %s", spec.Bind)
logrus.Fatal(http.ListenAndServe(spec.Bind, nil))
}