-
-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathserver.go
More file actions
115 lines (102 loc) · 3.38 KB
/
Copy pathserver.go
File metadata and controls
115 lines (102 loc) · 3.38 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
105
106
107
108
109
110
111
112
113
114
115
package cmd
import (
"context"
"os"
"os/signal"
"path"
"syscall"
"github.com/getfider/fider/app/jobs"
"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/web"
"github.com/robfig/cron"
_ "github.com/getfider/fider/app/services/blob/fs"
_ "github.com/getfider/fider/app/services/blob/s3"
_ "github.com/getfider/fider/app/services/blob/sql"
_ "github.com/getfider/fider/app/services/email/awsses"
_ "github.com/getfider/fider/app/services/email/mailgun"
_ "github.com/getfider/fider/app/services/email/smtp"
_ "github.com/getfider/fider/app/services/httpclient"
_ "github.com/getfider/fider/app/services/log/console"
_ "github.com/getfider/fider/app/services/log/file"
_ "github.com/getfider/fider/app/services/log/sql"
_ "github.com/getfider/fider/app/services/oauth"
_ "github.com/getfider/fider/app/services/sqlstore/postgres"
_ "github.com/getfider/fider/app/services/userlist"
_ "github.com/getfider/fider/app/services/webhook"
)
// RunServer starts the Fider Server
// Returns an exitcode, 0 for OK and 1 for ERROR
func RunServer() int {
svcs := bus.Init()
ctx := log.WithProperty(context.Background(), log.PropertyKeyTag, "BOOTSTRAP")
for _, s := range svcs {
log.Debugf(ctx, "Service '@{ServiceCategory}.@{ServiceName}' has been initialized.", dto.Props{
"ServiceCategory": s.Category(),
"ServiceName": s.Name(),
})
}
copyEtcFiles(ctx)
startJobs(ctx)
e := routes(web.New())
go e.Start(env.Config.Host + ":" + env.Config.Port)
return listenSignals(e)
}
// Starts all scheduled jobs
func startJobs(ctx context.Context) {
c := cron.New()
_ = c.AddJob(jobs.NewJob(ctx, "PurgeExpiredNotificationsJob", jobs.PurgeExpiredNotificationsJobHandler{}))
_ = c.AddJob(jobs.NewJob(ctx, "EmailSuppressionJob", jobs.EmailSuppressionJobHandler{}))
c.Start()
}
// on startup, copy all etc/ files from configured blob storage into local etc/ folder
// this can be used to avoid having to mount volumes on ephemeral environments
func copyEtcFiles(ctx context.Context) {
q := &query.ListBlobs{Prefix: "etc/"}
if err := bus.Dispatch(ctx, q); err != nil {
panic(errors.Wrap(err, "failed to list etc/ blobs"))
}
if len(q.Result) == 0 {
log.Debug(ctx, "No etc/ files to copy")
return
}
for _, blobKey := range q.Result {
getBlob := &query.GetBlobByKey{Key: blobKey}
if err := bus.Dispatch(ctx, getBlob); err != nil {
panic(errors.Wrap(err, "failed to get blob by key '%s'", blobKey))
}
if err := os.MkdirAll(path.Dir(blobKey), 0774); err != nil {
panic(errors.Wrap(err, "failed to create dir"))
}
if err := os.WriteFile(blobKey, getBlob.Result.Content, 0774); err != nil {
panic(errors.Wrap(err, "failed to write blob '%s'", blobKey))
}
log.Debugf(ctx, "Copied '@{BlobKey}' to etc/ folder.", dto.Props{
"BlobKey": blobKey,
})
}
}
func listenSignals(e *web.Engine) int {
signals := make(chan os.Signal, 1)
signal.Notify(signals, append([]os.Signal{syscall.SIGTERM, syscall.SIGINT}, extraSignals...)...)
for {
s := <-signals
switch s {
case syscall.SIGINT, syscall.SIGTERM:
err := e.Stop()
if err != nil {
return 1
}
return 0
default:
ret := handleExtraSignal(s, e)
if ret >= 0 {
return ret
}
}
}
}