switched to gorilla, added out of the box handlers for logging, proxy headers, and recovery

This commit is contained in:
Łukasz Budnik
2021-02-03 09:08:31 +01:00
parent b013a03c8f
commit cd90e887fe
5 changed files with 57 additions and 11 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
debug.test
yosoy

8
go.mod Normal file
View File

@@ -0,0 +1,8 @@
module github.com/lukaszbudnik/yosoy
go 1.15
require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
)

6
go.sum Normal file
View File

@@ -0,0 +1,6 @@
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=

View File

@@ -3,11 +3,14 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
var counter = 0
@@ -16,16 +19,11 @@ var showEnvs = os.Getenv("YOSOY_SHOW_ENVS")
var showFiles = os.Getenv("YOSOY_SHOW_FILES")
func handler(w http.ResponseWriter, req *http.Request) {
if req.RequestURI == "/favicon.ico" {
w.WriteHeader(http.StatusNotFound)
return
}
remoteAddr := req.RemoteAddr
if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
remoteAddr = remoteAddr[0:index]
}
fmt.Printf("[%v] - %v - %v - \"%v %v\"\n", hostname, time.Now().Format(time.RFC3339), remoteAddr, req.Method, req.RequestURI)
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "Request URI: %v\n", req.RequestURI)
@@ -58,7 +56,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
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)
log.Printf("Could not read file %v: %v\n", file, err)
continue
}
fmt.Fprintln(w)
@@ -70,7 +68,16 @@ func handler(w http.ResponseWriter, req *http.Request) {
}
func main() {
fmt.Printf("[%v] - %v - yosoy is up!\n", hostname, time.Now().Format(time.RFC3339))
http.HandleFunc("/", handler)
http.ListenAndServe(":80", nil)
log.Printf("yosoy is up %v\n", hostname)
r := mux.NewRouter()
r.Handle("/favicon.ico", r.NotFoundHandler)
r.PathPrefix("/").HandlerFunc(handler)
loggingRouter := handlers.CombinedLoggingHandler(os.Stdout, r)
proxyRouter := handlers.ProxyHeaders(loggingRouter)
recoveryRouter := handlers.RecoveryHandler()(proxyRouter)
http.ListenAndServe(":80", recoveryRouter)
}

23
server_test.go Normal file
View File

@@ -0,0 +1,23 @@
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}