Skip to content

Commit

Permalink
feat: Add more code
Browse files Browse the repository at this point in the history
  • Loading branch information
dadav committed Feb 28, 2024
1 parent 1c4aede commit b252d98
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 58 deletions.
25 changes: 4 additions & 21 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -69,26 +68,10 @@ to quickly create a Cobra application.`,
return nil, errors.New("not implemented")
}, func(r *http.Request, data []byte) error {
if strings.HasPrefix(r.URL.Path, "/v3/files") {
releaseFile := filepath.Base(r.URL.Path)
module := v3.ReleaseToModule(strings.TrimSuffix(releaseFile, ".tar.gz"))
cacheFileDir := filepath.Join(config.ModulesDir, module)

if _, err := os.Stat(cacheFileDir); err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(cacheFileDir, os.ModePerm)
if err != nil {
return err
}
} else {
return err
}
}
cacheFile := filepath.Join(cacheFileDir, releaseFile)
err := os.WriteFile(cacheFile, data, 0644)
if err != nil {
return err
}
return backend.ConfiguredBackend.LoadModules()
releaseFile := strings.TrimSuffix(filepath.Base(r.URL.Path), ".tar.gz")
module := v3.ReleaseToModule(releaseFile)
version := strings.TrimPrefix(releaseFile, module)
return backend.ConfiguredBackend.AddRelease(module, version, data)
}
return errors.New("no caching")
}))
Expand Down
44 changes: 24 additions & 20 deletions internal/backend/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,31 @@ func (s *FilesystemBackend) GetAllReleases() []*gen.Release {
return result
}

func (s *FilesystemBackend) AddRelease(name string, data []byte) error {
func (s *FilesystemBackend) AddRelease(name, version string, data []byte) error {
releaseFile := fmt.Sprintf("%s-%s.tar.gz", name, version)
cacheFileDir := filepath.Join(config.ModulesDir, name)
if _, err := os.Stat(cacheFileDir); err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(cacheFileDir, os.ModePerm)
if err != nil {
return err
}
} else {
return err
}
}

cacheFile := filepath.Join(cacheFileDir, releaseFile)
_, err := os.Stat(cacheFile)
if err == nil {
return nil
}

err = os.WriteFile(cacheFile, data, 0644)
if err != nil {
return err
}
return nil
// module := v3.ReleaseToModule(name)
// cacheFileDir := filepath.Join(config.ModulesDir, module)
//
// if _, err := os.Stat(cacheFileDir); err != nil {
// if errors.Is(err, os.ErrNotExist) {
// err = os.MkdirAll(cacheFileDir, os.ModePerm)
// if err != nil {
// return err
// }
// } else {
// return err
// }
// }
// cacheFile := filepath.Join(cacheFileDir, releaseFile)
// err := os.WriteFile(cacheFile, data, 0644)
// if err != nil {
// return err
// }
// return nil
}

func (s *FilesystemBackend) GetAllModules() []*gen.Module {
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ type Backend interface {
GetReleaseBySlug(string) (*gen.Release, error)

// Add a new release
AddRelease(releaseName string, data []byte) error
AddRelease(module string, version string, data []byte) error
}
16 changes: 0 additions & 16 deletions internal/middleware/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ func CacheMiddleware(readCacheCallback func(r *http.Request) ([]byte, error), wr
return
}

// if err != nil {
// log.Log.Debugf("Skipping caching of %s?\n", r.URL.Path, r.URL.RawQuery)
// next.ServeHTTP(w, r)
// return
// }

// if _, err := os.Stat(cacheFile); err == nil {
// content, err := os.ReadFile(cacheFile)
// if err != nil {
// http.Error(w, "Failed to read cache", http.StatusInternalServerError)
// return
// }
// w.Write(data)
// return
// }

capturedResponseWriter := &capturedResponseWriter{ResponseWriter: w}
next.ServeHTTP(capturedResponseWriter, r)

Expand Down

0 comments on commit b252d98

Please sign in to comment.