Skip to content
Snippets Groups Projects
Unverified Commit a645601f authored by Max Adamo's avatar Max Adamo
Browse files

feat: add no-cache option to disable caching in HTTP responses

parent 601a1697
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,7 @@ var ( ...@@ -25,6 +25,7 @@ var (
InfoLogger *log.Logger InfoLogger *log.Logger
WarningLogger *log.Logger WarningLogger *log.Logger
ErrorLogger *log.Logger ErrorLogger *log.Logger
noCacheBool bool
verboseBool bool verboseBool bool
baseURLs []string baseURLs []string
apiURLs []string apiURLs []string
...@@ -51,6 +52,11 @@ func renderJSON(w http.ResponseWriter, req *http.Request) { ...@@ -51,6 +52,11 @@ func renderJSON(w http.ResponseWriter, req *http.Request) {
if verboseBool { if verboseBool {
DebugLogger.Printf("JSON generation initiated for provider: %s", provider) DebugLogger.Printf("JSON generation initiated for provider: %s", provider)
} }
if noCacheBool {
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, err = w.Write(jsonData) _, err = w.Write(jsonData)
...@@ -76,7 +82,13 @@ func renderPage(w http.ResponseWriter, req *http.Request) { ...@@ -76,7 +82,13 @@ func renderPage(w http.ResponseWriter, req *http.Request) {
return return
} }
if noCacheBool {
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
}
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
if verboseBool { if verboseBool {
DebugLogger.Printf("Serving file: %s", serveFile) DebugLogger.Printf("Serving file: %s", serveFile)
} }
...@@ -136,9 +148,14 @@ func customHandler(w http.ResponseWriter, req *http.Request) { ...@@ -136,9 +148,14 @@ func customHandler(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path path := req.URL.Path
rootPath := []string{"/", "/index.html", "/index.htm"} rootPath := []string{"/", "/index.html", "/index.htm"}
if slices.Contains(rootPath, path) { if slices.Contains(rootPath, path) {
// if verboseBool { if noCacheBool {
// DebugLogger.Printf("Serving file: %s", path) w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
// } w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
}
if verboseBool {
DebugLogger.Printf("Serving file: %s", path)
}
http.ServeFile(w, req, filepath.Join(webDir, "index.html")) http.ServeFile(w, req, filepath.Join(webDir, "index.html"))
return return
} }
...@@ -197,7 +214,7 @@ func main() { ...@@ -197,7 +214,7 @@ func main() {
- serve ACME HTML pages, trigger Puppet, expose API - serve ACME HTML pages, trigger Puppet, expose API
Usage: Usage:
%v [--listen-address=LISTENADDRESS] [--listen-port=LISTENPORT] [--verbose] %v [--listen-address=LISTENADDRESS] [--listen-port=LISTENPORT] [--no-cache] [--verbose]
%v -h | --help %v -h | --help
%v -b | --build %v -b | --build
%v -v | --version %v -v | --version
...@@ -206,6 +223,7 @@ Options: ...@@ -206,6 +223,7 @@ Options:
-h --help Show this screen -h --help Show this screen
-b --build Print version and build information and exit -b --build Print version and build information and exit
-v --version Print version information and exit -v --version Print version information and exit
--no-cache Disable caching
--listen-address=LISTENADDRESS Web server address. Check Go net/http documentation [default: any] --listen-address=LISTENADDRESS Web server address. Check Go net/http documentation [default: any]
--listen-port=LISTENPORT Web server port [default: 8000] --listen-port=LISTENPORT Web server port [default: 8000]
--verbose Log also successful connections --verbose Log also successful connections
...@@ -233,6 +251,7 @@ Options: ...@@ -233,6 +251,7 @@ Options:
baseDir = "/etc" baseDir = "/etc"
webDir = "/var/www/acme_web" webDir = "/var/www/acme_web"
noCacheBool = arguments["--no-cache"].(bool)
verboseBool = arguments["--verbose"].(bool) verboseBool = arguments["--verbose"].(bool)
listenAddress := arguments["--listen-address"].(string) listenAddress := arguments["--listen-address"].(string)
listenPort := arguments["--listen-port"].(string) listenPort := arguments["--listen-port"].(string)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment