This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
87 lines (75 loc) · 2.63 KB
/
main.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
package main
//go:generate go run generate/includemaps.go
import (
"log"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/justinas/alice"
"github.com/sul-dlss-labs/taco/aws_session"
"github.com/sul-dlss-labs/taco/config"
"github.com/sul-dlss-labs/taco/db"
"github.com/sul-dlss-labs/taco/generated/restapi"
"github.com/sul-dlss-labs/taco/generated/restapi/operations"
"github.com/sul-dlss-labs/taco/handlers"
"github.com/sul-dlss-labs/taco/identifier"
"github.com/sul-dlss-labs/taco/middleware"
"github.com/sul-dlss-labs/taco/storage"
"github.com/sul-dlss-labs/taco/streaming"
)
type Taco struct {
server *restapi.Server
}
var tacoServer Taco
func main() {
// Initialize our global struct
config := config.NewConfig()
awsSession := aws_session.Connect(config.AwsDisableSSL)
database := &db.DynamodbDatabase{
Connection: db.Connect(awsSession, config.DynamodbEndpoint),
Table: config.ResourceTableName,
}
stream := &streaming.KinesisStream{
Connection: streaming.Connect(awsSession, config.KinesisEndpoint),
StreamName: config.DepositStreamName,
}
storage := &storage.S3BucketStorage{
Uploader: connectToStorage(awsSession, config.S3Endpoint),
S3BucketName: config.S3BucketName,
}
identifierService := identifier.NewService(config)
tacoServer.server = createServer(database, stream, storage, identifierService, config.Port)
// serve API
if err := tacoServer.server.Serve(); err != nil {
log.Fatalln(err)
}
}
// NewS3Bucket creates a new storage adapter that uses S3 bucket storage to
// actually store the files
func connectToStorage(awsSession *session.Session, endpoint string) *s3manager.Uploader {
forcePath := true // This is required for localstack
s3Svc := s3.New(awsSession, &aws.Config{
Endpoint: aws.String(endpoint),
S3ForcePathStyle: &forcePath,
})
return s3manager.NewUploaderWithClient(s3Svc)
}
func createServer(database db.Database, stream streaming.Stream, storage storage.Storage, identifierService identifier.Service, port int) *restapi.Server {
api := handlers.BuildAPI(database, stream, storage, identifierService)
server := restapi.NewServer(api)
server.SetHandler(BuildHandler(api))
defer server.Shutdown()
// set the port this service will be run on
server.Port = port
return server
}
// BuildHandler sets up the middleware that wraps the API
func BuildHandler(api *operations.TacoAPI) http.Handler {
return alice.New(
middleware.NewHoneyBadgerMW(),
middleware.NewRecoveryMW(),
middleware.NewRequestLoggerMW(),
).Then(api.Serve(nil))
}