Skip to content

Commit

Permalink
fix(ui): Add comments, remove unneeded func, polish ui
Browse files Browse the repository at this point in the history
  • Loading branch information
dadav committed Dec 29, 2024
1 parent 9f62fc6 commit 6a2378d
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 123 deletions.
80 changes: 24 additions & 56 deletions internal/v3/backend/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/hashicorp/go-version"
)

// FilesystemBackend implements the Backend interface for local filesystem storage
type FilesystemBackend struct {
muModules sync.RWMutex
Modules map[string]*gen.Module
Expand All @@ -49,6 +50,7 @@ func NewFilesystemBackend(path string) *FilesystemBackend {
}
}

// findLatestVersion compares version strings and returns the most recent one
func findLatestVersion(releases []gen.ReleaseAbbreviated) string {
if len(releases) == 0 {
return defaultVersion
Expand Down Expand Up @@ -100,6 +102,7 @@ func (s *FilesystemBackend) GetAllReleases() ([]*gen.Release, error) {
return result, nil
}

// MetadataToRelease converts release metadata into a Release object
func MetadataToRelease(metadata *model.ReleaseMetadata) *gen.Release {
var releaseMetadataInterface map[string]interface{}
inrec, _ := json.Marshal(metadata)
Expand Down Expand Up @@ -345,29 +348,34 @@ func (s *FilesystemBackend) DeleteReleaseBySlug(slug string) error {
}

func (s *FilesystemBackend) LoadModules() error {
// don't overwrite data we already have on modules and releases
// Initialize maps if they haven't been created yet
if s.Modules == nil {
s.Modules = make(map[string]*gen.Module)
}
if s.Releases == nil {
s.Releases = make(map[string][]*gen.Release)
}

// Walk through all files in the modules directory recursively
err := filepath.Walk(s.ModulesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Skip directories and non-tar.gz files
if info.IsDir() || !strings.HasSuffix(info.Name(), ".tar.gz") {
return nil
}

log.Log.Debugf("Reading %s\n", path)
// Read the release archive file
releaseBytes, err := os.ReadFile(path)
if err != nil {
return err
}

// Process the release archive and add it to the backend
// This will update both s.Modules and s.Releases maps
_, err = s.AddRelease(releaseBytes)
return err
})
Expand All @@ -377,61 +385,14 @@ func (s *FilesystemBackend) LoadModules() error {
return nil
}

func ReadReleaseMetadataFromFile(path string) (*model.ReleaseMetadata, string, error) {
var jsonData bytes.Buffer
var releaseMetadata model.ReleaseMetadata
readme := new(strings.Builder)

f, err := os.Open(path)
if err != nil {
return nil, readme.String(), err
}
defer f.Close()

g, err := gzip.NewReader(f)
if err != nil {
return nil, readme.String(), err
}

tarReader := tar.NewReader(g)

for {
header, err := tarReader.Next()
if err == io.EOF {
break
}

if err != nil {
return nil, readme.String(), err
}

if header.Typeflag != tar.TypeReg {
continue
}

switch filepath.Base(header.Name) {
case "metadata.json":
_, err = io.Copy(&jsonData, tarReader)
if err != nil {
return nil, readme.String(), err
}

if err := json.Unmarshal(jsonData.Bytes(), &releaseMetadata); err != nil {
return nil, readme.String(), err
}

case "README.md":
_, err = io.Copy(readme, tarReader)
if err != nil {
return nil, readme.String(), err
}
default:
continue
}
}
return &releaseMetadata, readme.String(), nil
}

// ReadReleaseMetadataFromBytes extracts metadata and README from a gzipped tar archive
// Parameters:
// - data: byte slice containing the gzipped tar archive
//
// Returns:
// - *model.ReleaseMetadata: parsed metadata from metadata.json
// - string: contents of README.md
// - error: any errors encountered during processing
func ReadReleaseMetadataFromBytes(data []byte) (*model.ReleaseMetadata, string, error) {
if len(data) == 0 {
return nil, "", errors.New("empty data provided")
Expand All @@ -441,6 +402,7 @@ func ReadReleaseMetadataFromBytes(data []byte) (*model.ReleaseMetadata, string,
var releaseMetadata model.ReleaseMetadata
readme := new(strings.Builder)

// Create readers to process the gzipped tar data
f := bytes.NewReader(data)
g, err := gzip.NewReader(f)
if err != nil {
Expand All @@ -450,6 +412,7 @@ func ReadReleaseMetadataFromBytes(data []byte) (*model.ReleaseMetadata, string,

tarReader := tar.NewReader(g)

// Iterate through all files in the archive
for {
header, err := tarReader.Next()
if err == io.EOF {
Expand All @@ -460,12 +423,15 @@ func ReadReleaseMetadataFromBytes(data []byte) (*model.ReleaseMetadata, string,
return nil, readme.String(), err
}

// Skip if not a regular file
if header.Typeflag != tar.TypeReg {
continue
}

// Process only metadata.json and README.md files
switch filepath.Base(header.Name) {
case "metadata.json":
// Read and parse the metadata file
_, err = io.Copy(&jsonData, tarReader)
if err != nil {
return nil, readme.String(), err
Expand All @@ -475,10 +441,12 @@ func ReadReleaseMetadataFromBytes(data []byte) (*model.ReleaseMetadata, string,
return nil, readme.String(), err
}

// Validate the module name
if !utils.CheckModuleSlug(releaseMetadata.Name) {
return nil, readme.String(), errors.New("invalid module name")
}
case "README.md":
// Read the README contents
_, err = io.Copy(readme, tarReader)
if err != nil {
return nil, readme.String(), err
Expand Down
6 changes: 3 additions & 3 deletions internal/v3/ui/components/author.templ
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ templ AuthorView(modules []*gen.Module) {
<table id="modulesTable">
<thead>
<tr>
<th scope="col" onclick="sortTable('modulesTable', 0)">Module</th>
<th scope="col" onclick="sortTable('modulesTable', 1)">Version</th>
<th scope="col" onclick="sortTable('modulesTable', 0)">Module</th>
<th scope="col" onclick="sortTable('modulesTable', 1)">Version</th>
</tr>
</thead>
<tbody>
for _, module := range modules {
for _, module := range sortModules(modules) {
<tr>
<td>
<a href={ templ.URL(fmt.Sprintf("/modules/%s", module.Slug)) }>{ module.Name }</a>
Expand Down
4 changes: 2 additions & 2 deletions internal/v3/ui/components/author_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/v3/ui/components/search.templ
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ templ SearchView(query string, modules []*gen.Module) {
</thead>
<tbody id="search-results">
if len(modules) > 0 {
for _, module := range modules {
for _, module := range sortModules(modules) {
@ModuleToTableRow(module)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/v3/ui/components/search_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions internal/v3/ui/components/statistics.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@ package components

import (
customMiddleware "github.com/dadav/gorge/internal/middleware"
"sort"
"strconv"
"time"
)

func sortedKeys(stats *customMiddleware.Statistics) []string {
keys := []string{}
for k := range stats.ConnectionsPerEndpoint {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

templ StatisticsView(stats *customMiddleware.Statistics) {
<div>
<h3>Statistics</h3>
Expand All @@ -37,7 +27,7 @@ templ StatisticsView(stats *customMiddleware.Statistics) {
</tr>
</thead>
<tbody>
for _, path := range sortedKeys(stats) {
for _, path := range getSortedKeys(stats) {
<tr>
<td>{ path }</td>
<td>{ strconv.Itoa(stats.ConnectionsPerEndpoint[path]) }</td>
Expand Down
38 changes: 14 additions & 24 deletions internal/v3/ui/components/statistics_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6a2378d

Please sign in to comment.