github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/app.go (about) 1 // Copyright 2017-present Kirill Danshin and Gramework contributors 2 // Copyright 2019-present Highload LTD (UK CN: 11893420) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 11 package gramework 12 13 import ( 14 "time" 15 16 "github.com/microcosm-cc/bluemonday" 17 ) 18 19 // SetName for the server 20 // Deprecated: Use New() with OptAppName option instead 21 func (app *App) SetName(name string) { 22 if len(name) > 0 { 23 app.name = name 24 } else { 25 app.name = DefaultAppName 26 } 27 app.serverBase.Name = app.name 28 } 29 30 // SetCookieExpire allows you set cookie expire time 31 func (app *App) SetCookieExpire(d time.Duration) { 32 if d != 0 { 33 app.cookieExpire = d 34 } 35 } 36 37 // SetCookiePath allows you set cookie path 38 func (app *App) SetCookiePath(path string) { 39 app.cookiePath = path 40 } 41 42 // SetCookieDomain allows you to implement SSO and other useful features 43 // without additional pain 44 func (app *App) SetCookieDomain(domain string) { 45 app.cookieDomain = domain 46 } 47 48 // SetSanitizerPolicy updates app's sanitizer policy to a new one, if newPolicy is not nil 49 func (app *App) SetSanitizerPolicy(newPolicy *bluemonday.Policy) { 50 if newPolicy != nil { 51 app.sanitizerPolicy = newPolicy 52 } 53 } 54 55 // ToTLSHandler returns handler that redirects user to HTTPS scheme 56 func (app *App) ToTLSHandler() func(*Context) { 57 return func(ctx *Context) { 58 ctx.ToTLS() 59 } 60 } 61 62 // HTTP returns HTTP-only router 63 func (app *App) HTTP() *Router { 64 return app.defaultRouter.HTTP() 65 } 66 67 // HTTPS returns HTTPS-only router 68 func (app *App) HTTPS() *Router { 69 return app.defaultRouter.HTTPS() 70 } 71 72 // MethodNotAllowed sets MethodNotAllowed handler 73 func (app *App) MethodNotAllowed(c func(ctx *Context)) *App { 74 app.defaultRouter.router.MethodNotAllowed = c 75 return app 76 } 77 78 // Redir sends 301 redirect to the given url 79 // 80 // it's equivalent to 81 // 82 // ctx.Redirect(url, 301) 83 func (app *App) Redir(url string) func(*Context) { 84 return func(ctx *Context) { 85 ctx.Redirect(url, redirectCode) 86 } 87 } 88 89 // Forbidden send 403 Forbidden error 90 func (app *App) Forbidden(ctx *Context) { 91 ctx.Forbidden() 92 }