handlers

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddXRequestIdHandler = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		xRequestIdRequest := r.Header.Get(constants.X_REQUEST_ID)
		if xRequestIdRequest != "" {
			w.Header().Add(constants.X_REQUEST_ID, xRequestIdRequest)
		} else {
			uniqueId := uuid.New().String()
			w.Header().Add(constants.X_REQUEST_ID, uniqueId)
			r.Header.Add(constants.X_REQUEST_ID, uniqueId)
		}

		next.ServeHTTP(w, r)
	})
}
View Source
var LogHttpRequestHandler = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		request := logging.NewMessageDumper().DumpRequest(r)
		fluentdLogger := logging.GetFluentdInstance()
		result, _ := json.Marshal(fluentdLogger.Emit(constants.NAME+"."+"api",
			request, "DEBUG"))
		log.Println(string(result))

		next.ServeHTTP(w, r)
	})
}
View Source
var TokenAuthenticationHandler = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		tokenHeader := r.Header.Get("Token")
		matchedUrl, _ := regexp.MatchString(`.*swaggerui.*`, r.URL.RequestURI())

		if (tokenHeader == environment.GetInstance().GetConfigEnvVars()[constants.HTTP_AUTH_TOKEN]) || matchedUrl {

			next.ServeHTTP(w, r)
		} else {
			resp := u.ApiMessage(uint32(constants.UNAUTHORIZED),
				u.GetMessage()[uint32(constants.UNAUTHORIZED)],
				"Invalid Token",
				r.URL.Path)
			w.Header().Add("Content-Type", "application/json")
			w.WriteHeader(http.StatusUnauthorized)
			u.ApiResponseError(w, resp)
		}

		LogHttpResponse(w)
	})
}

Functions

func LogHttpResponse

func LogHttpResponse(w http.ResponseWriter)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL