mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 00:22:14 +01:00
improving Godoc
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user