@@ -21,7 +21,6 @@ package main
21
21
22
22
import (
23
23
"context"
24
- "fmt"
25
24
"net"
26
25
"net/http"
27
26
"os"
@@ -32,13 +31,7 @@ import (
32
31
33
32
nested "github.com/antonfisher/nested-logrus-formatter"
34
33
"github.com/gorilla/handlers"
35
- "github.com/in-toto/archivista/internal/artifactstore"
36
- "github.com/in-toto/archivista/internal/config"
37
- "github.com/in-toto/archivista/internal/metadatastorage/sqlstore"
38
- "github.com/in-toto/archivista/internal/objectstorage/blobstore"
39
- "github.com/in-toto/archivista/internal/objectstorage/filestore"
40
- "github.com/in-toto/archivista/internal/server"
41
- "github.com/minio/minio-go/v7/pkg/credentials"
34
+ "github.com/in-toto/archivista/pkg/server"
42
35
"github.com/sirupsen/logrus"
43
36
)
44
37
@@ -57,76 +50,19 @@ func main() {
57
50
defer cancel ()
58
51
59
52
startTime := time .Now ()
60
- serverOpts := make ([]server.Option , 0 )
61
53
62
- logrus .Infof ("executing phase 1: get config from environment (time since start: %s)" , time .Since (startTime ))
63
- now := time .Now ()
64
-
65
- cfg := new (config.Config )
66
- if err := cfg .Process (); err != nil {
67
- logrus .Fatal (err )
68
- }
69
-
70
- level , err := logrus .ParseLevel (cfg .LogLevel )
71
- if err != nil {
72
- logrus .Fatalf ("invalid log level %s" , cfg .LogLevel )
73
- }
74
- logrus .SetLevel (level )
75
-
76
- logrus .WithField ("duration" , time .Since (now )).Infof ("completed phase 1: get config from environment" )
77
-
78
- // ********************************************************************************
79
- logrus .Infof ("executing phase 2: initializing storage clients (time since start: %s)" , time .Since (startTime ))
80
- // ********************************************************************************
81
- now = time .Now ()
82
- fileStore , fileStoreCh , err := initObjectStore (ctx , cfg )
83
- if err != nil {
84
- logrus .Fatalf ("error initializing storage clients: %+v" , err )
85
- }
86
- serverOpts = append (serverOpts , server .WithObjectStore (fileStore ))
54
+ archivistaService := & server.ArchivistaService {Ctx : ctx , Cfg : nil }
87
55
88
- entClient , err := sqlstore .NewEntClient (
89
- cfg .SQLStoreBackend ,
90
- cfg .SQLStoreConnectionString ,
91
- sqlstore .ClientWithMaxIdleConns (cfg .SQLStoreMaxIdleConnections ),
92
- sqlstore .ClientWithMaxOpenConns (cfg .SQLStoreMaxOpenConnections ),
93
- sqlstore .ClientWithConnMaxLifetime (cfg .SQLStoreConnectionMaxLifetime ))
56
+ server , err := archivistaService .Setup ()
94
57
if err != nil {
95
- logrus .Fatalf ("could not create ent client : %+v" , err )
58
+ logrus .Fatalf ("unable to setup archivista service : %+v" , err )
96
59
}
97
-
98
- sqlStore , sqlStoreCh , err := sqlstore .New (ctx , entClient )
99
- if err != nil {
100
- logrus .Fatalf ("error initializing mysql client: %+v" , err )
101
- }
102
- serverOpts = append (serverOpts , server .WithMetadataStore (sqlStore ))
103
-
104
- logrus .WithField ("duration" , time .Since (now )).Infof ("completed phase 3: initializing storage clients" )
105
-
106
60
// ********************************************************************************
107
- logrus .Infof ("executing phase 3 : create and register http service (time since start: %s)" , time .Since (startTime ))
61
+ logrus .Infof ("executing phase: create and register http service (time since start: %s)" , time .Since (startTime ))
108
62
// ********************************************************************************
109
- now = time .Now ()
110
-
111
- // initialize the artifact store
112
- if cfg .EnableArtifactStore {
113
- wds , err := artifactstore .New (artifactstore .WithConfigFile (cfg .ArtifactStoreConfig ))
114
- if err != nil {
115
- logrus .Fatalf ("could not create the artifact store: %+v" , err )
116
- }
117
-
118
- serverOpts = append (serverOpts , server .WithArtifactStore (wds ))
119
- }
120
-
121
- // initialize the server
122
- sqlClient := sqlStore .GetClient ()
123
- serverOpts = append (serverOpts , server .WithEntSqlClient (sqlClient ))
124
- server , err := server .New (cfg , serverOpts ... )
125
- if err != nil {
126
- logrus .Fatalf ("could not create archivista server: %+v" , err )
127
- }
63
+ now := time .Now ()
128
64
129
- listenAddress := cfg .ListenOn
65
+ listenAddress := archivistaService . Cfg .ListenOn
130
66
listenAddress = strings .ToLower (strings .TrimSpace (listenAddress ))
131
67
proto := ""
132
68
if strings .HasPrefix (listenAddress , "tcp://" ) {
@@ -143,10 +79,10 @@ func main() {
143
79
}
144
80
srv := & http.Server {
145
81
Handler : handlers .CORS (
146
- handlers .AllowedOrigins (cfg .CORSAllowOrigins ),
82
+ handlers .AllowedOrigins (archivistaService . Cfg .CORSAllowOrigins ),
147
83
handlers .AllowedMethods ([]string {"GET" , "POST" , "OPTIONS" }),
148
84
handlers .AllowedHeaders ([]string {"Accept" , "Content-Type" , "Content-Length" , "Accept-Encoding" , "X-CSRF-Token" , "Authorization" }),
149
- )(server .Router ()),
85
+ )(server .Router ()),
150
86
ReadTimeout : 5 * time .Second ,
151
87
WriteTimeout : 10 * time .Second ,
152
88
}
@@ -156,47 +92,12 @@ func main() {
156
92
}
157
93
}()
158
94
159
- logrus .WithField ("duration" , time .Since (now )).Infof ("completed phase 5 : create and register http service" )
95
+ logrus .WithField ("duration" , time .Since (now )).Infof ("completed phase: create and register http service" )
160
96
logrus .Infof ("startup complete (time since start: %s)" , time .Since (startTime ))
161
97
162
98
<- ctx .Done ()
163
- <- fileStoreCh
164
- <- sqlStoreCh
99
+ <- archivistaService . GetFileStoreCh ()
100
+ <- archivistaService . GetSQLStoreCh ()
165
101
166
102
logrus .Infof ("exiting, uptime: %v" , time .Since (startTime ))
167
103
}
168
-
169
- func initObjectStore (ctx context.Context , cfg * config.Config ) (server.StorerGetter , <- chan error , error ) {
170
- switch strings .ToUpper (cfg .StorageBackend ) {
171
- case "FILE" :
172
- return filestore .New (ctx , cfg .FileDir , cfg .FileServeOn )
173
-
174
- case "BLOB" :
175
- var creds * credentials.Credentials
176
- if cfg .BlobStoreCredentialType == "IAM" {
177
- creds = credentials .NewIAM ("" )
178
- } else if cfg .BlobStoreCredentialType == "ACCESS_KEY" {
179
- creds = credentials .NewStaticV4 (cfg .BlobStoreAccessKeyId , cfg .BlobStoreSecretAccessKeyId , "" )
180
- } else {
181
- logrus .Fatalln ("invalid blob store credential type: " , cfg .BlobStoreCredentialType )
182
- }
183
- return blobstore .New (
184
- ctx ,
185
- cfg .BlobStoreEndpoint ,
186
- creds ,
187
- cfg .BlobStoreBucketName ,
188
- cfg .BlobStoreUseTLS ,
189
- )
190
-
191
- case "" :
192
- errCh := make (chan error )
193
- go func () {
194
- <- ctx .Done ()
195
- close (errCh )
196
- }()
197
- return nil , errCh , nil
198
-
199
- default :
200
- return nil , nil , fmt .Errorf ("unknown storage backend: %s" , cfg .StorageBackend )
201
- }
202
- }
0 commit comments