custom robots / favicon / css

This commit is contained in:
Lars Hoogestraat 2018-12-13 14:32:52 +01:00
parent 72e3075311
commit 4ba261718d
5 changed files with 109 additions and 14 deletions

View File

@ -32,6 +32,17 @@ application_description =
# form: https://sub.domain.tld
application_domain =
# file to a custom favicon
application_favicon =
# file to a custom robots.txt
application_robots_txt =
# file to a custom css
application_custom_css =
# overwrite the default css, so it will not be included
application_overwrite_default_css = false
# sqlite config
sqlite_file =

View File

@ -89,6 +89,12 @@ func FuncMap(ss models.SiteService, cfg *settings.Settings) template.FuncMap {
"ApplicationURL": func() string {
return cfg.Application.Domain
},
"CustomCSS": func() string {
return cfg.Application.CustomCSS
},
"OverwriteCSS": func() bool {
return cfg.Application.OverwriteCSS
},
"KeepAliveInterval": func() int64 {
return (cfg.Session.TTL.Nanoseconds() / 1e9) - 5
},

View File

@ -42,6 +42,7 @@ func InitRoutes(ctx *m.AppContext, cfg *settings.Settings) *mux.Router {
}
publicRoutes(ctx, sr, chain)
ar := router.PathPrefix("/admin").Subrouter()
restrictedChain := chain.Append(csrf).Append(ctx.AuthHandler)
@ -49,7 +50,22 @@ func InitRoutes(ctx *m.AppContext, cfg *settings.Settings) *mux.Router {
restrictedRoutes(ctx, ar, restrictedChain)
router.NotFoundHandler = chain.Then(useTemplateHandler(ctx, m.NotFound))
router.HandleFunc("/favicon.ico", faviconHandler)
router.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, cfg.Application.Favicon)
})
if len(cfg.Application.RobotsTxt) > 0 {
router.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, cfg.Application.RobotsTxt)
})
}
if len(cfg.Application.CustomCSS) > 0 {
router.HandleFunc("/assets/css/custom.css", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, cfg.Application.CustomCSS)
})
}
http.Handle("/", router)
@ -59,10 +75,6 @@ func InitRoutes(ctx *m.AppContext, cfg *settings.Settings) *mux.Router {
return router
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "assets/favicon.ico")
}
func stdOutLoggingHandler(h http.Handler) http.Handler {
return handlers.CombinedLoggingHandler(os.Stdout, h)
}

View File

@ -88,10 +88,14 @@ type Server struct {
}
type Application struct {
Title string `cfg:"application_title"`
Language string `cfg:"application_language"`
Description string `cfg:"application_description"`
Domain string `cfg:"application_domain"`
Title string `cfg:"application_title"`
Language string `cfg:"application_language"`
Description string `cfg:"application_description"`
Domain string `cfg:"application_domain"`
Favicon string `cfg:"application_favicon" default:"assets/favicon.ico"`
RobotsTxt string `cfg:"application_robots_txt"`
CustomCSS string `cfg:"application_custom_css"`
OverwriteCSS bool `cfg:"application_overwrite_default_css" default:"false"`
}
type Database struct {
@ -182,20 +186,74 @@ func (cfg *Settings) CheckConfig() error {
//check log file is rw in production mode
if cfg.Environment != "dev" {
if _, err := os.OpenFile(cfg.Log.File, os.O_RDONLY|os.O_CREATE, 0644); err != nil {
return fmt.Errorf("config: could not open log file %s error %v", cfg.Log.File, err)
return fmt.Errorf("config 'log_file': could not open log file %s error %v", cfg.Log.File, err)
}
if _, err := os.OpenFile(cfg.Log.AccessFile, os.O_RDONLY|os.O_CREATE, 0644); err != nil {
return fmt.Errorf("config: could not open access log file %s error %v", cfg.Log.AccessFile, err)
return fmt.Errorf("config 'log_access_file': could not open access log file %s error %v", cfg.Log.AccessFile, err)
}
}
if len(cfg.Application.Domain) == 0 {
return errors.New("config: please specify a domain name 'application_domain'")
return errors.New("config 'application_domain': please specify a domain name")
}
_, err := url.ParseRequestURI(cfg.Application.Domain)
if err != nil {
return fmt.Errorf("config: invalid url setting for key 'application_domain' value '%s'", cfg.Application.Domain)
return fmt.Errorf("config 'application_domain': invalid url setting for key 'application_domain' value '%s'", cfg.Application.Domain)
}
if len(cfg.Application.CustomCSS) > 0 {
f, err := os.Open(cfg.Application.CustomCSS)
if err != nil {
return fmt.Errorf("config 'application_custom_css': could not open specified custom css file %s error %v", cfg.Application.CustomCSS, err)
}
fi, err := f.Stat()
if err != nil {
return fmt.Errorf("config 'application_custom_css': could not open specified custom css file %s error %v", cfg.Application.CustomCSS, err)
}
if fi.IsDir() {
return fmt.Errorf("config 'application_custom_css': the custom css file '%s' is a directory", cfg.Application.CustomCSS)
}
}
if len(cfg.Application.RobotsTxt) > 0 {
f, err := os.Open(cfg.Application.RobotsTxt)
if err != nil {
return fmt.Errorf("config 'application_robots_txt': could not open specified robots txt file %s error %v", cfg.Application.RobotsTxt, err)
}
fi, err := f.Stat()
if err != nil {
return fmt.Errorf("config 'application_robots_txt': could not open specified custom css file %s error %v", cfg.Application.CustomCSS, err)
}
if fi.IsDir() {
return fmt.Errorf("config 'application_robots_txt': the robots txt file '%s' is a directory", cfg.Application.CustomCSS)
}
}
if len(cfg.Application.Favicon) > 0 {
f, err := os.Open(cfg.Application.Favicon)
if err != nil {
return fmt.Errorf("config 'application_favicon': could not open specified favicon file %s error %v", cfg.Application.Favicon, err)
}
fi, err := f.Stat()
if err != nil {
return fmt.Errorf("config 'application_favicon': could not open specified custom css file %s error %v", cfg.Application.Favicon, err)
}
if fi.IsDir() {
return fmt.Errorf("config 'application_favicon': the favicon file '%s' is a directory", cfg.Application.CuFaviconstomCSS)
}
}
//server settings

View File

@ -6,7 +6,15 @@
<meta charset="UTF-8">
<title>{{PageTitle}} {{if .actual}} | {{.actual}} {{end}}{{if $.site}}{{if $.site.Link}} | {{$.site.Link}}{{end}}{{end}}</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="/assets/css/master.css">
{{if not OverwriteCSS}}
<link rel="stylesheet" href="/assets/css/master.css">
{{end}}
{{if CustomCSS}}
<link rel="stylesheet" href="/assets/css/custom.css">
{{end}}
<link rel="icon" href="/favicon.ico" type="image/vnd.microsoft.icon">
{{GetMetadata .}}