diff --git a/main.go b/main.go index 2d69d4b8219708952a262c4fa60604a5008823cb..f0e605535b030d07ace27490d8395e8ae970891e 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/docopt/docopt-go" + "github.com/gorilla/mux" "gopkg.in/ini.v1" ) @@ -31,7 +32,7 @@ func init() { ErrorLogger = log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime) } -// write to file +// write file func writeFile(fileContent string, filePath string) { content := []byte(fileContent) err := os.WriteFile(filePath, content, 0644) @@ -78,21 +79,16 @@ func renderPage(w http.ResponseWriter, req *http.Request) { // trigger puppet func triggerPuppet(w http.ResponseWriter, req *http.Request) { - //cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "-s", "SIGUSR1") - cmd := exec.Command("/usr/bin/touch", "/TEST") + cmd := exec.Command("/usr/bin/pkill", "-f", "/opt/puppetlabs/puppet/bin/puppet", "-s", "SIGUSR1") authToken := strings.Split(req.Header.Get("Authorization"), "Bearer ")[1] statusFile := "/tmp/200.json" - ok := fmt.Sprintf("{\n \"status\": \"OK\",\n \"response\": 200\n \"token\": %v\n}", authToken) - ko := fmt.Sprintf("{\n \"status\": \"OK\",\n \"response\": 401\n \"token\": %v\n}", authToken) + ok := fmt.Sprintln("{\n \"status\": \"OK\",\n \"response\": 200\n}") + ko := fmt.Sprintln("{\n \"status\": \"OK\",\n \"response\": 401\n}") writeFile(ok, "/tmp/200.json") writeFile(ko, "/tmp/401.json") w.Header().Set("Content-Type", "application/json") if authToken != bearerToken { - statusFile = "/tmp/401.json" - //w.WriteHeader(http.StatusUnauthorized) http.Error(w, ko, http.StatusUnauthorized) - //http.ServeFile(w, req, "/tmp/401.json") - //http.ServeFile(w, req, statusFile) } else { cmd.Run() if verboseBool { @@ -102,12 +98,17 @@ func triggerPuppet(w http.ResponseWriter, req *http.Request) { } } -// function redirect +// redirect to /by_name.html func redirect(w http.ResponseWriter, req *http.Request) { redirectURL := filepath.Join(req.URL.Path, "/by_name.html") http.Redirect(w, req, redirectURL, http.StatusMovedPermanently) } +// throw 404 +func throwNotFound(w http.ResponseWriter, req *http.Request) { + http.NotFound(w, req) +} + func main() { progName := filepath.Base(os.Args[0]) @@ -143,7 +144,6 @@ Options: fmt.Printf("Fail to read file: %v", err) os.Exit(1) } - //bearerToken = fmt.Sprintf("Bearer %v", cfg.Section("acme").Key("bearer_token").String()) bearerToken = cfg.Section("acme").Key("bearer_token").String() webDir = "/var/www/acme_web" @@ -184,6 +184,9 @@ Options: http.HandleFunc(otherElement, renderPage) } + rtr := mux.NewRouter() + rtr.HandleFunc("/number/{.+}", throwNotFound) + if listenAddress == "any" { log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", listenPort), nil)) } else {