Add documents
This commit is contained in:
23
utils/sorting/fleets.go
Normal file
23
utils/sorting/fleets.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package sorting
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
|
||||
)
|
||||
|
||||
type VehiclesByLicencePlate []fleetsstorage.Vehicle
|
||||
|
||||
func (a VehiclesByLicencePlate) Len() int { return len(a) }
|
||||
func (a VehiclesByLicencePlate) Less(i, j int) bool {
|
||||
return strings.Compare(a[i].Data["licence_plate"].(string), a[j].Data["licence_plate"].(string)) < 0
|
||||
}
|
||||
func (a VehiclesByLicencePlate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
type BookingsByStartdate []fleetsstorage.Booking
|
||||
|
||||
func (a BookingsByStartdate) Len() int { return len(a) }
|
||||
func (a BookingsByStartdate) Less(i, j int) bool {
|
||||
return a[i].Startdate.Before(a[j].Startdate)
|
||||
}
|
||||
func (a BookingsByStartdate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
1
utils/sorting/sorting.go
Normal file
1
utils/sorting/sorting.go
Normal file
@@ -0,0 +1 @@
|
||||
package sorting
|
||||
30
utils/storage/files.go
Normal file
30
utils/storage/files.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
PREFIX_BENEFICIARIES = "beneficiaries"
|
||||
)
|
||||
|
||||
type FileInfo struct {
|
||||
Key string
|
||||
FileName string
|
||||
LastModified time.Time
|
||||
ContentType string
|
||||
Metadata map[string]string
|
||||
}
|
||||
|
||||
type FileStorage interface {
|
||||
Put(reader io.Reader, prefix string, filename string, size int64, metadata map[string]string) error
|
||||
List(prefix string) []FileInfo
|
||||
Get(prefix string, file string) (io.Reader, *FileInfo, error)
|
||||
}
|
||||
|
||||
func NewFileStorage(cfg *viper.Viper) (FileStorage, error) {
|
||||
return NewMinioStorageHandler(cfg)
|
||||
}
|
||||
104
utils/storage/minio.go
Normal file
104
utils/storage/minio.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type MinioStorageHandler struct {
|
||||
*minio.Client
|
||||
|
||||
BucketName string
|
||||
}
|
||||
|
||||
func NewMinioStorageHandler(cfg *viper.Viper) (*MinioStorageHandler, error) {
|
||||
minioClient, err := minio.New(cfg.GetString("storage.files.minio.endpoint"), &minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.GetString("storage.files.minio.access_key"), cfg.GetString("storage.files.minio.secret_key"), ""),
|
||||
Secure: cfg.GetBool("storage.files.minio.use_ssl"),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &MinioStorageHandler{
|
||||
Client: minioClient,
|
||||
BucketName: cfg.GetString("storage.files.minio.bucket_name"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MinioStorageHandler) Put(reader io.Reader, prefix string, filename string, size int64, metadata map[string]string) error {
|
||||
s.Client.PutObject(context.TODO(), s.BucketName, prefix+"/"+filename, reader, size, minio.PutObjectOptions{
|
||||
|
||||
UserMetadata: metadata,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MinioStorageHandler) List(prefix string) []FileInfo {
|
||||
res := []FileInfo{}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
objectCh := s.Client.ListObjects(ctx, s.BucketName, minio.ListObjectsOptions{
|
||||
Prefix: prefix,
|
||||
Recursive: true,
|
||||
})
|
||||
|
||||
for object := range objectCh {
|
||||
if object.Err != nil {
|
||||
fmt.Println("Error : ", object.Err)
|
||||
continue
|
||||
}
|
||||
|
||||
objinfo, err := s.Client.StatObject(context.Background(), s.BucketName, object.Key, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
path := strings.Split(object.Key, "/")
|
||||
|
||||
fileinfo := FileInfo{
|
||||
Key: object.Key,
|
||||
FileName: path[len(path)-1],
|
||||
LastModified: object.LastModified,
|
||||
ContentType: object.ContentType,
|
||||
Metadata: objinfo.UserMetadata,
|
||||
}
|
||||
|
||||
res = append(res, fileinfo)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (s *MinioStorageHandler) Get(prefix string, file string) (io.Reader, *FileInfo, error) {
|
||||
object, err := s.Client.GetObject(context.Background(), s.BucketName, prefix+"/"+file, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
objinfo, err := s.Client.StatObject(context.Background(), s.BucketName, prefix+"/"+file, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
path := strings.Split(objinfo.Key, "/")
|
||||
fileinfo := &FileInfo{
|
||||
Key: objinfo.Key,
|
||||
FileName: path[len(path)-1],
|
||||
LastModified: objinfo.LastModified,
|
||||
ContentType: objinfo.ContentType,
|
||||
Metadata: objinfo.UserMetadata,
|
||||
}
|
||||
|
||||
return object, fileinfo, nil
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func NewSessionStore(client KVHandler, keyPairs ...[]byte) *SessionStore {
|
||||
Path: "/",
|
||||
MaxAge: sessionExpire,
|
||||
},
|
||||
DefaultMaxAge: 60 * 20, // 20 minutes seems like a reasonable default
|
||||
DefaultMaxAge: sessionExpire, // 20 minutes seems like a reasonable default
|
||||
//maxLength: 4096,
|
||||
keyPrefix: "session/",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user