mirror of
https://github.com/khairul169/garage-webui.git
synced 2026-09-15 00:43:21 +07:00
feat: add bucket object browser
This commit is contained in:
@@ -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())
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user