Created
March 15, 2017 14:12
-
-
Save aslakknutsen/1344971d509e0d98192c3c8641f8b555 to your computer and use it in GitHub Desktop.
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
// Show runs the show action. | |
func (c *WorkitemtypeController) Show(ctx *app.ShowWorkitemtypeContext) error { | |
var err error | |
var result *app.WorkItemTypeSingle | |
err = application.Transactional(c.db, func(appl application.Application) error { | |
result, err = appl.WorkItemTypes().Load(ctx.Context, ctx.WitID) | |
if err != nil { | |
return jsonapi.JSONErrorResponse(ctx, err) | |
} | |
return nil | |
}) | |
if err != nil { | |
return err | |
} | |
return conditional( | |
ctx, | |
workItemTypeMetdata(result, c.configuration), | |
func(ctx *app.ShowWorkitemtypeContext) error { return ctx.OK(result) }) | |
} | |
func workItemTypeMetdata(result *app.WorkItemTypeSingle, config cacheControlConfiguration) Metadata { | |
return Metadata{ | |
LastModified: GetWorkItemTypeLastModified(*result), | |
ETag: GenerateWorkItemTypeETag(*result), | |
MaxAge: config.GetCacheControlMaxAgeWorkItemTypes(), | |
} | |
} | |
type Metadata struct { | |
LastModified time.Time | |
ETag string | |
MaxAge string | |
} | |
type ConvertFunc func(ctx *app.ShowWorkitemtypeContext) error | |
func conditional(ctx *app.ShowWorkitemtypeContext, meta Metadata, convert ConvertFunc) error { | |
// check the "If-Modified-Since header against the last update timestamp" | |
// HTTP header does not include microseconds, so we need to ignore them in the "updated_at" record field. | |
lastModified := meta.LastModified | |
etag := meta.ETag | |
// return the work item type along with conditional query and caching headers | |
ctx.ResponseData.Header().Set(LastModified, lastModified.String()) | |
ctx.ResponseData.Header().Set(ETag, etag) | |
ctx.ResponseData.Header().Set(CacheControl, MaxAge+"="+meta.MaxAge) | |
if ctx.IfModifiedSince != nil && !ctx.IfModifiedSince.UTC().Before(lastModified) { | |
return ctx.NotModified() | |
} | |
// check the ETag | |
if ctx.IfNoneMatch != nil && *ctx.IfNoneMatch == etag { | |
return ctx.NotModified() | |
} | |
return convert(ctx) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment