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

feat: implement custom HTTP handler with 404 fallback and enhance routing logic

parent 53355cff
No related branches found
No related tags found
No related merge requests found
...@@ -172,7 +172,6 @@ func ProcessCertificates(baseDir, provider string, verboseBool bool) ([]byte, er ...@@ -172,7 +172,6 @@ func ProcessCertificates(baseDir, provider string, verboseBool bool) ([]byte, er
return results[i].CertName < results[j].CertName return results[i].CertName < results[j].CertName
}) })
// Encode results directly into JSON
jsonData, err := json.MarshalIndent(results, "", " ") jsonData, err := json.MarshalIndent(results, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to encode JSON: %w", err) return nil, fmt.Errorf("failed to encode JSON: %w", err)
......
...@@ -80,7 +80,6 @@ func renderPage(w http.ResponseWriter, req *http.Request) { ...@@ -80,7 +80,6 @@ func renderPage(w http.ResponseWriter, req *http.Request) {
// trigger puppet // trigger puppet
func triggerPuppet(w http.ResponseWriter, req *http.Request) { func triggerPuppet(w http.ResponseWriter, req *http.Request) {
fmt.Printf("triggerPuppet function\n")
// content-type currently not working // content-type currently not working
cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "--signal", "SIGUSR1") cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "--signal", "SIGUSR1")
authToken := "BOFH" authToken := "BOFH"
...@@ -111,11 +110,72 @@ func triggerPuppet(w http.ResponseWriter, req *http.Request) { ...@@ -111,11 +110,72 @@ func triggerPuppet(w http.ResponseWriter, req *http.Request) {
// redirect to /by_name.html // redirect to /by_name.html
func redirect(w http.ResponseWriter, req *http.Request) { func redirect(w http.ResponseWriter, req *http.Request) {
fmt.Printf("redirect function\n")
redirectURL := filepath.Join(req.URL.Path, "/by_name.html") redirectURL := filepath.Join(req.URL.Path, "/by_name.html")
if verboseBool {
DebugLogger.Printf("running redirect to: %v", redirectURL)
}
http.Redirect(w, req, redirectURL, http.StatusMovedPermanently) http.Redirect(w, req, redirectURL, http.StatusMovedPermanently)
} }
// Custom HTTP handler with 404 fallback
func customHandler(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path
if path == "/" || path == "/index.html" || path == "/index.htm" {
//if verboseBool {
// DebugLogger.Printf("Serving file: %s", path)
//}
http.ServeFile(w, req, filepath.Join(webDir, "index.html"))
return
}
if path == "/favicon.ico" {
http.ServeFile(w, req, filepath.Join(webDir, "static", "anvil.ico"))
}
if path == "/ping" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"status": "ok"}`))
if err != nil {
WarningLogger.Printf("Failed to write ping response: %v", err)
}
return
}
// Check if the path matches any valid routes
for _, apiElement := range apiURLs {
if path == apiElement {
renderJSON(w, req)
return
}
}
for _, element := range baseURLs {
if path == element {
redirect(w, req)
return
}
}
for _, otherElement := range otherURLs {
if path == otherElement {
renderPage(w, req)
return
}
}
if path == "/puppet" {
triggerPuppet(w, req)
return
}
if strings.HasPrefix(path, "/static/") {
fs := http.FileServer(http.Dir(filepath.Join(webDir, "static")))
http.StripPrefix("/static/", fs).ServeHTTP(w, req)
return
}
// If no route matches, return 404
http.NotFound(w, req)
}
func main() { func main() {
progName := filepath.Base(os.Args[0]) progName := filepath.Base(os.Args[0])
...@@ -173,13 +233,13 @@ Options: ...@@ -173,13 +233,13 @@ Options:
baseURLs = append(baseURLs, "/"+provider, "/"+provider+"/") baseURLs = append(baseURLs, "/"+provider, "/"+provider+"/")
} }
for _, provider := range acmeProviders { for _, provider := range acmeProviders {
otherURLs = append(otherURLs, "/"+provider+"/by_name.html", "/"+provider+"/by_date.html") otherURLs = append(
otherURLs, "/"+provider+"/by_name.html", "/"+provider+"/by_date.html",
"/"+provider+"/"+provider+".json", "/"+provider+"/"+provider+"_expired.json",
)
} }
for _, provider := range acmeProviders { for _, provider := range acmeProviders {
apiURLs = append( apiURLs = append(apiURLs, "/api/"+provider, "/api/"+provider+"/")
apiURLs, "/api/"+provider, "/api/"+provider+"/",
"/api/"+provider+"/"+provider+".json", "/api/"+provider+"/"+provider+"_expired.json",
)
} }
if verboseBool { if verboseBool {
DebugLogger.Printf("Serving baseURLs: %v", baseURLs) DebugLogger.Printf("Serving baseURLs: %v", baseURLs)
...@@ -187,28 +247,7 @@ Options: ...@@ -187,28 +247,7 @@ Options:
DebugLogger.Printf("Serving otherURLs: %v", otherURLs) DebugLogger.Printf("Serving otherURLs: %v", otherURLs)
} }
puppetURL := "/puppet" http.HandleFunc("/", customHandler)
fs := http.FileServer(http.Dir(filepath.Join(webDir, "static")))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc(puppetURL, triggerPuppet)
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, filepath.Join(webDir, "index.html"))
})
for _, apiElement := range apiURLs {
http.HandleFunc(apiElement, renderJSON)
}
for _, element := range baseURLs {
http.HandleFunc(element, redirect)
}
for _, otherElement := range otherURLs {
http.HandleFunc(otherElement, renderPage)
}
if listenAddress == "any" { if listenAddress == "any" {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", listenPort), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", listenPort), nil))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment