You've already forked golang-base-project
Adds a cache max age parameter and fixes cache parameter for main.js (#12)
Fixed throttling
This commit is contained in:
@ -18,4 +18,5 @@ type Config struct {
|
||||
SMTPSender string
|
||||
RequestsPerMinute int
|
||||
CacheParameter string
|
||||
CacheMaxAge int
|
||||
}
|
||||
|
2
dist/templates/footer.html
vendored
2
dist/templates/footer.html
vendored
@ -3,7 +3,7 @@
|
||||
<span class="text-muted">Fork this project on <a href="https://github.com/uberswe/golang-base-project">GitHub</a> | Created by <a href="https://github.com/uberswe">Markus Tenghamn</a></span>
|
||||
</div>
|
||||
</footer>
|
||||
<script src="/assets/js/main.js"></script>
|
||||
<script src="/assets/js/main.js?c={{ .CacheParameter }}"></script>
|
||||
{{ template "tracking.html" }}
|
||||
</body>
|
||||
</html>
|
@ -32,6 +32,7 @@ services:
|
||||
- STRICT_TRANSPORT_SECURITY=false
|
||||
- REQUESTS_PER_MINUTE=5
|
||||
- CACHE_PARAMETER=
|
||||
- CACHE_MAX_AGE=
|
||||
depends_on:
|
||||
- db
|
||||
ports:
|
||||
|
11
env.go
11
env.go
@ -77,5 +77,16 @@ func loadEnvVariables() (c config.Config) {
|
||||
if os.Getenv("CACHE_PARAMETER") != "" {
|
||||
c.CacheParameter = os.Getenv("CACHE_PARAMETER")
|
||||
}
|
||||
|
||||
// CacheMaxAge is how many seconds to cache static assets
|
||||
c.CacheMaxAge = 31536000
|
||||
if os.Getenv("CACHE_MAX_AGE") != "" {
|
||||
i, err := strconv.Atoi(os.Getenv("CACHE_MAX_AGE"))
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return
|
||||
}
|
||||
c.CacheMaxAge = i
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -53,7 +53,7 @@ func Run() {
|
||||
}
|
||||
|
||||
assets := r.Group("/assets")
|
||||
assets.Use(middleware.Cache())
|
||||
assets.Use(middleware.Cache(conf.CacheMaxAge))
|
||||
|
||||
assets.StaticFS("/", http.FS(subFS))
|
||||
|
||||
|
@ -1,10 +1,13 @@
|
||||
package middleware
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Cache() gin.HandlerFunc {
|
||||
func Cache(maxAge int) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Header("Cache-Control", "public, max-age=60")
|
||||
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%d", maxAge))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
@ -5,21 +5,15 @@ import (
|
||||
"github.com/ulule/limiter/v3"
|
||||
middlewareGin "github.com/ulule/limiter/v3/drivers/middleware/gin"
|
||||
"github.com/ulule/limiter/v3/drivers/store/memory"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Throttle(limit int) gin.HandlerFunc {
|
||||
// Define a limit rate to 3 requests per minute.
|
||||
rate, err := limiter.NewRateFromFormatted("5-M")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
store := memory.NewStore()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Create a new middleware with the limiter instance.
|
||||
return middlewareGin.NewMiddleware(limiter.New(store, rate))
|
||||
return middlewareGin.NewMiddleware(limiter.New(store, limiter.Rate{
|
||||
Period: time.Minute,
|
||||
Limit: int64(limit),
|
||||
}))
|
||||
}
|
||||
|
Reference in New Issue
Block a user