Skip to content

Commit 4704057

Browse files
committed
Fix lint and move some methods to base context from context
1 parent ba072ae commit 4704057

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

modules/context/base.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package context
77
import (
88
"net/http"
99
"net/url"
10+
"path"
1011
"strconv"
1112
"strings"
1213
"time"
@@ -18,12 +19,14 @@ import (
1819
chi "github.com/go-chi/chi/v5"
1920
)
2021

22+
// BaseContext represents a general context for some simple routes
2123
type BaseContext struct {
2224
Resp ResponseWriter
2325
Req *http.Request
2426
Data map[string]interface{}
2527
}
2628

29+
// NewBaseContext creates a new base context
2730
func NewBaseContext(resp http.ResponseWriter, req *http.Request, data map[string]interface{}) *BaseContext {
2831
return &BaseContext{
2932
Resp: NewResponse(resp),
@@ -144,6 +147,25 @@ func (ctx *BaseContext) QueryOptionalBool(key string, defaults ...util.OptionalB
144147
return (*Forms)(ctx.Req).MustOptionalBool(key, defaults...)
145148
}
146149

150+
// Error returned an error to web browser
151+
func (ctx *BaseContext) Error(status int, contents ...string) {
152+
var v = http.StatusText(status)
153+
if len(contents) > 0 {
154+
v = contents[0]
155+
}
156+
http.Error(ctx.Resp, v, status)
157+
}
158+
159+
// Redirect redirect the request
160+
func (ctx *BaseContext) Redirect(location string, status ...int) {
161+
code := http.StatusFound
162+
if len(status) == 1 {
163+
code = status[0]
164+
}
165+
166+
http.Redirect(ctx.Resp, ctx.Req, location, code)
167+
}
168+
147169
// JSON render content as JSON
148170
func (ctx *BaseContext) JSON(status int, content interface{}) {
149171
ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8")
@@ -163,3 +185,21 @@ func (ctx *BaseContext) PlainText(status int, bs []byte) {
163185
ctx.Status(500)
164186
}
165187
}
188+
189+
// ServeFile serves given file to response.
190+
func (ctx *BaseContext) ServeFile(file string, names ...string) {
191+
var name string
192+
if len(names) > 0 {
193+
name = names[0]
194+
} else {
195+
name = path.Base(file)
196+
}
197+
ctx.Resp.Header().Set("Content-Description", "File Transfer")
198+
ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
199+
ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+name)
200+
ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary")
201+
ctx.Resp.Header().Set("Expires", "0")
202+
ctx.Resp.Header().Set("Cache-Control", "must-revalidate")
203+
ctx.Resp.Header().Set("Pragma", "public")
204+
http.ServeFile(ctx.Resp, ctx.Req, file)
205+
}

modules/context/context.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interfa
317317
http.ServeContent(ctx.Resp, ctx.Req, name, modtime, r)
318318
}
319319

320-
// PlainText render content as plain text
320+
// PlainText render content as plain text, this will override basecontext's method
321+
// because we need a beautiful failed page
321322
func (ctx *Context) PlainText(status int, bs []byte) {
322323
ctx.Resp.WriteHeader(status)
323324
ctx.Resp.Header().Set("Content-Type", "text/plain;charset=utf-8")
@@ -326,24 +327,6 @@ func (ctx *Context) PlainText(status int, bs []byte) {
326327
}
327328
}
328329

329-
// ServeFile serves given file to response.
330-
func (ctx *Context) ServeFile(file string, names ...string) {
331-
var name string
332-
if len(names) > 0 {
333-
name = names[0]
334-
} else {
335-
name = path.Base(file)
336-
}
337-
ctx.Resp.Header().Set("Content-Description", "File Transfer")
338-
ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
339-
ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+name)
340-
ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary")
341-
ctx.Resp.Header().Set("Expires", "0")
342-
ctx.Resp.Header().Set("Cache-Control", "must-revalidate")
343-
ctx.Resp.Header().Set("Pragma", "public")
344-
http.ServeFile(ctx.Resp, ctx.Req, file)
345-
}
346-
347330
// ServeStream serves file via io stream
348331
func (ctx *Context) ServeStream(rd io.Reader, name string) {
349332
ctx.Resp.Header().Set("Content-Description", "File Transfer")
@@ -359,25 +342,6 @@ func (ctx *Context) ServeStream(rd io.Reader, name string) {
359342
}
360343
}
361344

362-
// Error returned an error to web browser
363-
func (ctx *Context) Error(status int, contents ...string) {
364-
var v = http.StatusText(status)
365-
if len(contents) > 0 {
366-
v = contents[0]
367-
}
368-
http.Error(ctx.Resp, v, status)
369-
}
370-
371-
// Redirect redirect the request
372-
func (ctx *Context) Redirect(location string, status ...int) {
373-
code := http.StatusFound
374-
if len(status) == 1 {
375-
code = status[0]
376-
}
377-
378-
http.Redirect(ctx.Resp, ctx.Req, location, code)
379-
}
380-
381345
// SetCookie convenience function to set most cookies consistently
382346
// CSRF and a few others are the exception here
383347
func (ctx *Context) SetCookie(name, value string, expiry int) {

0 commit comments

Comments
 (0)