Esempio n. 1
0
func main() {
	app := mux.NewRouter()
	app.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

	app.Get("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "hello")
	})

	notFound := middleware.NotFound(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "custom 404 page")
	})

	fileServer := middleware.FileServer(http.Dir("."))
	logger := middleware.Logger

	log.Println("listening on port 8080")
	log.Fatal(http.ListenAndServe(":8080", alice.New(logger, notFound, fileServer).Then(app)))
}
Esempio n. 2
0
	It("should return the 404 message", func() {
		assert.Equal(t, "404 page not found\n", w.Body.String())
	})
}

const ServeDir = "__fixtures__"

var (
	files = map[string]string{
		"foo.txt":         "this is foo",
		"bar.txt":         "this is bar",
		"testdir/foo.txt": "this is nested foo",
		"testdir/bar.txt": "this is nested bar",
	}

	fs = middleware.FileServer(http.Dir(ServeDir))
)

func TestFileServer(t *testing.T) {
	err := os.MkdirAll(fmt.Sprintf("%s/testdir", ServeDir), 0755)
	if err != nil {
		t.Fatal(err)
	}

	for path, contents := range files {
		err := ioutil.WriteFile(fmt.Sprintf("%s/%s", ServeDir, path), []byte(contents), 0644)
		if err != nil {
			t.Fatal(err)
		}
	}