From fd6c9629765ba8acecac7de26b9cee57ab449697 Mon Sep 17 00:00:00 2001
From: Massimiliano Adamo <maxadamo@gmail.com>
Date: Thu, 1 Sep 2022 01:57:28 +0200
Subject: [PATCH] test gorilla

---
 main.go | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/main.go b/main.go
index 2d69d4b..f0e6055 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 {
-- 
GitLab