godoc for http package + improving godoc for config and handlers packages

This commit is contained in:
Julien Neuhart
2018-03-30 17:41:34 +02:00
parent 700f88b14e
commit 6206452e39
3 changed files with 21 additions and 14 deletions

View File

@@ -162,8 +162,6 @@ var levels = map[string]logrus.Level{
"PANIC": logrus.PanicLevel, "PANIC": logrus.PanicLevel,
} }
// wrongLoggingLevelError is raised when the logging level defined by the user
// is not applicable.
type wrongLoggingLevelError struct{} type wrongLoggingLevelError struct{}
func (e *wrongLoggingLevelError) Error() string { func (e *wrongLoggingLevelError) Error() string {
@@ -172,7 +170,7 @@ func (e *wrongLoggingLevelError) Error() string {
// getLoggingLevelFromFileConfig returns a logrus level if a matching was found // getLoggingLevelFromFileConfig returns a logrus level if a matching was found
// with the one defined by the user. // with the one defined by the user.
// If no match, throws a wrongLoggingLevelError. // If no match, throws an error.
func getLoggingLevelFromFileConfig(c *fileConfig) (logrus.Level, error) { func getLoggingLevelFromFileConfig(c *fileConfig) (logrus.Level, error) {
l, ok := levels[c.Logs.Level] l, ok := levels[c.Logs.Level]
if !ok { if !ok {
@@ -189,8 +187,6 @@ var formatters = map[string]logrus.Formatter{
"json": &logrus.JSONFormatter{}, "json": &logrus.JSONFormatter{},
} }
// wrongLoggingFormatError is raised when the logging format defined by the user
// is not applicable.
type wrongLoggingFormatError struct{} type wrongLoggingFormatError struct{}
func (e *wrongLoggingFormatError) Error() string { func (e *wrongLoggingFormatError) Error() string {
@@ -199,7 +195,7 @@ func (e *wrongLoggingFormatError) Error() string {
// getLoggingLevelFromFileConfig returns a logrus Formatter if a matching was found // getLoggingLevelFromFileConfig returns a logrus Formatter if a matching was found
// with the format defined by the user. // with the format defined by the user.
// If no match, throws a wrongLoggingFormatError. // If no match, throws an error.
func getLoggingFormatterFromFileConfig(c *fileConfig) (logrus.Formatter, error) { func getLoggingFormatterFromFileConfig(c *fileConfig) (logrus.Formatter, error) {
f, ok := formatters[c.Logs.Format] f, ok := formatters[c.Logs.Format]
if !ok { if !ok {

View File

@@ -21,8 +21,6 @@ func GetHandlersChain() http.Handler {
return alice.New(enforceContentLengthHandler, enforceContentTypeHandler, convertHandler, serveHandler).ThenFunc(clearHandler) return alice.New(enforceContentLengthHandler, enforceContentTypeHandler, convertHandler, serveHandler).ThenFunc(clearHandler)
} }
// requestHasNoContentError is raised when the request
// content length is 0.
type requestHasNoContentError struct{} type requestHasNoContentError struct{}
func (e *requestHasNoContentError) Error() string { func (e *requestHasNoContentError) Error() string {

View File

@@ -1,3 +1,4 @@
// Package http provides functions for detecting a request or a file content type.
package http package http
import ( import (
@@ -7,22 +8,30 @@ import (
"strings" "strings"
) )
// ContentType is a string which represents a content type.
type ContentType string type ContentType string
const ( const (
PDFContentType ContentType = "application/pdf" // PDFContentType represents... the PDF content type.
HTMLContentType ContentType = "text/html" PDFContentType ContentType = "application/pdf"
OctetStreamContentType ContentType = "application/octet-stream" // HTMLContentType represents... the HTML content type.
ZipContentType ContentType = "application/zip" HTMLContentType ContentType = "text/html"
// OctetStreamContentType represents... the octet stream content type.
OctetStreamContentType ContentType = "application/octet-stream"
// ZipContentType represents... the zip content type.
ZipContentType ContentType = "application/zip"
// MultipartFormDataContentType represents... the multipart form data content type.
MultipartFormDataContentType ContentType = "multipart/form-data" MultipartFormDataContentType ContentType = "multipart/form-data"
) )
type notAuthorizedContentTypeError struct{} type notAuthorizedContentTypeError struct{}
func (e *notAuthorizedContentTypeError) Error() string { func (e *notAuthorizedContentTypeError) Error() string {
return fmt.Sprintf("Accepted values for 'Content-Type': %s, %s, %s, %s", HTMLContentType, OctetStreamContentType, MultipartFormDataContentType) return fmt.Sprintf("Accepted values for 'Content-Type': %s, %s, %s", HTMLContentType, OctetStreamContentType, MultipartFormDataContentType)
} }
// FindAuthorizedContentType tries to return a content type according to a request header.
// If no authorized content type found, throws an error.
func FindAuthorizedContentType(h http.Header) (ContentType, error) { func FindAuthorizedContentType(h http.Header) (ContentType, error) {
ct := findContentType(h.Get("Content-Type"), HTMLContentType, OctetStreamContentType, MultipartFormDataContentType) ct := findContentType(h.Get("Content-Type"), HTMLContentType, OctetStreamContentType, MultipartFormDataContentType)
if ct == "" { if ct == "" {
@@ -35,9 +44,11 @@ func FindAuthorizedContentType(h http.Header) (ContentType, error) {
type notAuthorizedFileContentTypeError struct{} type notAuthorizedFileContentTypeError struct{}
func (e *notAuthorizedFileContentTypeError) Error() string { func (e *notAuthorizedFileContentTypeError) Error() string {
return fmt.Sprintf("Unable to detect a file 'Content-Type'") return fmt.Sprintf("Unable to detect a file content type")
} }
// SniffContentType tries to detect the content type of a file.
// If no authorized content type found, throws an error.
func SniffContentType(f *os.File) (ContentType, error) { func SniffContentType(f *os.File) (ContentType, error) {
// only the first 512 bytes are used to sniff the content type. // only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512) buffer := make([]byte, 512)
@@ -58,6 +69,8 @@ func SniffContentType(f *os.File) (ContentType, error) {
return ct, nil return ct, nil
} }
// findContentType parses a string representing a content type and tries to find
// one of the given content types.
func findContentType(contentType string, contentTypes ...ContentType) ContentType { func findContentType(contentType string, contentTypes ...ContentType) ContentType {
for _, ct := range contentTypes { for _, ct := range contentTypes {
if i := strings.IndexRune(contentType, ';'); i != -1 { if i := strings.IndexRune(contentType, ';'); i != -1 {