forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance improvements when rendering
- Fast path for JSON, XML and plain text rendering
- Loading branch information
1 parent
eb3e929
commit 2d8f0a4
Showing
10 changed files
with
294 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package render | ||
|
||
import "net/http" | ||
|
||
type dataRender struct{} | ||
|
||
func (_ dataRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||
contentType := data[0].(string) | ||
bytes := data[1].([]byte) | ||
WriteData(w, code, contentType, bytes) | ||
return nil | ||
} | ||
|
||
func WriteData(w http.ResponseWriter, code int, contentType string, data []byte) { | ||
if len(contentType) > 0 { | ||
w.Header().Set("Content-Type", contentType) | ||
} | ||
w.WriteHeader(code) | ||
w.Write(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package render | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
) | ||
|
||
type ( | ||
HTMLRender struct { | ||
Template *template.Template | ||
} | ||
|
||
htmlPlainRender struct{} | ||
|
||
HTMLDebugRender struct { | ||
Files []string | ||
Glob string | ||
} | ||
) | ||
|
||
func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||
WriteHeader(w, code, "text/html") | ||
file := data[0].(string) | ||
args := data[1] | ||
return html.Template.ExecuteTemplate(w, file, args) | ||
} | ||
|
||
func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||
WriteHeader(w, code, "text/html") | ||
file := data[0].(string) | ||
obj := data[1] | ||
|
||
if t, err := r.loadTemplate(); err == nil { | ||
return t.ExecuteTemplate(w, file, obj) | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
func (r *HTMLDebugRender) loadTemplate() (*template.Template, error) { | ||
if len(r.Files) > 0 { | ||
return template.ParseFiles(r.Files...) | ||
} | ||
if len(r.Glob) > 0 { | ||
return template.ParseGlob(r.Glob) | ||
} | ||
return nil, errors.New("the HTML debug render was created without files or glob pattern") | ||
} | ||
|
||
func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||
format := data[0].(string) | ||
values := data[1].([]interface{}) | ||
WriteHTMLString(w, code, format, values) | ||
return nil | ||
} | ||
|
||
func WriteHTMLString(w http.ResponseWriter, code int, format string, values []interface{}) { | ||
WriteHeader(w, code, "text/html") | ||
if len(values) > 0 { | ||
fmt.Fprintf(w, format, values...) | ||
} else { | ||
w.Write([]byte(format)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package render | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type ( | ||
jsonRender struct{} | ||
|
||
indentedJSON struct{} | ||
) | ||
|
||
func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||
return WriteJSON(w, code, data[0]) | ||
} | ||
|
||
func (_ indentedJSON) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||
WriteHeader(w, code, "application/json") | ||
jsonData, err := json.MarshalIndent(data[0], "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = w.Write(jsonData) | ||
return err | ||
} | ||
|
||
func WriteJSON(w http.ResponseWriter, code int, data interface{}) error { | ||
WriteHeader(w, code, "application/json") | ||
return json.NewEncoder(w).Encode(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package render | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type redirectRender struct{} | ||
|
||
func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { | ||
req := data[0].(*http.Request) | ||
location := data[1].(string) | ||
WriteRedirect(w, code, req, location) | ||
return nil | ||
} | ||
|
||
func WriteRedirect(w http.ResponseWriter, code int, req *http.Request, location string) { | ||
if code < 300 || code > 308 { | ||
panic(fmt.Sprintf("Cannot redirect with status code %d", code)) | ||
} | ||
http.Redirect(w, req, location, code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.