first version, prints request URI, time, counts requests, can show env variables, and files

This commit is contained in:
Łukasz Budnik
2020-11-13 15:00:37 +01:00
parent 088f345c70
commit fce263c463
2 changed files with 76 additions and 0 deletions

16
Docker Normal file
View File

@@ -0,0 +1,16 @@
FROM golang:1.13.5-alpine3.10 as builder
MAINTAINER Łukasz Budnik lukasz.budnik@gmail.com
# build yosoy
RUN apk add git
RUN git clone https://github.com/lukaszbudnik/yosoy.git
RUN cd /go/yosoy && go build
FROM alpine:3.10
COPY --from=builder /go/yosoy/yosoy /bin
# register entrypoint
ENTRYPOINT ["yosoy"]
EXPOSE 80

60
server.go Normal file
View File

@@ -0,0 +1,60 @@
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
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) {
fmt.Printf("[%v] - %v - %v - \"%v %v\"\n", hostname, time.Now().Format(time.RFC3339), req.RemoteAddr, req.Method, req.RequestURI)
w.WriteHeader(200)
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", req.RemoteAddr)
counter++
fmt.Fprintf(w, "Called: %v\n", counter)
fmt.Fprintln(w)
fmt.Fprintf(w, "HTTP headers:\n")
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
if showEnvs == "1" || strings.ToLower(showEnvs) == "yes" || strings.ToLower(showEnvs) == "true" {
fmt.Fprintln(w)
fmt.Fprintf(w, "Env variables:\n")
for _, e := range os.Environ() {
fmt.Fprintln(w, e)
}
}
if len(showFiles) > 0 {
files := strings.Split(showFiles, ",")
for _, file := range files {
bytes, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("[%v] - %v - could not read file %v: %v\n", hostname, time.Now().Format(time.RFC3339), file, err)
continue
}
fmt.Fprintln(w)
fmt.Fprintf(w, "File %v:\n", file)
contents := string(bytes)
fmt.Fprintln(w, contents)
}
}
}
func main() {
fmt.Printf("[%v] - %v - YoSoy is up!\n", hostname, time.Now().Format(time.RFC3339))
http.HandleFunc("/", handler)
http.ListenAndServe(":80", nil)
}