~blu/artemis

6a7033f69c84b8ecc57a396b19a19f3841752ce2 — Bryant Conquest 1 year, 4 months ago 0b67f71
updates :D
2 files changed, 22 insertions(+), 37 deletions(-)

M cache/cache.go
M main.go
M cache/cache.go => cache/cache.go +18 -21
@@ 25,12 25,11 @@ type Cache struct {
}

type CachedAsset struct {
	Name        string
	Size        int
	Hits        uint64
	PathHash    string
	ContentHash string
	DiesAt      time.Time
	Name     string
	Size     int
	Hits     uint64
	PathHash string
	DiesAt   time.Time
}

var (


@@ 69,9 68,7 @@ func (c *Cache) PurgeCache(pattern string) error {
	c.Files.Range(func(key any, value any) bool {
		a := value.(CachedAsset)
		if r.MatchString(a.Name) {
			if err := c.removeAsset(&a); err != nil {
				log.Println(err)
			}
			c.removeAsset(&a)
		}
		return true
	})


@@ 146,10 143,7 @@ func (c *Cache) freeCache(toFreeSpace uint64) error {

	for _, a := range toFree {
		log.Println(filepath.Join(c.CacheDirectory, a.PathHash))
		if err := c.removeAsset(&a); err != nil {
			log.Println(err)
		}
		c.Files.Delete(a.ContentHash)
		c.removeAsset(&a)
	}
	return nil
}


@@ 199,12 193,11 @@ func (c *Cache) downloadAsset(path string) ([]byte, error) {
	}

	c.Files.Store(hashPath(path), CachedAsset{
		Name:        path,
		Size:        len(data),
		Hits:        1,
		PathHash:    hashPath(path),
		ContentHash: HashContent(data),
		DiesAt:      time.Now().Add(1 * time.Hour), // TODO: Configure
		Name:     path,
		Size:     len(data),
		Hits:     1,
		PathHash: hashPath(path),
		DiesAt:   time.Now().Add(1 * time.Hour), // TODO: Configure
	})

	return data, nil


@@ 215,10 208,14 @@ func (c *Cache) writeAsset(hash string, data []byte) error {
	return os.WriteFile(path, data, 0o644)
}

func (c *Cache) removeAsset(a *CachedAsset) error {
func (c *Cache) removeAsset(a *CachedAsset) {
	path := filepath.Join(c.CacheDirectory, a.PathHash)
	fileDeaths.Add(a.PathHash, 1)
	return os.Remove(path)

	if err := os.Remove(path); err != nil {
		c.Files.Delete(a.PathHash)
		log.Println(err)
	}
}

func HashContent(data []byte) string {

M main.go => main.go +4 -16
@@ 7,7 7,6 @@ import (
	"log"
	"net/http"
	"strings"
	"sync"

	"tailscale.com/tsweb"



@@ 22,9 21,7 @@ var (
)

type Handler struct {
	Cache    *cache.Cache
	etagLock *sync.RWMutex
	etags    map[string]bool
	Cache *cache.Cache
}

func HealthCheckHandler() http.Handler {


@@ 66,9 63,7 @@ func AssetHandler(h *Handler) http.Handler {
		}

		etag := r.Header.Get("If-None-Match")
		h.etagLock.RLock()
		ok := h.etags[etag]
		h.etagLock.RUnlock()
		_, ok := h.Cache.Files.Load(etag)
		if ok {
			w.WriteHeader(http.StatusNotModified)
			return


@@ 81,12 76,7 @@ func AssetHandler(h *Handler) http.Handler {
			return
		}

		// Does not ever get invalidated
		// should etags be handled by the cache?
		etag = fmt.Sprintf("%x", sha256.Sum256(data))
		h.etagLock.Lock()
		h.etags[etag] = true
		h.etagLock.Unlock()
		etag = fmt.Sprintf("%x", sha256.Sum256([]byte(path)))
		w.Header().Set("etag", etag)
		w.Header().Set("Cache-Control", "max-age=86400")
		w.Write(data)


@@ 136,9 126,7 @@ func main() {
	}

	h := Handler{
		Cache:    cache,
		etagLock: &sync.RWMutex{},
		etags:    make(map[string]bool),
		Cache: cache,
	}

	go cache.DeathRunner()