Skip to content

Commit 2eab116

Browse files
author
Kairo Araujo
authored
Refactoring services (#287)
* refactor: remove iternal for Archivista flexibility Remove the internal in order to enable Archivista source code user to: - Extend/Modify the existent services (config, storage, etc) within their code base - Allow to use Archivista encapuslated in other services, example API service Signed-off-by: Kairo Araujo <[email protected]> * refactor: simplify archivista main cmd Simplifies Archivista main cmd moving all the service logic into the `pkg/server`. It allows users to use the Archivista Server instance as an Service Interface allowing to integrate along to API services. Signed-off-by: Kairo Araujo <[email protected]> --------- Signed-off-by: Kairo Araujo <[email protected]>
1 parent 1e9626e commit 2eab116

File tree

16 files changed

+213
-121
lines changed

16 files changed

+213
-121
lines changed

cmd/archivista/main.go

Lines changed: 12 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package main
2121

2222
import (
2323
"context"
24-
"fmt"
2524
"net"
2625
"net/http"
2726
"os"
@@ -32,13 +31,7 @@ import (
3231

3332
nested "github.com/antonfisher/nested-logrus-formatter"
3433
"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"
4235
"github.com/sirupsen/logrus"
4336
)
4437

@@ -57,76 +50,19 @@ func main() {
5750
defer cancel()
5851

5952
startTime := time.Now()
60-
serverOpts := make([]server.Option, 0)
6153

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}
8755

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()
9457
if err != nil {
95-
logrus.Fatalf("could not create ent client: %+v", err)
58+
logrus.Fatalf("unable to setup archivista service: %+v", err)
9659
}
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-
10660
// ********************************************************************************
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))
10862
// ********************************************************************************
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()
12864

129-
listenAddress := cfg.ListenOn
65+
listenAddress := archivistaService.Cfg.ListenOn
13066
listenAddress = strings.ToLower(strings.TrimSpace(listenAddress))
13167
proto := ""
13268
if strings.HasPrefix(listenAddress, "tcp://") {
@@ -143,10 +79,10 @@ func main() {
14379
}
14480
srv := &http.Server{
14581
Handler: handlers.CORS(
146-
handlers.AllowedOrigins(cfg.CORSAllowOrigins),
82+
handlers.AllowedOrigins(archivistaService.Cfg.CORSAllowOrigins),
14783
handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}),
14884
handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}),
149-
)(server.Router()),
85+
)(server.Router()),
15086
ReadTimeout: 5 * time.Second,
15187
WriteTimeout: 10 * time.Second,
15288
}
@@ -156,47 +92,12 @@ func main() {
15692
}
15793
}()
15894

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")
16096
logrus.Infof("startup complete (time since start: %s)", time.Since(startTime))
16197

16298
<-ctx.Done()
163-
<-fileStoreCh
164-
<-sqlStoreCh
99+
<-archivistaService.GetFileStoreCh()
100+
<-archivistaService.GetSQLStoreCh()
165101

166102
logrus.Infof("exiting, uptime: %v", time.Since(startTime))
167103
}
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-
}
File renamed without changes.
File renamed without changes.

internal/metadatastorage/attestationcollection/parserstorer.go renamed to pkg/metadatastorage/attestationcollection/parserstorer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
"github.com/google/uuid"
2222
"github.com/in-toto/archivista/ent"
23-
"github.com/in-toto/archivista/internal/metadatastorage"
23+
"github.com/in-toto/archivista/pkg/metadatastorage"
2424
"github.com/in-toto/go-witness/attestation"
2525
)
2626

File renamed without changes.

internal/metadatastorage/parserregistry/registry.go renamed to pkg/metadatastorage/parserregistry/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package parserregistry
1616

1717
import (
18-
"github.com/in-toto/archivista/internal/metadatastorage"
19-
"github.com/in-toto/archivista/internal/metadatastorage/attestationcollection"
18+
"github.com/in-toto/archivista/pkg/metadatastorage"
19+
"github.com/in-toto/archivista/pkg/metadatastorage/attestationcollection"
2020
)
2121

2222
var (

internal/metadatastorage/sqlstore/store.go renamed to pkg/metadatastorage/sqlstore/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525

2626
"github.com/digitorus/timestamp"
2727
"github.com/in-toto/archivista/ent"
28-
"github.com/in-toto/archivista/internal/metadatastorage"
29-
"github.com/in-toto/archivista/internal/metadatastorage/parserregistry"
28+
"github.com/in-toto/archivista/pkg/metadatastorage"
29+
"github.com/in-toto/archivista/pkg/metadatastorage/parserregistry"
3030
"github.com/in-toto/go-witness/cryptoutil"
3131
"github.com/in-toto/go-witness/dsse"
3232
"github.com/in-toto/go-witness/intoto"

internal/objectstorage/filestore/file_test.go renamed to pkg/objectstorage/filestore/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"path/filepath"
2121
"testing"
2222

23-
"github.com/in-toto/archivista/internal/objectstorage/filestore"
23+
"github.com/in-toto/archivista/pkg/objectstorage/filestore"
2424
"github.com/stretchr/testify/suite"
2525
)
2626

internal/server/server.go renamed to pkg/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ import (
3535
"github.com/in-toto/archivista"
3636
_ "github.com/in-toto/archivista/docs"
3737
"github.com/in-toto/archivista/ent"
38-
"github.com/in-toto/archivista/internal/artifactstore"
39-
"github.com/in-toto/archivista/internal/config"
4038
"github.com/in-toto/archivista/pkg/api"
39+
"github.com/in-toto/archivista/pkg/artifactstore"
40+
"github.com/in-toto/archivista/pkg/config"
4141
"github.com/sirupsen/logrus"
4242
httpSwagger "github.com/swaggo/http-swagger/v2"
4343
)

internal/server/server_test.go renamed to pkg/server/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
"testing"
2828

2929
"github.com/gorilla/mux"
30-
"github.com/in-toto/archivista/internal/artifactstore"
31-
"github.com/in-toto/archivista/internal/config"
3230
"github.com/in-toto/archivista/pkg/api"
31+
"github.com/in-toto/archivista/pkg/artifactstore"
32+
"github.com/in-toto/archivista/pkg/config"
3333
"github.com/stretchr/testify/mock"
3434
"github.com/stretchr/testify/suite"
3535
)

0 commit comments

Comments
 (0)