/*
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 (
	"fmt"
	"html/template"
	"log"
	"mime"
	"net/http"
	"net/url"
	"os"
	"path"
	"strconv"
	"strings"
	"time"

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

const (
	// FormatHeader name of the header used to extract the format
	FormatHeader = "X-Format"

	// CodeHeader name of the header used as source of the HTTP statu code to return
	CodeHeader = "X-Code"

	// ContentType name of the header that defines the format of the reply
	ContentType = "Content-Type"

	// OriginalURI name of the header with the original URL from NGINX
	OriginalURI = "X-Original-URI"

	// Namespace name of the header that contains information about the Ingress namespace
	Namespace = "X-Namespace"

	// IngressName name of the header that contains the matched Ingress
	IngressName = "X-Ingress-Name"

	// ServiceName name of the header that contains the matched Service in the Ingress
	ServiceName = "X-Service-Name"

	// ServicePort name of the header that contains the matched Service port in the Ingress
	ServicePort = "X-Service-Port"

	// ErrFilesPathVar is the name of the environment variable indicating
	// the location on disk of files served by the handler.
	ErrFilesPathVar = "ERROR_FILES_PATH"
)

type ErrorInfo struct {
	Url  string
	Code int
}

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

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

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

	http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

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

func errorHandler(errorFilesPath string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		exts := []string{"html", "htm"}

		if os.Getenv("DEBUG") != "" {
			w.Header().Set(FormatHeader, r.Header.Get(FormatHeader))
			w.Header().Set(CodeHeader, r.Header.Get(CodeHeader))
			w.Header().Set(ContentType, r.Header.Get(ContentType))
			w.Header().Set(OriginalURI, r.Header.Get(OriginalURI))
			w.Header().Set(Namespace, r.Header.Get(Namespace))
			w.Header().Set(IngressName, r.Header.Get(IngressName))
			w.Header().Set(ServiceName, r.Header.Get(ServiceName))
			w.Header().Set(ServicePort, r.Header.Get(ServicePort))
		}

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

		cext, err := mime.ExtensionsByType(format)
		if err != nil {
			format = "text/html"
			log.Printf("unexpected error reading media type extension: %v. Using %v", err, strings.Join(exts, ", "))
		} else if len(cext) == 0 {
			format = "text/html"
			log.Printf("couldn't get media type extension. Using %v", strings.Join(exts, ", "))
		} else {
			exts = cext
		}
		w.Header().Set(ContentType, format)

		errCode := r.Header.Get(CodeHeader)
		originalURI, _ := url.Parse(r.Header.Get(OriginalURI))
		var requestURI string
		if originalURI != nil {
			requestURI, err = url.PathUnescape(originalURI.RequestURI())
		}
		code, err := strconv.Atoi(errCode)
		if err != nil {
			code = 404
			log.Printf("unexpected error reading return code: %v. Using %v", err, code)
			err = nil
		}
		w.WriteHeader(code)

		var file string
		for _, ext := range exts {
			var filePath string

			if !strings.HasPrefix(ext, ".") {
				ext = fmt.Sprintf(".%v", ext)
			}

			filePath = fmt.Sprintf("%v/%v%v", errorFilesPath, code, ext)
			if _, err = os.Stat(filePath); err == nil {
				file = filePath
				break
			}

			scode := strconv.Itoa(code)
			filePath = fmt.Sprintf("%v/%cxx%v", errorFilesPath, scode[0], ext)
			if _, err = os.Stat(filePath); err == nil {
				file = filePath
				break
			}
		}
		if file == "" {
			log.Printf("unexpected error opening file: %v", err)
			http.NotFound(w, r)
			return
		}

		tmpl, err := template.New(path.Base(file)).ParseFiles(file)
		if err != nil {
			log.Printf("unexpected error executing template: %v", err)
			http.NotFound(w, r)
			return
		}
		log.Printf("serving custom error response for code %v and format %v from file %v", code, format, file)
		err = tmpl.Execute(w, ErrorInfo{
			Url:  requestURI,
			Code: code,
		})
		if err != nil {
			log.Printf("unexpected error executing template: %v", err)
			http.NotFound(w, r)
			return
		}

		duration := time.Now().Sub(start).Seconds()

		proto := strconv.Itoa(r.ProtoMajor)
		proto = fmt.Sprintf("%s.%s", proto, strconv.Itoa(r.ProtoMinor))

		requestCount.WithLabelValues(proto).Inc()
		requestDuration.WithLabelValues(proto).Observe(duration)
	}
}