Skip to content

Commit

Permalink
update static pages, dockerfile and add license info
Browse files Browse the repository at this point in the history
  • Loading branch information
lovellfelix committed May 4, 2023
1 parent 6317cd0 commit fbc6c38
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ COPY --from=build /go/src/github.com/raspbernetes/custom-error-pages .

USER nonroot:nonroot

ENTRYPOINT ["/custom-error-pages"]
ENTRYPOINT ["/custom-error-pages"]
58 changes: 49 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand All @@ -10,7 +26,7 @@ import (
"strconv"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

Expand Down Expand Up @@ -45,31 +61,51 @@ const (
// ErrFilesPathVar is the name of the environment variable indicating
// the location on disk of files served by the handler.
ErrFilesPathVar = "ERROR_FILES_PATH"

// DefaultFormatVar is the name of the environment variable indicating
// the default error MIME type that should be returned if either the
// client does not specify an Accept header, or the Accept header provided
// cannot be mapped to a file extension.
DefaultFormatVar = "DEFAULT_RESPONSE_FORMAT"
)

func init() {
prometheus.MustRegister(requestCount)
prometheus.MustRegister(requestDuration)
}

func main() {
errFilesPath := "./pages"
errFilesPath := "/www"
if os.Getenv(ErrFilesPathVar) != "" {
errFilesPath = os.Getenv(ErrFilesPathVar)
}

http.HandleFunc("/", errorHandler(errFilesPath))
defaultFormat := "text/html"
if os.Getenv(DefaultFormatVar) != "" {
defaultFormat = os.Getenv(DefaultFormatVar)
}

http.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))

http.Handle("/metrics", promhttp.Handler())

http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
fmt.Fprint(w)
})

http.ListenAndServe(fmt.Sprintf(":8080"), nil)
}

func errorHandler(path string) func(http.ResponseWriter, *http.Request) {
func errorHandler(path, defaultFormat string) func(http.ResponseWriter, *http.Request) {
defaultExts, err := mime.ExtensionsByType(defaultFormat)
if err != nil || len(defaultExts) == 0 {
panic("couldn't get file extension for default format")
}
defaultExt := defaultExts[0]

return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ext := "html"
ext := defaultExt

if os.Getenv("DEBUG") != "" {
w.Header().Set(FormatHeader, r.Header.Get(FormatHeader))
Expand All @@ -85,14 +121,14 @@ func errorHandler(path string) func(http.ResponseWriter, *http.Request) {

format := r.Header.Get(FormatHeader)
if format == "" {
format = "text/html"
format = defaultFormat
log.Printf("format not specified. Using %v", format)
}

cext, err := mime.ExtensionsByType(format)
if err != nil {
log.Printf("unexpected error reading media type extension: %v. Using %v", err, ext)
format = "text/html"
format = defaultFormat
} else if len(cext) == 0 {
log.Printf("couldn't get media type extension. Using %v", ext)
} else {
Expand All @@ -111,6 +147,10 @@ func errorHandler(path string) func(http.ResponseWriter, *http.Request) {
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
// special case for compatibility
if ext == ".htm" {
ext = ".html"
}
file := fmt.Sprintf("%v/%v%v", path, code, ext)
f, err := os.Open(file)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Collect and display prometheus metrics

package main
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions www/401.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "message": "You do not have permission to perform this request" }
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions www/404.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "message": "The page you're looking for could not be found" }
File renamed without changes.
1 change: 1 addition & 0 deletions www/500.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "message": "An internal server error has been occured" }
File renamed without changes.
1 change: 1 addition & 0 deletions www/503.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "message": "The server is temporarily unavailable due maintenance" }

0 comments on commit fbc6c38

Please # to comment.