improving Godoc

This commit is contained in:
Julien Neuhart
2018-03-28 22:08:36 +02:00
parent d9a09dc795
commit 2f97a44bed
4 changed files with 45 additions and 16 deletions

View File

@@ -1,3 +1,4 @@
// Package context implements a solution for accessing and setting a request's context values.
package context package context
import ( import (
@@ -7,14 +8,20 @@ import (
"github.com/gulien/gotenberg/logger" "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 { 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 { func GetTransactionID(ctx context.Context) string {
v, ok := ctx.Value(transactionIDKey).(string) v, ok := ctx.Value(transactionIDCtxKey).(string)
if !ok { if !ok {
logger.Log.Warn("Unable to retrieve the transaction ID from request context") logger.Log.Warn("Unable to retrieve the transaction ID from request context")
return "" return ""
@@ -22,14 +29,20 @@ func GetTransactionID(ctx context.Context) string {
return v 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 { 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 { func GetContentType(ctx context.Context) string {
v, ok := ctx.Value(contentTypeKey).(string) v, ok := ctx.Value(contentTypeCtxKey).(string)
if !ok { if !ok {
logger.Log.Error("Unable to retrieve the content type from request context") logger.Log.Error("Unable to retrieve the content type from request context")
return "" return ""
@@ -37,14 +50,20 @@ func GetContentType(ctx context.Context) string {
return v 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 { 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 { func GetResultFilePath(ctx context.Context) string {
v, ok := ctx.Value(resultFilePathKey).(string) v, ok := ctx.Value(resultFilePathCtxKey).(string)
if !ok { if !ok {
logger.Log.Error("Unable to retrieve the result file path from request context") logger.Log.Error("Unable to retrieve the result file path from request context")
return "" return ""
@@ -52,14 +71,20 @@ func GetResultFilePath(ctx context.Context) string {
return v 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 { 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 { func GetConverter(ctx context.Context) converters.Converter {
v, ok := ctx.Value(converterKey).(converters.Converter) v, ok := ctx.Value(converterCtxKey).(converters.Converter)
if !ok { if !ok {
logger.Log.Warn("Unable to retrieve the converter from request context") logger.Log.Warn("Unable to retrieve the converter from request context")
return nil return nil

View File

@@ -66,7 +66,7 @@ type ConverterUnprocessableEntityError struct {
message string 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 { func (e *ConverterUnprocessableEntityError) Error() string {
return e.message return e.message
} }

View File

@@ -39,18 +39,21 @@ func SetLevel(logLevel string) {
} }
} }
// InfoR logs an information with a transaction field.
func InfoR(transactionID string, msg string) { func InfoR(transactionID string, msg string) {
Log.WithFields(logrus.Fields{ Log.WithFields(logrus.Fields{
"transaction": transactionID, "transaction": transactionID,
}).Info(msg) }).Info(msg)
} }
// WarnR logs a warning with a transaction field.
func WarnR(transactionID string, msg string) { func WarnR(transactionID string, msg string) {
Log.WithFields(logrus.Fields{ Log.WithFields(logrus.Fields{
"transaction": transactionID, "transaction": transactionID,
}).Warn(msg) }).Warn(msg)
} }
// ErrorR logs an error with transaction, err and code fields.
func ErrorR(transactionID string, err error, code int, msg string) { func ErrorR(transactionID string, err error, code int, msg string) {
Log.WithFields(logrus.Fields{ Log.WithFields(logrus.Fields{
"transaction": transactionID, "transaction": transactionID,

View File

@@ -32,6 +32,7 @@ func loggingHandler(next http.Handler) http.Handler {
transactionID := uuid.NewV4().String() transactionID := uuid.NewV4().String()
r = r.WithContext(context.WithTransactionID(r.Context(), transactionID)) r = r.WithContext(context.WithTransactionID(r.Context(), transactionID))
logger.InfoR(context.GetTransactionID(r.Context()), fmt.Sprintf("Hello %s", r.RemoteAddr)) logger.InfoR(context.GetTransactionID(r.Context()), fmt.Sprintf("Hello %s", r.RemoteAddr))
next.ServeHTTP(w, r) 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") logger.ErrorR(context.GetTransactionID(r.Context()), fmt.Errorf("%s", http.StatusText(http.StatusUnsupportedMediaType)), http.StatusUnsupportedMediaType, "No matching content type found")
return return
} }
r = r.WithContext(context.WithContentType(r.Context(), contentType)) r = r.WithContext(context.WithContentType(r.Context(), contentType))
next.ServeHTTP(w, r) 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") logger.ErrorR(context.GetTransactionID(r.Context()), err, http.StatusInternalServerError, "An error occured during conversion")
return return
} }
r = r.WithContext(context.WithResultFilePath(r.Context(), resultFilePath))
r = r.WithContext(context.WithConverter(r.Context(), c)) r = r.WithContext(context.WithConverter(r.Context(), c))
r = r.WithContext(context.WithResultFilePath(r.Context(), resultFilePath))
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }