mirror of
https://github.com/asciimoo/wuzz
synced 2024-11-10 13:10:29 +00:00
29 lines
746 B
Go
29 lines
746 B
Go
package formatter
|
|
|
|
import (
|
|
"io"
|
|
"mime"
|
|
"strings"
|
|
|
|
"github.com/asciimoo/wuzz/config"
|
|
)
|
|
|
|
type ResponseFormatter interface {
|
|
Format(writer io.Writer, data []byte) error
|
|
Title() string
|
|
Searchable() bool
|
|
}
|
|
|
|
func New(appConfig *config.Config, contentType string) ResponseFormatter {
|
|
ctype, _, err := mime.ParseMediaType(contentType)
|
|
if err == nil && appConfig.General.FormatJSON && (ctype == config.ContentTypes["json"] || strings.HasSuffix(ctype, "+json")) {
|
|
return &jsonFormatter{}
|
|
} else if strings.Contains(contentType, "text/html") {
|
|
return &htmlFormatter{}
|
|
} else if strings.Index(contentType, "text") == -1 && strings.Index(contentType, "application") == -1 {
|
|
return &binaryFormatter{}
|
|
} else {
|
|
return &textFormatter{}
|
|
}
|
|
}
|