-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
105 lines (85 loc) · 2.83 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/mux"
"github.com/periskop-dev/periskop-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/periskop-dev/periskop/api"
"github.com/periskop-dev/periskop/config"
"github.com/periskop-dev/periskop/metrics"
"github.com/periskop-dev/periskop/repository"
"github.com/periskop-dev/periskop/scraper"
"github.com/periskop-dev/periskop/servicediscovery"
)
const numOfProcessors = 8
func main() {
var (
port = flag.String("port", os.Getenv("PORT"), "The server port")
configurationFile = flag.String("config", os.Getenv("CONFIG_FILE"), "The configuration file")
)
flag.Parse()
if _, err := os.Stat(*configurationFile); err != nil {
log.Fatalf("Missing configuration file %s", *configurationFile)
}
log.Printf("Using configFile %s", *configurationFile)
cfg, err := config.LoadFile(*configurationFile)
if err != nil {
panic(err)
}
processor := scraper.NewProcessor(numOfProcessors)
processor.Run()
repo := repository.NewRepository(cfg.Repository)
for _, service := range cfg.Services {
resolver := servicediscovery.NewResolver(service)
s := scraper.NewScraper(resolver, &repo, service, processor)
go s.Scrape()
}
router := mux.NewRouter()
// API routing
setupAPIRouting(repo, router)
// Web routing
setupWebRouting(router)
// Telemetry endpoints
errorExporter := periskop.NewErrorExporter(&metrics.ErrorCollector)
periskopHandler := periskop.NewHandler(errorExporter)
http.Handle("/metrics", promhttp.Handler())
http.Handle("/errors", periskopHandler)
http.HandleFunc("/-/health", healthHandler)
address := fmt.Sprintf(":%s", *port)
log.Printf("Serving on address %s", address)
log.Fatal(http.ListenAndServe(address, nil))
}
func setupWebRouting(r *mux.Router) {
basePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
log.Printf("Using baseDir %s", basePath)
webFolder := filepath.Join(basePath, "web/build")
log.Printf("Using webFolder %s", webFolder)
fs := http.FileServer(http.Dir(webFolder))
r.PathPrefix("/").Handler(http.StripPrefix("/", fs))
}
func setupAPIRouting(repo repository.ErrorsRepository, r *mux.Router) {
r.Handle("/services/",
api.NewServicesListHandler(&repo)).Methods(http.MethodGet)
r.Handle("/services/{service_name}/errors/",
api.NewErrorsListHandler(&repo)).Methods(http.MethodGet)
r.Handle("/services/{service_name}/errors/{error_key:.*}/",
api.NewErrorResolveHandler(&repo)).Methods(http.MethodDelete, http.MethodOptions)
r.Handle("/targets/",
api.NewTargetsHandler(&repo)).Methods(http.MethodGet)
r.Use(api.CORSLocalhostMiddleware(r))
http.Handle("/", r)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("OK"))
if err != nil {
log.Fatalf("error running health handler")
}
}