Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

cnlangzi
Copy link
Member

@cnlangzi cnlangzi commented May 4, 2025

Changed

Fixed

Added

  • feat(static): added asseturl feature

Tests

Tasks to complete before merging PR:

  • Ensure unit tests are passing. If not run make unit-tests to check for any regressions 📋
  • Ensure lint tests are passing. if not run make lint to check for any issues
  • Ensure codecov/patch is passing for changes.

Summary by Sourcery

Implement asset URL feature with enhanced static file handling and caching

New Features:

  • Added asset URL generation with ETag and cache control
  • Introduced new template functions for asset handling

Enhancements:

  • Improved file watching and static file serving
  • Updated ETag computation to use CRC32
  • Added more flexible template function management

Chores:

  • Refactored file viewer and template loading mechanisms
  • Updated tests to accommodate new changes

@sourcery-ai
Copy link

sourcery-ai bot commented May 4, 2025

Reviewer's Guide

This PR introduces an asseturl feature that generates unique, content-hashed URLs for static assets to enable long-term caching. It works by calculating a CRC32 hash (replacing the previous MD5) of asset files matching configurable patterns, creating URLs like file-<hash>.ext, and serving them with immutable cache headers. A new asset template function resolves original paths to these versioned URLs.

File-Level Changes

Change Details Files
Implemented asset URL generation for cache busting.
  • Added handleAssetUrl function to compute ETag (CRC32), generate versioned URL, and register a handler with immutable cache headers.
  • Added AssetURLs map to App to store original-to-versioned URL mappings.
  • Added buildAssetURLs slice and WithBuildAssetURL option to configure which files get versioned URLs.
  • Introduced asset template function (getAssetUrl) to look up versioned URLs.
  • Modified StaticViewEngine.handle to call handleAssetUrl for matching files.
  • Triggered asset handling on file write events in StaticViewEngine.FileChanged.
viewengine_static.go
app.go
option.go
funcmap.go
Refactored FileViewer to explicitly handle ETag and Cache-Control headers.
  • Added etag and cache fields to FileViewer.
  • Modified NewFileViewer to accept etag and cache parameters.
  • Updated Render to set ETag header based on FileViewer.etag.
  • Updated serveContent to set Cache-Control header based on FileViewer.cache.
viewer_file.go
viewengine_static.go
viewer_file_test.go
Changed ETag computation algorithm from MD5 to CRC32.
  • Updated ComputeETag default hash to crc32.NewIEEE().
  • Updated ETag calculation in CSRF and HTMX extensions to use CRC32.
etag.go
ext/csrf/csrf.go
ext/htmx/htmx.go
ext/csrf/csrf_test.go
viewengine_static.go
Introduced a centralized template function map and configuration options.
  • Added funcMap field to App, initialized with builtins.
  • Added WithTempalteFunc and WithTempalteFuncMap options.
  • Removed global FuncMap.
  • Updated template loading/reloading methods to accept and use the App's funcMap.
app.go
option.go
funcmap.go
template_html.go
template_text.go
viewengine_html.go
viewengine_text.go
app_test.go
template_text_test.go
Switched to using minified JavaScript files for CSRF and HTMX extensions.
  • Embedded .min.js files instead of .js.
  • Updated loadJavaScript functions to open the minified versions.
ext/csrf/csrf.go
ext/htmx/htmx.go
ext/csrf/csrf.min.js
ext/htmx/htmx.min.js
ext/htmx/htmx_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepsource-io
Copy link

deepsource-io bot commented May 4, 2025

Here's the code health analysis summary for commits add434c..059805f. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Go LogoGo✅ Success
🎯 1 occurence resolved
View Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@codecov
Copy link

codecov bot commented May 4, 2025

Codecov Report

Attention: Patch coverage is 96.66667% with 3 lines in your changes missing coverage. Please review.

Project coverage is 91.57%. Comparing base (add434c) to head (059805f).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
viewengine_html.go 33.33% 0 Missing and 2 partials ⚠️
viewengine_text.go 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #71      +/-   ##
==========================================
+ Coverage   91.28%   91.57%   +0.29%     
==========================================
  Files          62       63       +1     
  Lines        2490     2541      +51     
==========================================
+ Hits         2273     2327      +54     
+ Misses        188      186       -2     
+ Partials       29       28       -1     
Flag Coverage Δ
Unit-Tests 91.57% <96.66%> (+0.29%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cnlangzi - I've reviewed your changes - here's some feedback:

  • This PR includes refactoring of template function handling and changes to ETag generation alongside the main asset URL feature; consider splitting these into separate PRs.
  • The default ETag algorithm was changed from MD5 to CRC32; ensure the increased risk of collisions with CRC32 is acceptable for caching purposes.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@cnlangzi cnlangzi requested a review from Copilot May 4, 2025 02:53
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements an asset URL feature that enhances static file handling by introducing asset URL generation with ETag and cache control. Key changes include updating the NewFileViewer signature and its usage across tests and view engines, enhancing static view engine file handling with asset URL support, and updating etag computation to use crc32.

Reviewed Changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
viewer_file_test.go Updated NewFileViewer calls to accommodate new parameters.
viewer_file.go Modified NewFileViewer and Render logic for etag and cache handling.
viewengine_* Adjusted Load and Reload method signatures to pass funcMap.
option.go Added new option functions for template functions and asset URL building.
ext/* Switched to using minified JS files and updated etag computation.
etag.go Changed etag computation from md5 to crc32.
app.go and app_test.go Updated app initialization to incorporate new asset URL handling and template function registration.
Comments suppressed due to low confidence (1)

viewer_file.go:98

  • [nitpick] The type assertion 'f.(io.ReadSeeker)' assumes that f implements io.ReadSeeker; it may be safer to check the type assertion to avoid potential panics if the file does not satisfy the interface.
http.ServeContent(w, r, v.path, fi.ModTime(), f.(io.ReadSeeker))

@cnlangzi cnlangzi requested a review from Copilot May 4, 2025 07:32
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements the asset URL feature for static file handling by extending the file viewer and view engine APIs to support custom ETag and cache control parameters. Key changes include:

  • Updating the NewFileViewer API and its callers across multiple files to include new etag and cache parameters.
  • Refactoring template loading/reloading functions to require a function map, enabling more flexible template behavior.
  • Introducing asset URL generation support and modifications in static asset and CSRF handling to use CRC32-based ETag computation.

Reviewed Changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated no comments.

Show a summary per file
File Description
viewer_file_test.go Updated NewFileViewer calls for new parameters in test cases.
viewer_file.go Modified NewFileViewer signature and added cache header handling.
viewengine_text.go Updated template Load/Reload calls to include function map usage.
viewengine_static.go Added asset URL generation logic and cache control header support.
viewengine_html.go Adjusted template Reload/Load functions for function map usage.
template_text_test.go Updated tests to pass new function map parameters.
template_text.go Modified Load/Reload function signatures to include the function map.
template_html.go Updated to require function map parameter in Load/Reload methods.
option.go Added options for custom template functions and asset URL building.
funcmap_test.go New tests for function map validation (note potential client issue).
ext/htmx and ext/csrf files Updated JavaScript resource references and ETag computation to CRC32.
app.go and app_test.go Registered new asset URL function and updated tests accordingly.
etag.go Changed ETag computation from MD5 to CRC32-based hashing.
Comments suppressed due to low confidence (2)

funcmap_test.go:45

  • The variable 'client' is undefined. Replace 'client' with http.DefaultClient or use srv.Client() if available.
resp, err := client.Do(req)

app_test.go:854

  • The variable 'client' is undefined. Replace 'client' with http.DefaultClient or use srv.Client() to execute the HTTP request.
resp, err := client.Do(req)

@cnlangzi cnlangzi merged commit 7b70679 into main May 4, 2025
6 of 7 checks passed
@cnlangzi cnlangzi deleted the feat/funcmap branch May 4, 2025 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant