-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
service.go
103 lines (93 loc) · 3.08 KB
/
service.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
package yolosvc
import (
"context"
"net/http"
"sync"
"time"
"berty.tech/yolo/v2/go/pkg/bintray"
"berty.tech/yolo/v2/go/pkg/yolopb"
"berty.tech/yolo/v2/go/pkg/yolostore"
"github.com/buildkite/go-buildkite/buildkite"
"github.com/google/go-github/v32/github"
"github.com/jinzhu/gorm"
"github.com/jszwedko/go-circleci"
"github.com/tevino/abool"
"go.uber.org/zap"
"moul.io/u"
)
type Service interface {
yolopb.YoloServiceServer
PlistGenerator(w http.ResponseWriter, r *http.Request)
ArtifactDownloader(w http.ResponseWriter, r *http.Request)
ArtifactIcon(w http.ResponseWriter, r *http.Request)
ArtifactGetFile(w http.ResponseWriter, r *http.Request)
GitHubWorker(ctx context.Context, opts GithubWorkerOpts) error
BuildkiteWorker(ctx context.Context, opts BuildkiteWorkerOpts) error
CircleciWorker(ctx context.Context, opts CircleciWorkerOpts) error
BintrayWorker(ctx context.Context, opts BintrayWorkerOpts) error
PkgmanWorker(ctx context.Context, opts PkgmanWorkerOpts) error
}
type service struct {
startTime time.Time
store yolostore.Store
logger *zap.Logger
bkc *buildkite.Client
btc *bintray.Client
ccc *circleci.Client
ghc *github.Client
authSalt string
devMode bool
clearCache *abool.AtomicBool
artifactsCachePath string
artifactsCacheMapMutex map[string]*sync.Mutex // per-cache mutex
artifactsCacheMutex sync.Mutex // mutex used to manipulate the artifactsCacheMapMutex
iosPrivkeyPath string
iosProvPath string
iosPrivkeyPass string
}
type ServiceOpts struct {
BuildkiteClient *buildkite.Client
CircleciClient *circleci.Client
BintrayClient *bintray.Client
GithubClient *github.Client
Logger *zap.Logger
AuthSalt string
DevMode bool
ClearCache *abool.AtomicBool
ArtifactsCachePath string
IOSPrivkeyPath string
IOSProvPath string
IOSPrivkeyPass string
}
func NewService(db *gorm.DB, opts ServiceOpts) (Service, error) {
opts.applyDefaults()
store, err := yolostore.NewStore(db, opts.Logger)
if err != nil {
return nil, err
}
return &service{
startTime: time.Now(),
store: store,
logger: opts.Logger,
bkc: opts.BuildkiteClient,
btc: opts.BintrayClient,
ccc: opts.CircleciClient,
ghc: opts.GithubClient,
authSalt: opts.AuthSalt,
devMode: opts.DevMode,
clearCache: opts.ClearCache,
artifactsCachePath: opts.ArtifactsCachePath,
iosPrivkeyPath: u.MustExpandUser(opts.IOSPrivkeyPath),
iosProvPath: u.MustExpandUser(opts.IOSProvPath),
iosPrivkeyPass: opts.IOSPrivkeyPass,
artifactsCacheMapMutex: map[string]*sync.Mutex{},
}, nil
}
func (o *ServiceOpts) applyDefaults() {
if o.Logger == nil {
o.Logger = zap.NewNop()
}
if o.ClearCache == nil {
o.ClearCache = abool.New()
}
}