Add caching headers for production, fix css for RMO
This commit is contained in:
parent
58da733531
commit
ee7dc1dd08
5 changed files with 17 additions and 10 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
|
@ -16,6 +17,8 @@ import (
|
|||
|
||||
// fileServer conveniently sets up a http.FileServer handler to serve
|
||||
// static files from a http.FileSystem.
|
||||
var startedTime time.Time = time.Now()
|
||||
|
||||
func fileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embed.FS, embeddedPath string) {
|
||||
if strings.ContainsAny(path, "{}*") {
|
||||
panic("FileServer does not permit any URL parameters.")
|
||||
|
|
@ -66,6 +69,18 @@ func fileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embe
|
|||
// Create a custom ResponseWriter that allows us to modify headers
|
||||
crw := &customResponseWriter{ResponseWriter: w}
|
||||
|
||||
// Add caching headers
|
||||
if config.IsProductionEnvironment() {
|
||||
ext := filepath.Ext(requestedPath)
|
||||
switch ext {
|
||||
case ".css", ".js", ".jpg", ".jpeg", ".png", ".gif", ".svg", ".woff", ".woff2", ".ttf":
|
||||
// Cache for 1 week (604800 seconds)
|
||||
crw.Header().Set("Cache-Control", "public, max-age=604800, stale-while-revalidate=86400")
|
||||
default:
|
||||
// Other files, 1 hour
|
||||
crw.Header().Set("Cache-Control", "public, max-age=3600")
|
||||
}
|
||||
}
|
||||
// Serve the file
|
||||
http.ServeContent(crw, r, requestedPath, startedTime, fileToServe)
|
||||
|
||||
|
|
|
|||
|
|
@ -2,15 +2,8 @@ package html
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RenderOrError(w http.ResponseWriter, template_name string, content interface{}) {
|
||||
templates.renderOrError(w, template_name, content)
|
||||
}
|
||||
|
||||
var startedTime time.Time
|
||||
|
||||
func SetStartedTime() {
|
||||
startedTime = time.Now()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ class AddressOrReportInput extends HTMLElement {
|
|||
this.shadowRoot.innerHTML = `
|
||||
<style>
|
||||
@import url('/static/css/bootstrap.css');
|
||||
@import url('/static/css/bootstrap-icons.css');
|
||||
@import url('/static/vendor/css/bootstrap-icons.min.css');
|
||||
.detail-label {
|
||||
font-size: 0.8rem;
|
||||
text-transform: uppercase;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<!-- Bootstrap & Custom CSS -->
|
||||
<link href="/static/css/bootstrap.css" rel="stylesheet" />
|
||||
<!-- Bootstrap Icons -->
|
||||
<link rel="stylesheet" href="/static/css/bootstrap-icons.css" />
|
||||
<link rel="stylesheet" href="/static/vendor/css/bootstrap-icons.min.css" />
|
||||
<!-- favicon -->
|
||||
<link rel="icon" href="/static/favicon-rmo.ico" type="image/x-icon" />
|
||||
{{ block "extraheader" . }}{{ end }}
|
||||
|
|
|
|||
1
main.go
1
main.go
|
|
@ -143,7 +143,6 @@ func main() {
|
|||
log.Error().Err(err).Msg("Failed to start openAI client")
|
||||
os.Exit(8)
|
||||
}
|
||||
http.SetStartedTime()
|
||||
server := &http.Server{
|
||||
Addr: config.Bind,
|
||||
Handler: r,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue