From 2f97a44bedd7bb19f8085d164cf02077d83d4d1a Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Wed, 28 Mar 2018 22:08:36 +0200 Subject: [PATCH] improving Godoc --- context/context.go | 49 ++++++++++++++++++++++++++++---------- converters/converters.go | 2 +- logger/logger.go | 3 +++ middlewares/middlewares.go | 7 +++--- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/context/context.go b/context/context.go index ba8a54d1..f1d8dce5 100644 --- a/context/context.go +++ b/context/context.go @@ -1,3 +1,4 @@ +// Package context implements a solution for accessing and setting a request's context values. package context import ( @@ -7,14 +8,20 @@ import ( "github.com/gulien/gotenberg/logger" ) -const transactionIDKey = "TransactionID" +// transactionIDCtxKeyType is a basic type for transactionIDCtxKey. +type transactionIDCtxKeyType string +// transactionIDCtxKey is the transactionID accessing key. +const transactionIDCtxKey transactionIDCtxKeyType = "transactionID" + +// WithTransactionID populates a context ctx with a transaction ID v. func WithTransactionID(ctx context.Context, v string) context.Context { - return context.WithValue(ctx, transactionIDKey, v) + return context.WithValue(ctx, transactionIDCtxKey, v) } +// GetTransactionID returns the transaction ID from the context ctx. func GetTransactionID(ctx context.Context) string { - v, ok := ctx.Value(transactionIDKey).(string) + v, ok := ctx.Value(transactionIDCtxKey).(string) if !ok { logger.Log.Warn("Unable to retrieve the transaction ID from request context") return "" @@ -22,14 +29,20 @@ func GetTransactionID(ctx context.Context) string { return v } -const contentTypeKey = "ContentType" +// contentTypeCtxKeyType is a basic type for contentTypeCtxKey. +type contentTypeCtxKeyType string +// contentTypeCtxKey is the contentType accessing key. +const contentTypeCtxKey contentTypeCtxKeyType = "contentType" + +// WithContentType populates a context ctx with a content type v. func WithContentType(ctx context.Context, v string) context.Context { - return context.WithValue(ctx, contentTypeKey, v) + return context.WithValue(ctx, contentTypeCtxKey, v) } +// GetContentType returns the content type from the context ctx. func GetContentType(ctx context.Context) string { - v, ok := ctx.Value(contentTypeKey).(string) + v, ok := ctx.Value(contentTypeCtxKey).(string) if !ok { logger.Log.Error("Unable to retrieve the content type from request context") return "" @@ -37,14 +50,20 @@ func GetContentType(ctx context.Context) string { return v } -const resultFilePathKey = "ResultFilePath" +// resultFilePathCtxKeyType is a basic type for resultFilePathCtxKey. +type resultFilePathCtxKeyType string +// resultFilePathCtxKey is the resultFilePath accessing key. +const resultFilePathCtxKey resultFilePathCtxKeyType = "resultFilePath" + +// WithResultFilePath populates a context ctx with a result file path v. func WithResultFilePath(ctx context.Context, v string) context.Context { - return context.WithValue(ctx, resultFilePathKey, v) + return context.WithValue(ctx, resultFilePathCtxKey, v) } +// GetResultFilePath returns the result file path from the context ctx. func GetResultFilePath(ctx context.Context) string { - v, ok := ctx.Value(resultFilePathKey).(string) + v, ok := ctx.Value(resultFilePathCtxKey).(string) if !ok { logger.Log.Error("Unable to retrieve the result file path from request context") return "" @@ -52,14 +71,20 @@ func GetResultFilePath(ctx context.Context) string { return v } -const converterKey = "Converter" +// converterCtxKeyType is a basic type for converterCtxKey. +type converterCtxKeyType string +// converterCtxKey is the converter accessing key. +const converterCtxKey contentTypeCtxKeyType = "converter" + +// WithConverter populates a context ctx with a converter v. func WithConverter(ctx context.Context, v converters.Converter) context.Context { - return context.WithValue(ctx, converterKey, v) + return context.WithValue(ctx, converterCtxKey, v) } +// GetConverter returns the converter from the context ctx. func GetConverter(ctx context.Context) converters.Converter { - v, ok := ctx.Value(converterKey).(converters.Converter) + v, ok := ctx.Value(converterCtxKey).(converters.Converter) if !ok { logger.Log.Warn("Unable to retrieve the converter from request context") return nil diff --git a/converters/converters.go b/converters/converters.go index 4b84d878..90e60504 100644 --- a/converters/converters.go +++ b/converters/converters.go @@ -66,7 +66,7 @@ type ConverterUnprocessableEntityError struct { message string } -// Error is the implementation of the function Error from the error interface. +// Error is the implementation of the Error function from the error interface. func (e *ConverterUnprocessableEntityError) Error() string { return e.message } diff --git a/logger/logger.go b/logger/logger.go index 0e17d1cf..d70c23f2 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -39,18 +39,21 @@ func SetLevel(logLevel string) { } } +// InfoR logs an information with a transaction field. func InfoR(transactionID string, msg string) { Log.WithFields(logrus.Fields{ "transaction": transactionID, }).Info(msg) } +// WarnR logs a warning with a transaction field. func WarnR(transactionID string, msg string) { Log.WithFields(logrus.Fields{ "transaction": transactionID, }).Warn(msg) } +// ErrorR logs an error with transaction, err and code fields. func ErrorR(transactionID string, err error, code int, msg string) { Log.WithFields(logrus.Fields{ "transaction": transactionID, diff --git a/middlewares/middlewares.go b/middlewares/middlewares.go index 69e3cad8..908759eb 100644 --- a/middlewares/middlewares.go +++ b/middlewares/middlewares.go @@ -32,6 +32,7 @@ func loggingHandler(next http.Handler) http.Handler { transactionID := uuid.NewV4().String() r = r.WithContext(context.WithTransactionID(r.Context(), transactionID)) logger.InfoR(context.GetTransactionID(r.Context()), fmt.Sprintf("Hello %s", r.RemoteAddr)) + next.ServeHTTP(w, r) }) } @@ -59,8 +60,8 @@ func enforceContentTypeHandler(next http.Handler) http.Handler { logger.ErrorR(context.GetTransactionID(r.Context()), fmt.Errorf("%s", http.StatusText(http.StatusUnsupportedMediaType)), http.StatusUnsupportedMediaType, "No matching content type found") return } - r = r.WithContext(context.WithContentType(r.Context(), contentType)) + next.ServeHTTP(w, r) }) } @@ -93,9 +94,9 @@ func convertHandler(next http.Handler) http.Handler { logger.ErrorR(context.GetTransactionID(r.Context()), err, http.StatusInternalServerError, "An error occured during conversion") return } - - r = r.WithContext(context.WithResultFilePath(r.Context(), resultFilePath)) r = r.WithContext(context.WithConverter(r.Context(), c)) + r = r.WithContext(context.WithResultFilePath(r.Context(), resultFilePath)) + next.ServeHTTP(w, r) }) }