Skip to content

Commit

Permalink
👔 up: complete the Auto response logic and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 7, 2024
1 parent b07109c commit c9d6d41
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Respond

respond `Text`, `HTML`, `XML`, `JSON`, `JSONP` data to http.ResponseWriter
[![GoDoc](https://pkg.go.dev/badge/github.com/gookit/respond.svg)](https://pkg.go.dev/github.com/gookit/respond)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/respond)](https://goreportcard.com/report/github.com/gookit/respond)
[![Unit-Tests](https://github.com/gookit/respond/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/respond/actions)

Quickly respond `Text`, `HTML`, `XML`, `JSON`, `JSONP` and more data to `http.ResponseWriter`.

## Godoc

- [doc on gowalker](https://gowalker.org/github.com/gookit/respond)
- [godoc for gopkg](https://godoc.org/gopkg.in/gookit/respond.v1)
- [godoc for github](https://godoc.org/github.com/gookit/respond)
- [godoc](https://pkg.go.dev/github.com/gookit/respond)

## Quick start

Expand All @@ -15,6 +17,7 @@ package main

import (
"net/http"

"github.com/gookit/respond"
)

Expand Down Expand Up @@ -54,6 +57,7 @@ package main

import (
"net/http"

"github.com/gookit/respond"
)

Expand Down
2 changes: 1 addition & 1 deletion respond.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func HTMLString(w http.ResponseWriter, status int, tplContent string, v any) err
return std.HTMLString(w, status, tplContent, v)
}

// HTMLText data response
// HTMLText output raw HTML contents response
func HTMLText(w http.ResponseWriter, status int, html string) error {
return std.HTMLText(w, status, html)
}
Expand Down
52 changes: 35 additions & 17 deletions responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/gookit/easytpl"
"github.com/gookit/goutil"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/goutil/netutil/httpreq"
)

Expand All @@ -30,9 +32,11 @@ type Options struct {
TplViewsDir string
TplSuffixes []string

// supported content types
// supported content types for response, you can change it.
ContentBinary, ContentHTML, ContentXML, ContentText, ContentJSON, ContentJSONP string

// ContentType default content type value. use for Responder.Auto() method
ContentType string
// Charset default content data charset
Charset string
// AddCharset on response content
Expand Down Expand Up @@ -119,6 +123,10 @@ func (r *Responder) Initialize() *Responder {

// append charset for all content types
func (r *Responder) appendCharset() {
if len(r.opts.Charset) == 0 {
return
}

r.opts.ContentBinary += "; " + r.opts.Charset
r.opts.ContentHTML += "; " + r.opts.Charset
r.opts.ContentXML += "; " + r.opts.Charset
Expand Down Expand Up @@ -172,11 +180,10 @@ func (r *Responder) HTMLString(w http.ResponseWriter, status int, tplContent str
return nil
}

// HTMLText response string as html/text
// HTMLText output raw HTML contents response(content-type=html/text)
func (r *Responder) HTMLText(w http.ResponseWriter, status int, html string) error {
w.Header().Set(ContentType, r.opts.ContentHTML)
w.WriteHeader(status)

_, err := w.Write([]byte(html))
return err
}
Expand All @@ -186,19 +193,26 @@ func (r *Responder) HTMLText(w http.ResponseWriter, status int, html string) err
*************************************************************/

// Auto response data by request accepted header
func (r *Responder) Auto(w http.ResponseWriter, req *http.Request, data any) error {
resContentType := w.Header().Get(ContentType)
if resContentType != "" {
// TODO: check the accepted header
}

accepted := req.Header.Get("Accepted")
if accepted == "" {
return nil
func (r *Responder) Auto(w http.ResponseWriter, req *http.Request, data any, tplName ...string) error {
resContentType := w.Header().Get(httpctype.Key)
outTypeList := []string{resContentType}

if resContentType == "" {
// check the request accepted header
accepted := req.Header.Get("Accepted")
if len(accepted) > 0 {
outTypeList = httpreq.ParseAccept(accepted)
} else {
// fallback to default content type
if len(r.opts.ContentType) == 0 {
return errorx.E("no content type for response")
}
outTypeList = []string{r.opts.ContentType}
}
}

ls := httpreq.ParseAccept(accepted)
for _, val := range ls {
var lastVal string
for _, val := range outTypeList {
if len(val) == 0 {
continue
}
Expand All @@ -209,15 +223,19 @@ func (r *Responder) Auto(w http.ResponseWriter, req *http.Request, data any) err
case "application/xml":
return r.XML(w, http.StatusOK, data)
case "text/html":
return r.HTML(w, http.StatusOK, "index", data)
if len(tplName) > 0 {
return r.HTML(w, http.StatusOK, tplName[0], data)
}
return errorx.Ef("no template name for HTML response")
case "text/plain":
return r.Text(w, http.StatusOK, goutil.String(data))
}

lastVal = val
break
}

// TODO default response text
return nil
return errorx.Ef("unsupported content type %q for response", lastVal)
}

// Empty alias method of the NoContent()
Expand Down

0 comments on commit c9d6d41

Please sign in to comment.