converted to go modules, added gorilla web toolkit, added test, changed yosoy to respond with JSON rather than plain text, added travis support
This commit is contained in:
77
server.go
77
server.go
@@ -1,57 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type response struct {
|
||||
Host string `json:"host"`
|
||||
RequestURI string `json:"requestUri"`
|
||||
RemoteAddr string `json:"remoteAddr"`
|
||||
Counter int `json:"counter"`
|
||||
Headers map[string][]string `json:"headers"`
|
||||
Hostname string `json:"hostname"`
|
||||
EnvVariables []string `json:"envVariables,omitempty"`
|
||||
Files map[string]string `json:"files,omitempty"`
|
||||
}
|
||||
|
||||
var counter = 0
|
||||
var hostname = os.Getenv("HOSTNAME")
|
||||
var showEnvs = os.Getenv("YOSOY_SHOW_ENVS")
|
||||
var showFiles = os.Getenv("YOSOY_SHOW_FILES")
|
||||
|
||||
func handler(w http.ResponseWriter, req *http.Request) {
|
||||
remoteAddr := req.RemoteAddr
|
||||
if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
|
||||
remoteAddr = remoteAddr[0:index]
|
||||
}
|
||||
showEnvs := os.Getenv("YOSOY_SHOW_ENVS")
|
||||
showFiles := os.Getenv("YOSOY_SHOW_FILES")
|
||||
|
||||
response := &response{}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
fmt.Fprintf(w, "Request URI: %v\n", req.RequestURI)
|
||||
fmt.Fprintf(w, "Hostname: %v\n", hostname)
|
||||
fmt.Fprintf(w, "Remote IP: %v\n", remoteAddr)
|
||||
counter++
|
||||
fmt.Fprintf(w, "Called: %v\n", counter)
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintf(w, "HTTP headers:\n")
|
||||
headers := make([]string, 0, len(req.Header))
|
||||
for k := range req.Header {
|
||||
headers = append(headers, k)
|
||||
}
|
||||
sort.Strings(headers)
|
||||
for _, header := range headers {
|
||||
headers := req.Header[header]
|
||||
for _, h := range headers {
|
||||
fmt.Fprintf(w, "%v: %v\n", header, h)
|
||||
}
|
||||
}
|
||||
response.Counter = counter
|
||||
|
||||
remoteAddr := remoteAddrWithoutPort(req)
|
||||
response.RemoteAddr = remoteAddr
|
||||
|
||||
response.RequestURI = req.RequestURI
|
||||
response.Host = req.Host
|
||||
response.Headers = req.Header
|
||||
|
||||
response.Hostname = hostname
|
||||
|
||||
if strings.ToLower(showEnvs) == "true" || strings.ToLower(showEnvs) == "yes" || strings.ToLower(showEnvs) == "on" || showEnvs == "1" {
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintf(w, "Env variables:\n")
|
||||
for _, e := range os.Environ() {
|
||||
fmt.Fprintln(w, e)
|
||||
}
|
||||
response.EnvVariables = os.Environ()
|
||||
}
|
||||
if len(showFiles) > 0 {
|
||||
response.Files = make(map[string]string)
|
||||
files := strings.Split(showFiles, ",")
|
||||
for _, file := range files {
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
@@ -59,12 +56,22 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
||||
log.Printf("Could not read file %v: %v\n", file, err)
|
||||
continue
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintf(w, "File %v:\n", file)
|
||||
contents := string(bytes)
|
||||
fmt.Fprintln(w, contents)
|
||||
response.Files[file] = contents
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func remoteAddrWithoutPort(req *http.Request) string {
|
||||
remoteAddr := req.RemoteAddr
|
||||
if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
|
||||
remoteAddr = remoteAddr[0:index]
|
||||
}
|
||||
return remoteAddr
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
Reference in New Issue
Block a user