Compare commits
2 Commits
13f3217a38
...
8725def3a1
| Author | SHA1 | Date | |
|---|---|---|---|
| 8725def3a1 | |||
| 9be7b6c18f |
1
.env
1
.env
@@ -6,3 +6,4 @@ S3_BUCKET=dev
|
|||||||
S3_DISABLE_SSL=true
|
S3_DISABLE_SSL=true
|
||||||
ADDRESS=:8080
|
ADDRESS=:8080
|
||||||
VERBOSE=true
|
VERBOSE=true
|
||||||
|
DB_CONNECTION=s3Browser:hunter2@/s3Browser
|
||||||
@@ -18,6 +18,7 @@ type args struct {
|
|||||||
CacheTTL int64 `arg:"--cache-ttl,env:CACHE_TTL" help:"Time in seconds" default:"30" placeholder:"TTL"`
|
CacheTTL int64 `arg:"--cache-ttl,env:CACHE_TTL" help:"Time in seconds" default:"30" placeholder:"TTL"`
|
||||||
CacheCleanup int64 `arg:"--cache-cleanup,env:CACHE_CLEANUP" help:"Time in seconds" default:"60" placeholder:"CLEANUP"`
|
CacheCleanup int64 `arg:"--cache-cleanup,env:CACHE_CLEANUP" help:"Time in seconds" default:"60" placeholder:"CLEANUP"`
|
||||||
Verbose bool `arg:"-v,--verbose,env:VERBOSE" help:"verbosity level" default:"false"`
|
Verbose bool `arg:"-v,--verbose,env:VERBOSE" help:"verbosity level" default:"false"`
|
||||||
|
DBConnection string `arg:"--db,required,env:DB_CONNECTION" help:"DSN in format: https://github.com/go-sql-driver/mysql#dsn-data-source-name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (args) Version() string {
|
func (args) Version() string {
|
||||||
@@ -35,6 +36,7 @@ func main() {
|
|||||||
S3AccessKey: args.S3AccessKey,
|
S3AccessKey: args.S3AccessKey,
|
||||||
S3SecretKey: args.S3SecretKey,
|
S3SecretKey: args.S3SecretKey,
|
||||||
S3Bucket: args.S3Bucket,
|
S3Bucket: args.S3Bucket,
|
||||||
|
DSN: args.DBConnection,
|
||||||
CacheTTL: time.Duration(args.CacheTTL) * time.Second,
|
CacheTTL: time.Duration(args.CacheTTL) * time.Second,
|
||||||
CacheCleanup: time.Duration(args.CacheCleanup) * time.Second,
|
CacheCleanup: time.Duration(args.CacheCleanup) * time.Second,
|
||||||
Address: args.Address,
|
Address: args.Address,
|
||||||
|
|||||||
@@ -19,18 +19,16 @@ type DB struct {
|
|||||||
dbConn *sql.DB
|
dbConn *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDB(driver, dataSourceName string) (*DB, error) {
|
func NewDB(dataSourceName string) (*DB, error) {
|
||||||
db, err := sql.Open(driver, dataSourceName)
|
db, err := sql.Open("mysql", dataSourceName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if driver == "mysql" {
|
|
||||||
db.SetConnMaxLifetime(time.Minute * 3)
|
db.SetConnMaxLifetime(time.Minute * 3)
|
||||||
db.SetMaxOpenConns(10)
|
db.SetMaxOpenConns(10)
|
||||||
db.SetMaxIdleConns(10)
|
db.SetMaxIdleConns(10)
|
||||||
}
|
|
||||||
|
|
||||||
return &DB{
|
return &DB{
|
||||||
dbConn: db,
|
dbConn: db,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
helper "git.kapelle.org/niklas/s3browser/internal/helper"
|
helper "git.kapelle.org/niklas/s3browser/internal/helper"
|
||||||
"git.kapelle.org/niklas/s3browser/internal/loader"
|
"git.kapelle.org/niklas/s3browser/internal/loader"
|
||||||
types "git.kapelle.org/niklas/s3browser/internal/types"
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func deleteMutation(ctx context.Context, id types.ID) error {
|
func deleteMutation(ctx context.Context, id types.ID) error {
|
||||||
@@ -20,6 +21,7 @@ func deleteMutation(ctx context.Context, id types.ID) error {
|
|||||||
return fmt.Errorf("Failed to get s3Client from context")
|
return fmt.Errorf("Failed to get s3Client from context")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug("S3 'RemoveObject': ", id)
|
||||||
// TODO: it is posible to remove multiple objects with a single call.
|
// TODO: it is posible to remove multiple objects with a single call.
|
||||||
// Is it better to batch this?
|
// Is it better to batch this?
|
||||||
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
|
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
|
||||||
@@ -47,6 +49,7 @@ func copyMutation(ctx context.Context, src, dest types.ID) (*types.File, error)
|
|||||||
dest.Key += helper.GetFilenameFromKey(src.Key)
|
dest.Key += helper.GetFilenameFromKey(src.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug("S3 'CopyObject': ", src, "-->", dest)
|
||||||
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
||||||
Bucket: dest.Bucket,
|
Bucket: dest.Bucket,
|
||||||
Object: dest.Key,
|
Object: dest.Key,
|
||||||
@@ -103,6 +106,7 @@ func moveDirMutation(ctx context.Context, src, dest types.ID) ([]*types.File, er
|
|||||||
}
|
}
|
||||||
newID.Normalize()
|
newID.Normalize()
|
||||||
|
|
||||||
|
log.Debug("S3 'CopyObject': ", src, "-->", dest)
|
||||||
_, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
_, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
||||||
Bucket: dest.Bucket,
|
Bucket: dest.Bucket,
|
||||||
Object: newID.Key,
|
Object: newID.Key,
|
||||||
@@ -139,6 +143,7 @@ func moveFileMutation(ctx context.Context, src, dest types.ID) (*types.File, err
|
|||||||
dest.Key += helper.GetFilenameFromKey(src.Key)
|
dest.Key += helper.GetFilenameFromKey(src.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug("S3 'CopyObject': ", src, "-->", dest)
|
||||||
// There is no (spoon) move. Only copy and delete
|
// There is no (spoon) move. Only copy and delete
|
||||||
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
info, err := s3Client.CopyObject(ctx, minio.CopyDestOptions{
|
||||||
Bucket: dest.Bucket,
|
Bucket: dest.Bucket,
|
||||||
@@ -180,6 +185,7 @@ func createDirectory(ctx context.Context, id types.ID) (*types.Directory, error)
|
|||||||
return nil, fmt.Errorf("Failed to get s3Client from context")
|
return nil, fmt.Errorf("Failed to get s3Client from context")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug("S3 'PutObject': ", id)
|
||||||
info, err := s3Client.PutObject(ctx, id.Bucket, id.Key, strings.NewReader(""), 0, minio.PutObjectOptions{
|
info, err := s3Client.PutObject(ctx, id.Bucket, id.Key, strings.NewReader(""), 0, minio.PutObjectOptions{
|
||||||
ContentType: "application/x-directory",
|
ContentType: "application/x-directory",
|
||||||
})
|
})
|
||||||
@@ -241,6 +247,7 @@ func deleteDirectory(ctx context.Context, id types.ID) error {
|
|||||||
// This is at least the behavior when working with minio as s3 backend
|
// This is at least the behavior when working with minio as s3 backend
|
||||||
// TODO: check if this is normal behavior when working with s3
|
// TODO: check if this is normal behavior when working with s3
|
||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
|
log.Debug("S3 'RemoveObject': ", id)
|
||||||
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
|
err := s3Client.RemoveObject(ctx, id.Bucket, id.Key, minio.RemoveObjectOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ func GetFilenameFromKey(id string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeleteMultiple(ctx context.Context, s3Client minio.Client, bucket string, keys []string) error {
|
func DeleteMultiple(ctx context.Context, s3Client minio.Client, bucket string, keys []string) error {
|
||||||
log.Debug("Remove multiple objects")
|
|
||||||
objectsCh := make(chan minio.ObjectInfo, 1)
|
objectsCh := make(chan minio.ObjectInfo, 1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -29,6 +28,7 @@ func DeleteMultiple(ctx context.Context, s3Client minio.Client, bucket string, k
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
log.Debug("S3 'RemoveObject': ", keys)
|
||||||
for err := range s3Client.RemoveObjects(ctx, bucket, objectsCh, minio.RemoveObjectsOptions{}) {
|
for err := range s3Client.RemoveObjects(ctx, bucket, objectsCh, minio.RemoveObjectsOptions{}) {
|
||||||
log.Error("Failed to delete object ", err.ObjectName, " because: ", err.Err.Error())
|
log.Error("Failed to delete object ", err.ObjectName, " because: ", err.Err.Error())
|
||||||
// TODO: error handel
|
// TODO: error handel
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("S3 call 'StatObject': ", id)
|
log.Debug("S3 'StatObject': ", id)
|
||||||
objInfo, err := s3Client.StatObject(context.Background(), id.Bucket, id.Key, minio.GetObjectOptions{})
|
objInfo, err := s3Client.StatObject(context.Background(), id.Bucket, id.Key, minio.GetObjectOptions{})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -138,7 +138,7 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("S3 call 'GetObject': ", id)
|
log.Debug("S3 'GetObject': ", id)
|
||||||
obj, err := s3Client.GetObject(context.Background(), id.Bucket, id.Key, minio.GetObjectOptions{})
|
obj, err := s3Client.GetObject(context.Background(), id.Bucket, id.Key, minio.GetObjectOptions{})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -180,12 +180,10 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
id.Normalize()
|
id.Normalize()
|
||||||
|
|
||||||
log.Debug("Upload file: ", id)
|
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
mimeType, _, _ := mime.ParseMediaType(contentType)
|
mimeType, _, _ := mime.ParseMediaType(contentType)
|
||||||
|
|
||||||
log.Debug("S3 call 'PutObject': ", id)
|
log.Debug("S3 'PutObject': ", id)
|
||||||
_, err := s3Client.PutObject(context.Background(), id.Bucket, id.Key, r.Body, r.ContentLength, minio.PutObjectOptions{
|
_, err := s3Client.PutObject(context.Background(), id.Bucket, id.Key, r.Body, r.ContentLength, minio.PutObjectOptions{
|
||||||
ContentType: mimeType,
|
ContentType: mimeType,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
// listObjectsBatch batch func for calling s3.ListObjects()
|
// listObjectsBatch batch func for calling s3.ListObjects()
|
||||||
func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||||
log.Debug("listObjectsBatch: ", k.Keys())
|
|
||||||
var results []*dataloader.Result
|
var results []*dataloader.Result
|
||||||
|
|
||||||
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
||||||
@@ -34,7 +33,6 @@ func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result
|
|||||||
|
|
||||||
// listObjectsRecursiveBatch just like listObjectsBatch but with recursive set to true
|
// listObjectsRecursiveBatch just like listObjectsBatch but with recursive set to true
|
||||||
func listObjectsRecursiveBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
func listObjectsRecursiveBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||||
log.Debug("listObjectsRecursiveBatch: ", k.Keys())
|
|
||||||
var results []*dataloader.Result
|
var results []*dataloader.Result
|
||||||
|
|
||||||
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
||||||
@@ -56,7 +54,7 @@ func listObjectsRecursiveBatch(c context.Context, k dataloader.Keys) []*dataload
|
|||||||
|
|
||||||
// listObjects helper func for listObjectsBatch
|
// listObjects helper func for listObjectsBatch
|
||||||
func listObjects(s3Client *minio.Client, id types.ID, recursive bool) []minio.ObjectInfo {
|
func listObjects(s3Client *minio.Client, id types.ID, recursive bool) []minio.ObjectInfo {
|
||||||
log.Debug("S3 call 'ListObjects': ", id)
|
log.Debug("S3 'ListObjects': ", id)
|
||||||
objectCh := s3Client.ListObjects(context.Background(), id.Bucket, minio.ListObjectsOptions{
|
objectCh := s3Client.ListObjects(context.Background(), id.Bucket, minio.ListObjectsOptions{
|
||||||
Prefix: id.Key,
|
Prefix: id.Key,
|
||||||
Recursive: recursive,
|
Recursive: recursive,
|
||||||
@@ -71,7 +69,6 @@ func listObjects(s3Client *minio.Client, id types.ID, recursive bool) []minio.Ob
|
|||||||
}
|
}
|
||||||
|
|
||||||
func listBucketsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
func listBucketsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||||
log.Debug("listBucketsBatch")
|
|
||||||
var results []*dataloader.Result
|
var results []*dataloader.Result
|
||||||
|
|
||||||
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
||||||
@@ -80,6 +77,7 @@ func listBucketsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result
|
|||||||
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
|
return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug("S3 'ListBuckets'")
|
||||||
buckets, err := s3Client.ListBuckets(c)
|
buckets, err := s3Client.ListBuckets(c)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -110,6 +108,7 @@ func statObjectBatch(ctx context.Context, k dataloader.Keys) []*dataloader.Resul
|
|||||||
|
|
||||||
for _, v := range k {
|
for _, v := range k {
|
||||||
id := v.Raw().(types.ID)
|
id := v.Raw().(types.ID)
|
||||||
|
log.Debug("S3 'StatObject': ", id)
|
||||||
stat, err := s3Client.StatObject(ctx, id.Bucket, id.Key, minio.GetObjectOptions{})
|
stat, err := s3Client.StatObject(ctx, id.Bucket, id.Key, minio.GetObjectOptions{})
|
||||||
results = append(results, &dataloader.Result{
|
results = append(results, &dataloader.Result{
|
||||||
Data: stat,
|
Data: stat,
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
types "git.kapelle.org/niklas/s3browser/internal/types"
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
||||||
"github.com/graph-gophers/dataloader"
|
"github.com/graph-gophers/dataloader"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Loader struct {
|
type Loader struct {
|
||||||
@@ -145,7 +144,6 @@ func (l *Loader) GetFilesRecursive(ctx context.Context, path types.ID) ([]types.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loader) InvalidateCacheForFile(ctx context.Context, id types.ID) {
|
func (l *Loader) InvalidateCacheForFile(ctx context.Context, id types.ID) {
|
||||||
log.Debug("Clear cache for file: ", id.String())
|
|
||||||
parent := id.Parent()
|
parent := id.Parent()
|
||||||
|
|
||||||
l.statObjectLoader.Clear(ctx, id)
|
l.statObjectLoader.Clear(ctx, id)
|
||||||
@@ -153,7 +151,6 @@ func (l *Loader) InvalidateCacheForFile(ctx context.Context, id types.ID) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loader) InvalidateCacheForDir(ctx context.Context, path types.ID) {
|
func (l *Loader) InvalidateCacheForDir(ctx context.Context, path types.ID) {
|
||||||
log.Debug("Clear cache for dir: ", path.String())
|
|
||||||
parent := helper.GetParentDir(path)
|
parent := helper.GetParentDir(path)
|
||||||
|
|
||||||
l.listObjectsLoader.Clear(ctx, path).Clear(ctx, parent)
|
l.listObjectsLoader.Clear(ctx, path).Clear(ctx, parent)
|
||||||
|
|||||||
@@ -47,24 +47,20 @@ func Start(config types.AppConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Starting")
|
log.Info("Starting")
|
||||||
log.Debug("Setting up s3 client")
|
|
||||||
s3Client, err := setupS3Client(config)
|
s3Client, err := setupS3Client(config)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to setup s3 client: ", err.Error())
|
log.Error("Failed to setup s3 client: ", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Info("s3 client connected")
|
|
||||||
|
|
||||||
dbStore, err := db.NewDB("mysql", "s3Browser:hunter2@/s3Browser")
|
dbStore, err := db.NewDB(config.DSN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to connect DB: ", err.Error())
|
log.Error("Failed to connect DB: ", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Creating dataloader")
|
|
||||||
loader := loader.NewLoader(config)
|
loader := loader.NewLoader(config)
|
||||||
|
|
||||||
log.Debug("Generating graphq schema")
|
|
||||||
gql.GraphqlTypes()
|
gql.GraphqlTypes()
|
||||||
schema, err := gql.GraphqlSchema()
|
schema, err := gql.GraphqlSchema()
|
||||||
|
|
||||||
@@ -77,7 +73,6 @@ func Start(config types.AppConfig) {
|
|||||||
resolveContext = context.WithValue(resolveContext, "loader", loader)
|
resolveContext = context.WithValue(resolveContext, "loader", loader)
|
||||||
resolveContext = context.WithValue(resolveContext, "dbStore", dbStore)
|
resolveContext = context.WithValue(resolveContext, "dbStore", dbStore)
|
||||||
|
|
||||||
log.Debug("Starting HTTP server")
|
|
||||||
err = httpserver.InitHttp(resolveContext, schema, config.Address)
|
err = httpserver.InitHttp(resolveContext, schema, config.Address)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type AppConfig struct {
|
|||||||
S3SecretKey string
|
S3SecretKey string
|
||||||
S3SSL bool
|
S3SSL bool
|
||||||
S3Bucket string
|
S3Bucket string
|
||||||
|
DSN string
|
||||||
CacheTTL time.Duration
|
CacheTTL time.Duration
|
||||||
CacheCleanup time.Duration
|
CacheCleanup time.Duration
|
||||||
Address string
|
Address string
|
||||||
|
|||||||
Reference in New Issue
Block a user