-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (85 loc) · 2.33 KB
/
main.go
File metadata and controls
104 lines (85 loc) · 2.33 KB
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
package main
import (
"context"
"database/sql"
_ "embed"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/cycloidio/sentry-plugin/config"
"github.com/cycloidio/sentry-plugin/issue"
"github.com/cycloidio/sentry-plugin/organization"
"github.com/cycloidio/sentry-plugin/project"
"github.com/cycloidio/sentry-plugin/sentry"
"github.com/cycloidio/sentry-plugin/service"
thttp "github.com/cycloidio/sentry-plugin/service/transport/http"
"github.com/cycloidio/sentry-plugin/sqlite"
"github.com/gorilla/handlers"
_ "github.com/mattn/go-sqlite3"
)
//go:embed schema.sql
var schema string
func main() {
var started = true
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
cfg, err := config.Load()
if err != nil {
started = false
logger.Error(fmt.Errorf("failed to load config: %w", err).Error())
}
// By default we use the 'memory' setting so for testing we can easily use it
q := "file::memory:?cache=shared&_foreign_keys=true"
if cfg.DB.File != "" {
q = cfg.DB.File + "?_foreign_keys=true"
}
db, err := sql.Open("sqlite3", q)
if err != nil {
started = false
logger.Error(fmt.Errorf("could not connect to the SQLite database: %w", err).Error())
}
var (
or organization.Repository
pr project.Repository
ir issue.Repository
ss sentry.Service
)
if started {
if err := sqlite.Migrate(db, schema); err != nil {
started = false
logger.Error(fmt.Errorf("failed to apply schema: %w", err).Error())
}
or = sqlite.NewOrganizationRepository(db)
pr = sqlite.NewProjectRepository(db)
ir = sqlite.NewIssueRepository(db)
ss, err = sentry.New(cfg.Sentry.APIKey, cfg.Sentry.Endpoint)
if err != nil {
started = false
}
}
sctx, cfn := context.WithCancel(context.TODO())
defer cfn()
s := service.New(sctx, or, pr, ir, ss, started, cfg, logger)
handler := thttp.Handler(s)
mux := http.NewServeMux()
mux.Handle("/", handler)
svr := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: handlers.LoggingHandler(os.Stdout, mux),
}
errs := make(chan error)
go func() {
logger.Info("started server", "port", cfg.Port)
errs <- svr.ListenAndServe()
}()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
logger.Info("exit", "reason", <-errs)
}