Skip to content

Commit

Permalink
#52, #53 apply minifier, cache-control to prod env profile
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jun 2, 2017
1 parent fb0123e commit f20bc2c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
2 changes: 2 additions & 0 deletions aah.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
appSSLKey string
appIsSSLEnabled bool
appIsLetsEncrypt bool
appIsProfileProd bool
appMultipartMaxMemory int64
appPID int
appInitialized bool
Expand Down Expand Up @@ -161,6 +162,7 @@ func SetAppProfile(profile string) error {
}

appProfile = profile
appIsProfileProd = appProfile == "prod"
return nil
}

Expand Down
23 changes: 4 additions & 19 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,9 @@ func (e *engine) writeReply(ctx *Context) {
// resolving view template
e.resolveView(ctx)

// Render and detect the errors earlier, framework can write error info
// without messing with response.
// HTTP Body
reply.body = e.getBuffer()
if reply.Rdr != nil {
if jsonp, ok := reply.Rdr.(*JSON); ok && ctx.Req.IsJSONP() && jsonp.IsJSONP {
if ess.IsStrEmpty(jsonp.Callback) {
jsonp.Callback = ctx.Req.QueryValue("callback")
}
}

if err := reply.Rdr.Render(reply.body); err != nil {
log.Error("Render response body error: ", err)
reply.InternalServerError()
reply.body.Reset()
reply.body.WriteString("500 Internal Server Error\n")
}
}
// Render it and detect the errors earlier. So that framework can write
// error info without messing with response on the wire.
e.doRender(ctx)

// Gzip
if !isNoGzipStatusCode(reply.Code) && reply.body.Len() != 0 {
Expand All @@ -299,7 +284,7 @@ func (e *engine) writeReply(ctx *Context) {
ctx.Res.WriteHeader(reply.Code)

// Write response buffer on the wire
if minifier == nil ||
if minifier == nil || !appIsProfileProd ||
isNoGzipStatusCode(reply.Code) ||
!ahttp.ContentTypeHTML.IsEqual(reply.ContType) {
_, _ = reply.body.WriteTo(ctx.Res)
Expand Down
22 changes: 22 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"

"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
)

type (
Expand Down Expand Up @@ -193,3 +194,24 @@ func (h *HTML) Render(w io.Writer) error {

return h.Template.ExecuteTemplate(w, h.Layout, h.ViewArgs)
}

// doRender method renders and detects the errors earlier. Writes the
// error info if any.
func (e *engine) doRender(ctx *Context) {
if ctx.Reply().Rdr != nil {
reply := ctx.Reply()
reply.body = e.getBuffer()
if jsonp, ok := reply.Rdr.(*JSON); ok && jsonp.IsJSONP {
if ess.IsStrEmpty(jsonp.Callback) {
jsonp.Callback = ctx.Req.QueryValue("callback")
}
}

if err := reply.Rdr.Render(reply.body); err != nil {
log.Error("Render response body error: ", err)
reply.InternalServerError()
reply.body.Reset()
reply.body.WriteString("500 Internal Server Error\n")
}
}
}
8 changes: 6 additions & 2 deletions static.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ func (e *engine) serveStatic(ctx *Context) error {

// Serve file
if fi.Mode().IsRegular() {
// Write `Cache-Control` header based on `cache.static.*`
// `Cache-Control` header based on `cache.static.*`
if contentType, err := detectStaticContentType(filePath, f); err == nil {
ctx.Res.Header().Set(ahttp.HeaderContentType, contentType)
ctx.Res.Header().Set(ahttp.HeaderCacheControl, cacheHeader(contentType))

// apply cache header if environment profile is `prod`
if appIsProfileProd {
ctx.Res.Header().Set(ahttp.HeaderCacheControl, cacheHeader(contentType))
}
}

// 'OnPreReply' server extension point
Expand Down
2 changes: 2 additions & 0 deletions static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
func TestStaticDirectoryListing(t *testing.T) {
appCfg, _ := config.ParseString("")
e := newEngine(appCfg)
appIsProfileProd = true

testStaticServe(t, e, "http://localhost:8080/static/css/aah\x00.css", "static", "css/aah\x00.css", "", "500 Internal Server Error", false)

Expand All @@ -34,6 +35,7 @@ func TestStaticDirectoryListing(t *testing.T) {
testStaticServe(t, e, "http://localhost:8080/static/", "static", "", "", `<title>Listing of /static/</title>`, true)

testStaticServe(t, e, "http://localhost:8080/robots.txt", "", "", "test.txt", "This is file content of test.txt", false)
appIsProfileProd = false
}

func TestStaticMisc(t *testing.T) {
Expand Down

0 comments on commit f20bc2c

Please sign in to comment.