From 05675455faa56302832660f19719c08bd939568b Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 4 Mar 2019 11:18:22 +0200 Subject: [PATCH 1/2] Add default index.html and not found 404.html --- Dockerfile | 2 ++ httpd/404.html | 9 +++++++++ httpd/index.html | 9 +++++++++ server.go | 4 ++-- 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 httpd/404.html create mode 100644 httpd/index.html diff --git a/Dockerfile b/Dockerfile index 184df50..e2b33f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,4 +20,6 @@ FROM alpine RUN apk add ca-certificates COPY --from=builder /go/bin/wio /bin/wio +COPY httpd /usr/share/httpd + ENTRYPOINT ["/bin/wio"] diff --git a/httpd/404.html b/httpd/404.html new file mode 100644 index 0000000..2e5ed82 --- /dev/null +++ b/httpd/404.html @@ -0,0 +1,9 @@ + + + + 404.html + + +

Page not found

+ + \ No newline at end of file diff --git a/httpd/index.html b/httpd/index.html new file mode 100644 index 0000000..370acfb --- /dev/null +++ b/httpd/index.html @@ -0,0 +1,9 @@ + + + + index.html + + +

Default page

+ + \ No newline at end of file diff --git a/server.go b/server.go index 14566eb..9f18876 100644 --- a/server.go +++ b/server.go @@ -17,7 +17,7 @@ var ( func notFoundHandler(ctx *fasthttp.RequestCtx) { ctx.Logger().Printf("File %s not found, defaulting to index.html", ctx.Path()) - ctx.Request.SetRequestURI("/index.html") + ctx.Request.SetRequestURI("/404.html") fsHandler(ctx) } @@ -26,7 +26,7 @@ func requestHandler(ctx *fasthttp.RequestCtx) { } func createFsHandler(stripSlashes int) fasthttp.RequestHandler { - fs := &fasthttp.FS{ + fs := &fasthttp.FS { Root: *root, Compress: *compress, IndexNames: []string{"index.html"}, From d48b45df3794cb63163d3ba7a137c9fb3e36f866 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 4 Mar 2019 16:11:02 +0200 Subject: [PATCH 2/2] Add default index.html and not found 404.html --- Dockerfile | 2 -- httpd/404.html | 9 --------- httpd/index.html | 9 --------- server.go | 23 ++++++++++++++++++++--- 4 files changed, 20 insertions(+), 23 deletions(-) delete mode 100644 httpd/404.html delete mode 100644 httpd/index.html diff --git a/Dockerfile b/Dockerfile index e2b33f6..184df50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,4 @@ FROM alpine RUN apk add ca-certificates COPY --from=builder /go/bin/wio /bin/wio -COPY httpd /usr/share/httpd - ENTRYPOINT ["/bin/wio"] diff --git a/httpd/404.html b/httpd/404.html deleted file mode 100644 index 2e5ed82..0000000 --- a/httpd/404.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - 404.html - - -

Page not found

- - \ No newline at end of file diff --git a/httpd/index.html b/httpd/index.html deleted file mode 100644 index 370acfb..0000000 --- a/httpd/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - index.html - - -

Default page

- - \ No newline at end of file diff --git a/server.go b/server.go index 9f18876..d5380f9 100644 --- a/server.go +++ b/server.go @@ -1,6 +1,7 @@ package main import ( + "os" "flag" "log" @@ -10,6 +11,7 @@ import ( var ( addr = flag.String("h", "0.0.0.0:8080", "TCP address to listen to") root = flag.String("d", "/usr/share/httpd", "Directory to serve static files from") + file = flag.String("f", "index.html", "File to serve") strip = flag.Int("s", 0, "Number of mount point to skip") compress = flag.Bool("c", false, "Enables transparent response compression if set to true") fsHandler fasthttp.RequestHandler @@ -17,8 +19,23 @@ var ( func notFoundHandler(ctx *fasthttp.RequestCtx) { ctx.Logger().Printf("File %s not found, defaulting to index.html", ctx.Path()) - ctx.Request.SetRequestURI("/404.html") - fsHandler(ctx) + if isIndexFileExists() { + ctx.Request.SetRequestURI("/index.html") + fsHandler(ctx) + } else { + ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError) + } +} + +func isIndexFileExists() bool { + path := *root + "/" + *file + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + log.Printf("File %s do not exists", *file) + return false + } + } + return true } func requestHandler(ctx *fasthttp.RequestCtx) { @@ -45,7 +62,7 @@ func main() { fsHandler = createFsHandler(*strip) // Start HTTP server. - if len(*addr) > 0 { + if len(*addr) > 0 && isIndexFileExists() { log.Printf("Starting HTTP server on %q", *addr) log.Printf("Serving files from directory %q", *root) if err := fasthttp.ListenAndServe(*addr, requestHandler); err != nil {