Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware to configure response headers #39

Merged
merged 7 commits into from
Sep 29, 2020

Conversation

phartenfeller
Copy link
Contributor

Hi, first of thanks for this project I use it a lot :) !

This PR adds a middleware that lets you specify response HTTP Headers for certain paths and filetypes. With a JSON config that gets mounted into the container multiple header rules can be specified.

I added a documentation file for more info on how to use it.

Example config for implementing the caching best practices from gatsby.js :

{
  "configs": [
    {
      "path": "/page-data",
      "fileExtension": "json",
      "headers": [
        {
          "key": "cache-control",
          "value": "public, max-age=0, must-revalidate"
        }
      ]
    },
    {
      "path": "*",
      "fileExtension": "html",
      "headers": [
        {
          "key": "cache-control",
          "value": "public, max-age=0, must-revalidate"
        }
      ]
    },
    {
      "path": "/static/",
      "fileExtension": "*",
      "headers": [
        {
          "key": "cache-control",
          "value": "public, max-age=31536000, immutable"
        }
      ]
    },
    {
      "path": "*",
      "fileExtension": "js",
      "headers": [
        {
          "key": "cache-control",
          "value": "public, max-age=31536000, immutable"
        }
      ]
    },
    {
      "path": "*",
      "fileExtension": "css",
      "headers": [
        {
          "key": "cache-control",
          "value": "public, max-age=31536000, immutable"
        }
      ]
    }
  ]
}

Maybe take a closer look to the code as this is my first time programming in Go :) .

Would solve #24

Copy link
Owner

@PierreZ PierreZ left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the PR, I just added some changes 👍

customHeaders.go Outdated
func initHeaderConfig() bool {
headerConfigValid := false

if fileExists("/config/headerConfig.json") {
Copy link
Owner

Choose a reason for hiding this comment

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

Can the path be a parameter from cli with this default?

@PierreZ PierreZ merged commit 131f492 into PierreZ:master Sep 29, 2020
@PierreZ
Copy link
Owner

PierreZ commented Sep 29, 2020

Thanks a lot for your PR and congrats! 🚀🚀🚀

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.

2 participants