aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/gin-gonic/gin/render
diff options
context:
space:
mode:
authorVidhu Kant Sharma <bokuwakanojogahoshii@yahoo.com>2020-10-19 11:05:36 +0530
committerVidhu Kant Sharma <bokuwakanojogahoshii@yahoo.com>2020-10-19 11:05:36 +0530
commit9c2ad91230d72fe6d661450cc78300ea223ae2bc (patch)
tree17bd774b3f972359dfbb46248f085bbf1c6dae98 /vendor/github.com/gin-gonic/gin/render
make it better
Diffstat (limited to 'vendor/github.com/gin-gonic/gin/render')
-rw-r--r--vendor/github.com/gin-gonic/gin/render/data.go20
-rw-r--r--vendor/github.com/gin-gonic/gin/render/html.go67
-rw-r--r--vendor/github.com/gin-gonic/gin/render/json.go41
-rw-r--r--vendor/github.com/gin-gonic/gin/render/redirect.go24
-rw-r--r--vendor/github.com/gin-gonic/gin/render/render.go30
-rw-r--r--vendor/github.com/gin-gonic/gin/render/text.go33
-rw-r--r--vendor/github.com/gin-gonic/gin/render/xml.go21
7 files changed, 236 insertions, 0 deletions
diff --git a/vendor/github.com/gin-gonic/gin/render/data.go b/vendor/github.com/gin-gonic/gin/render/data.go
new file mode 100644
index 0000000..efa75d5
--- /dev/null
+++ b/vendor/github.com/gin-gonic/gin/render/data.go
@@ -0,0 +1,20 @@
+// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package render
+
+import "net/http"
+
+type Data struct {
+ ContentType string
+ Data []byte
+}
+
+func (r Data) Render(w http.ResponseWriter) error {
+ if len(r.ContentType) > 0 {
+ w.Header()["Content-Type"] = []string{r.ContentType}
+ }
+ w.Write(r.Data)
+ return nil
+}
diff --git a/vendor/github.com/gin-gonic/gin/render/html.go b/vendor/github.com/gin-gonic/gin/render/html.go
new file mode 100644
index 0000000..01f6bf2
--- /dev/null
+++ b/vendor/github.com/gin-gonic/gin/render/html.go
@@ -0,0 +1,67 @@
+// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package render
+
+import (
+ "html/template"
+ "net/http"
+)
+
+type (
+ HTMLRender interface {
+ Instance(string, interface{}) Render
+ }
+
+ HTMLProduction struct {
+ Template *template.Template
+ }
+
+ HTMLDebug struct {
+ Files []string
+ Glob string
+ }
+
+ HTML struct {
+ Template *template.Template
+ Name string
+ Data interface{}
+ }
+)
+
+var htmlContentType = []string{"text/html; charset=utf-8"}
+
+func (r HTMLProduction) Instance(name string, data interface{}) Render {
+ return HTML{
+ Template: r.Template,
+ Name: name,
+ Data: data,
+ }
+}
+
+func (r HTMLDebug) Instance(name string, data interface{}) Render {
+ return HTML{
+ Template: r.loadTemplate(),
+ Name: name,
+ Data: data,
+ }
+}
+func (r HTMLDebug) loadTemplate() *template.Template {
+ if len(r.Files) > 0 {
+ return template.Must(template.ParseFiles(r.Files...))
+ }
+ if len(r.Glob) > 0 {
+ return template.Must(template.ParseGlob(r.Glob))
+ }
+ panic("the HTML debug render was created without files or glob pattern")
+}
+
+func (r HTML) Render(w http.ResponseWriter) error {
+ writeContentType(w, htmlContentType)
+ if len(r.Name) == 0 {
+ return r.Template.Execute(w, r.Data)
+ } else {
+ return r.Template.ExecuteTemplate(w, r.Name, r.Data)
+ }
+}
diff --git a/vendor/github.com/gin-gonic/gin/render/json.go b/vendor/github.com/gin-gonic/gin/render/json.go
new file mode 100644
index 0000000..32e6058
--- /dev/null
+++ b/vendor/github.com/gin-gonic/gin/render/json.go
@@ -0,0 +1,41 @@
+// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package render
+
+import (
+ "encoding/json"
+ "net/http"
+)
+
+type (
+ JSON struct {
+ Data interface{}
+ }
+
+ IndentedJSON struct {
+ Data interface{}
+ }
+)
+
+var jsonContentType = []string{"application/json; charset=utf-8"}
+
+func (r JSON) Render(w http.ResponseWriter) error {
+ return WriteJSON(w, r.Data)
+}
+
+func (r IndentedJSON) Render(w http.ResponseWriter) error {
+ writeContentType(w, jsonContentType)
+ jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
+ if err != nil {
+ return err
+ }
+ w.Write(jsonBytes)
+ return nil
+}
+
+func WriteJSON(w http.ResponseWriter, obj interface{}) error {
+ writeContentType(w, jsonContentType)
+ return json.NewEncoder(w).Encode(obj)
+}
diff --git a/vendor/github.com/gin-gonic/gin/render/redirect.go b/vendor/github.com/gin-gonic/gin/render/redirect.go
new file mode 100644
index 0000000..d64e4d7
--- /dev/null
+++ b/vendor/github.com/gin-gonic/gin/render/redirect.go
@@ -0,0 +1,24 @@
+// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package render
+
+import (
+ "fmt"
+ "net/http"
+)
+
+type Redirect struct {
+ Code int
+ Request *http.Request
+ Location string
+}
+
+func (r Redirect) Render(w http.ResponseWriter) error {
+ if r.Code < 300 || r.Code > 308 {
+ panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
+ }
+ http.Redirect(w, r.Request, r.Location, r.Code)
+ return nil
+}
diff --git a/vendor/github.com/gin-gonic/gin/render/render.go b/vendor/github.com/gin-gonic/gin/render/render.go
new file mode 100644
index 0000000..994fcd7
--- /dev/null
+++ b/vendor/github.com/gin-gonic/gin/render/render.go
@@ -0,0 +1,30 @@
+// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package render
+
+import "net/http"
+
+type Render interface {
+ Render(http.ResponseWriter) error
+}
+
+var (
+ _ Render = JSON{}
+ _ Render = IndentedJSON{}
+ _ Render = XML{}
+ _ Render = String{}
+ _ Render = Redirect{}
+ _ Render = Data{}
+ _ Render = HTML{}
+ _ HTMLRender = HTMLDebug{}
+ _ HTMLRender = HTMLProduction{}
+)
+
+func writeContentType(w http.ResponseWriter, value []string) {
+ header := w.Header()
+ if val := header["Content-Type"]; len(val) == 0 {
+ header["Content-Type"] = value
+ }
+}
diff --git a/vendor/github.com/gin-gonic/gin/render/text.go b/vendor/github.com/gin-gonic/gin/render/text.go
new file mode 100644
index 0000000..5a9e280
--- /dev/null
+++ b/vendor/github.com/gin-gonic/gin/render/text.go
@@ -0,0 +1,33 @@
+// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package render
+
+import (
+ "fmt"
+ "io"
+ "net/http"
+)
+
+type String struct {
+ Format string
+ Data []interface{}
+}
+
+var plainContentType = []string{"text/plain; charset=utf-8"}
+
+func (r String) Render(w http.ResponseWriter) error {
+ WriteString(w, r.Format, r.Data)
+ return nil
+}
+
+func WriteString(w http.ResponseWriter, format string, data []interface{}) {
+ writeContentType(w, plainContentType)
+
+ if len(data) > 0 {
+ fmt.Fprintf(w, format, data...)
+ } else {
+ io.WriteString(w, format)
+ }
+}
diff --git a/vendor/github.com/gin-gonic/gin/render/xml.go b/vendor/github.com/gin-gonic/gin/render/xml.go
new file mode 100644
index 0000000..be22e6f
--- /dev/null
+++ b/vendor/github.com/gin-gonic/gin/render/xml.go
@@ -0,0 +1,21 @@
+// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package render
+
+import (
+ "encoding/xml"
+ "net/http"
+)
+
+type XML struct {
+ Data interface{}
+}
+
+var xmlContentType = []string{"application/xml; charset=utf-8"}
+
+func (r XML) Render(w http.ResponseWriter) error {
+ writeContentType(w, xmlContentType)
+ return xml.NewEncoder(w).Encode(r.Data)
+}