From 486a75bbd61f4f0670807e20b766d6ac6b902f4b Mon Sep 17 00:00:00 2001 From: Massimiliano Adamo <maxadamo@gmail.com> Date: Wed, 11 May 2022 23:41:05 +0200 Subject: [PATCH] add API url --- main.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index c34c13f..591628a 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,24 @@ func init() { ErrorLogger = log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime) } +// serve certificates list +func renderJSON(w http.ResponseWriter, req *http.Request) { + provider := strings.Split(req.URL.Path, "/")[2] + serveFile := fmt.Sprintf("%v/%v/%v.json", webDir, provider, provider) + cmd := exec.Command(jsonConverter, "-p", provider) + err := cmd.Run() + if err != nil { + WarningLogger.Println(err) + w.WriteHeader(http.StatusServiceUnavailable) + } else { + if verboseBool { + InfoLogger.Printf("HTTP Status %v", http.StatusOK) + } + w.Header().Set("Content-Type", "application/json; charset=utf-8") + } + http.ServeFile(w, req, serveFile) +} + // serve certificates list func renderPage(w http.ResponseWriter, req *http.Request) { provider := strings.Split(req.URL.Path, "/") @@ -42,7 +60,7 @@ func renderPage(w http.ResponseWriter, req *http.Request) { if verboseBool { InfoLogger.Printf("HTTP Status %v", http.StatusOK) } - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/html; charset=utf-8") } http.ServeFile(w, req, serveFile) } @@ -90,23 +108,23 @@ Options: listenPort := arguments["--listen-port"].(string) baseURLs := [6]string{"/sectigo_ev", "/sectigo_ov", "/letsencrypt", "/sectigo_ev/", "/sectigo_ov/", "/letsencrypt/"} - otherURLs := [12]string{"/letsencrypt/by_name.html", "/letsencrypt/by_date.html", - "/letsencrypt/letsencrypt.json", "/letsencrypt/letsencrypt_expired.json", + apiURLs := [6]string{"/api/sectigo_ev", "/api/sectigo_ov", "/api/letsencrypt", + "/api/sectigo_ev/", "/api/sectigo_ov/", "/api/letsencrypt/"} + otherURLs := [6]string{"/letsencrypt/by_name.html", "/letsencrypt/by_date.html", "/sectigo_ov/by_name.html", "/sectigo_ov/by_date.html", - "/sectigo_ov/sectigo_ov.json", "/sectigo_ov/sectigo_ov_expired.json", - "/sectigo_ev/by_name.html", "/sectigo_ev/by_date.html", - "/sectigo_ev/sectigo_ev.json", "/sectigo_ev/sectigo_ev_expired.json"} + "/sectigo_ev/by_name.html", "/sectigo_ev/by_date.html"} fs := http.FileServer(http.Dir("/var/www/acme_web/static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) - //http.HandleFunc("index.html", func(res http.ResponseWriter, req *http.Request) { - // http.ServeFile(res, req, filepath.Join(webDir, "index.html")) - //}) 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) } -- GitLab