feat: add bucket object browser

This commit is contained in:
2024-08-18 22:57:08 +07:00
parent 3a147f4133
commit 934e0c409c
29 changed files with 656 additions and 18 deletions
+49
View File
@@ -0,0 +1,49 @@
package utils
import (
"sync"
"time"
)
type CacheEntry struct {
value interface{}
expiresAt time.Time
}
type CacheManager struct {
cache *sync.Map
}
var Cache *CacheManager
func InitCacheManager() {
Cache = &CacheManager{
cache: &sync.Map{},
}
}
func (c *CacheManager) Set(key string, value interface{}, ttl time.Duration) {
c.cache.Store(key, CacheEntry{
value: value,
expiresAt: time.Now().Add(ttl),
})
}
func (c *CacheManager) Get(key string) interface{} {
entry, ok := c.cache.Load(key)
if !ok {
return nil
}
cacheEntry := entry.(CacheEntry)
if cacheEntry.expiresAt.Before(time.Now()) {
c.cache.Delete(key)
return nil
}
return cacheEntry.value
}
func (c *CacheManager) IsExpired(entry CacheEntry) bool {
return entry.expiresAt.Before(time.Now())
}
+17
View File
@@ -56,6 +56,23 @@ func (g *garage) GetAdminEndpoint() string {
return endpoint
}
func (g *garage) GetS3Endpoint() string {
endpoint := os.Getenv("S3_ENDPOINT_URL")
if len(endpoint) > 0 {
return endpoint
}
host := strings.Split(g.Config.RPCPublicAddr, ":")[0]
port := LastString(strings.Split(g.Config.S3API.APIBindAddr, ":"))
endpoint = fmt.Sprintf("%s:%s", host, port)
if !strings.HasPrefix(endpoint, "http") {
endpoint = fmt.Sprintf("http://%s", endpoint)
}
return endpoint
}
func (g *garage) GetAdminKey() string {
key := os.Getenv("API_ADMIN_KEY")
if len(key) > 0 {
+5
View File
@@ -23,6 +23,11 @@ func ResponseError(w http.ResponseWriter, err error) {
w.Write([]byte(err.Error()))
}
func ResponseErrorStatus(w http.ResponseWriter, err error, status int) {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
}
func ResponseSuccess(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)