switched to gorilla, added out of the box handlers for logging, proxy headers, and recovery
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
debug.test
|
||||||
|
yosoy
|
||||||
8
go.mod
Normal file
8
go.mod
Normal 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
6
go.sum
Normal 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=
|
||||||
29
server.go
29
server.go
@@ -3,11 +3,14 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
var counter = 0
|
var counter = 0
|
||||||
@@ -16,16 +19,11 @@ var showEnvs = os.Getenv("YOSOY_SHOW_ENVS")
|
|||||||
var showFiles = os.Getenv("YOSOY_SHOW_FILES")
|
var showFiles = os.Getenv("YOSOY_SHOW_FILES")
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, req *http.Request) {
|
func handler(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.RequestURI == "/favicon.ico" {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
remoteAddr := req.RemoteAddr
|
remoteAddr := req.RemoteAddr
|
||||||
if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
|
if index := strings.LastIndex(remoteAddr, ":"); index > 0 {
|
||||||
remoteAddr = remoteAddr[0:index]
|
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.WriteHeader(http.StatusOK)
|
||||||
w.Header().Add("Content-Type", "text/plain")
|
w.Header().Add("Content-Type", "text/plain")
|
||||||
fmt.Fprintf(w, "Request URI: %v\n", req.RequestURI)
|
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 {
|
for _, file := range files {
|
||||||
bytes, err := ioutil.ReadFile(file)
|
bytes, err := ioutil.ReadFile(file)
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
fmt.Fprintln(w)
|
fmt.Fprintln(w)
|
||||||
@@ -70,7 +68,16 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("[%v] - %v - yosoy is up!\n", hostname, time.Now().Format(time.RFC3339))
|
log.Printf("yosoy is up %v\n", hostname)
|
||||||
http.HandleFunc("/", handler)
|
|
||||||
http.ListenAndServe(":80", nil)
|
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
23
server_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user