huge refactoring

This commit is contained in:
Julien Neuhart
2018-03-29 20:13:19 +02:00
parent 2f97a44bed
commit e847c86baa
24 changed files with 1039 additions and 783 deletions

View File

@@ -0,0 +1,51 @@
package context
import (
"context"
"net/http"
"github.com/gulien/gotenberg/app/handlers/converter"
ghttp "github.com/gulien/gotenberg/app/handlers/http"
)
type key uint32
const (
contentTypeKey key = iota
converterKey
)
func WithContentType(r *http.Request, contentType ghttp.ContentType) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, contentTypeKey, contentType)
r = r.WithContext(ctx)
return r
}
func GetContentType(r *http.Request) (ghttp.ContentType, error) {
ct, ok := r.Context().Value(contentTypeKey).(ghttp.ContentType)
if !ok {
return "", &contentTypeNotFoundError{}
}
return ct, nil
}
func WithConverter(r *http.Request, converter *converter.Converter) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, converterKey, converter)
r = r.WithContext(ctx)
return r
}
func GetConverter(r *http.Request) (*converter.Converter, error) {
c, ok := r.Context().Value(converterKey).(*converter.Converter)
if !ok {
return nil, &converterNotFoundError{}
}
return c, nil
}

View File

@@ -0,0 +1,13 @@
package context
type contentTypeNotFoundError struct{}
func (e *contentTypeNotFoundError) Error() string {
return "The 'Content-Type' was not found in request context"
}
type converterNotFoundError struct{}
func (e *converterNotFoundError) Error() string {
return "The converter was not found in request context"
}