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

test gorilla

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