-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
177 lines (151 loc) · 4.47 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"flag"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
"strings"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
const (
banner = " _ _ _ ______ _ _____\n" +
"| | | | | | | ____| | | / ____|\n" +
"| |__| | ___| |_ __ ___ | |__ ___| |__ ___| (___ ___ _ ____ _____ _ __\n" +
"| __ |/ _ | | '_ ` _ \\ | __| / __| '_ \\ / _ \\\\___ \\ / _ | '__\\ \\ / / _ | '__|\n" +
"| | | | __| | | | | | | | |___| (__| | | | (_) ____) | __| | \\ V | __| |\n" +
"|_| |_|\\___|_|_| |_| |_| |______\\___|_| |_|\\___|_____/ \\___|_| \\_/ \\___|_|"
contentFile = "/tmp/content"
)
var p string
func main() {
flag.StringVar(&p, "p", "8080", "the port to expose the server on.")
flag.Parse()
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.GET("/", hello)
e.GET("/env", env)
e.GET("/content", content)
e.POST("/content", writeContent)
// Start server
e.Logger.Fatal(e.Start(":" + p))
}
// Handler
func hello(c echo.Context) error {
return c.String(http.StatusOK, getHello()+getSpecificEnv("KUBERNETES")+getSpecificEnv("HELM")+getSpecificEnv("POD")+getSpecificEnv("CUSTOM")+getHeaders(c)+getResponse(c)+getHelp())
}
// Handler
func env(c echo.Context) error {
return c.String(http.StatusOK, getEnv())
}
// Handler
func content(c echo.Context) error {
return c.String(http.StatusOK, getContent())
}
// Handler
func writeContent(c echo.Context) error {
m := c.FormValue("message")
return c.String(http.StatusOK, appendContent(m))
}
func getHello() string {
s := banner + "\nHello, I am Helm Echoserver, I echo some cool stuff about myself!\n\n"
return s
}
func getSpecificEnv(q string) string {
s := "\n\n -----> " + strings.ToUpper(q) + " \n\n"
l := extractEnv(strings.ToUpper(q))
if len(l) > 0 {
for _, e := range l {
pair := strings.Split(e, "=")
s = s + pair[0] + "=" + pair[1] + "\n"
}
} else {
s = s + "-no " + strings.ToLower(q) + " info- "
}
return s
}
func getHeaders(c echo.Context) string {
s := "\n\n -----> HEADERS \n\n"
s = s + " -> Request : " + c.Request().Proto + " " + c.Request().Method + " " + c.Request().URL.RequestURI() + "\n"
s = s + " -> Host: " + c.Request().Host + "\n"
s = s + " -> Remote IP: " + c.RealIP() + "\n"
s = s + " -> Uri: " + c.Request().RequestURI + "\n"
p := c.Request().URL.Path
if p == "" {
p = "/"
}
s = s + " -> Path: " + p + "\n"
s = s + " -> Referer: " + c.Request().Referer() + "\n"
s = s + " -> User Agent: " + c.Request().UserAgent() + "\n"
l := c.Request().Header.Get(echo.HeaderContentLength)
if l == "" {
l = "0"
}
s = s + " -> Content Length: " + l + "\n"
//s = s + " -> Content Length: " + c.Request().Header.Get(tag[7:]) + "\n"
s = s + " -> Remote Address: " + c.Request().RemoteAddr + "\n"
return s
}
func getResponse(c echo.Context) string {
s := "\n\n -----> RESPONSE \n\n"
if c.Response() != nil {
s = s + " -> Status: " + strconv.Itoa(c.Response().Status) + "\n"
s = s + " -> Content Length: " + strconv.FormatInt(c.Response().Size, 10) + "\n"
} else {
s = s + "-no response info-"
}
return s
}
func getHelp() string {
s := "\n\n -----> Explore my other endpoints \n\n"
s = s + " -> GET /env : prints all env variables where I am running.\n"
s = s + " -> GET /content : reads the content of /tmp/content file where I am running.\n"
s = s + " -> POST /content : writes (appends) to the content of /tmp/content file where I am running.\n"
s = s + "e.g. curl -F \"message= a newely added content.\" http://localhost:" + p + "/content"
return s
}
func getEnv() string {
s := "\n -----> My environment |\n\n"
envs := os.Environ()
sort.Strings(envs)
for _, e := range envs {
pair := strings.Split(e, "=")
s = s + pair[0] + "=" + pair[1] + "\n"
}
return s
}
func extractEnv(s string) []string {
var result []string
envs := os.Environ()
sort.Strings(envs)
for _, e := range envs {
if strings.HasPrefix(e, s) {
result = append(result, e)
}
}
return result
}
func getContent() string {
s, err := ioutil.ReadFile(contentFile)
if err != nil {
return "File " + contentFile + " does not exist."
}
return string(s)
}
func appendContent(m string) string {
f, err := os.OpenFile(contentFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err.Error()
}
defer f.Close()
if _, err = f.WriteString(m); err != nil {
return err.Error()
}
return "Appended your message: [ " + m + " ] to " + contentFile
}