From 99ad76f76f75f2dbe963344ef8bf9b22764c1001 Mon Sep 17 00:00:00 2001 From: Atvaark Date: Sat, 3 Jun 2017 03:45:24 +0200 Subject: [PATCH] Add web address config (#888) * This allows changing the nyaa, sukebei and status URL via config. Previously only the nyaa address was configurable * This helps testing changes locally without having to set up a TLS terminating proxy * Also refactored uses of hardcoded URLs in the html templates The html templates will now also use the configured urls --- config/default_config.yml | 10 ++++++++-- config/types.go | 10 +++++++++- router/rss_handler.go | 4 ++-- router/template_functions.go | 1 + router/upload.go | 2 +- service/user/verification.go | 5 +++-- templates/FAQ.html | 6 +++--- templates/admin_index.html | 2 +- templates/index.html | 6 +++--- templates/view.html | 2 +- util/format.go | 10 ++++++++++ 11 files changed, 42 insertions(+), 16 deletions(-) diff --git a/config/default_config.yml b/config/default_config.yml index 6d2ae126..32fdbfd3 100644 --- a/config/default_config.yml +++ b/config/default_config.yml @@ -15,12 +15,18 @@ db_params: ./nyaa.db?cache_size=50 db_logmode: default # Environment should be one of: DEVELOPMENT, TEST, PRODUCTION environment: DEVELOPMENT -# WebAddress : url of the website -web_address: nyaa.pantsu.cat # AuthTokenExpirationDay : Number of Days for token expiration when logged in auth_token_expiration: 1000 # EnableSecureCSRF : Enable CSRF https mode : True if website support https, false otherwise (eg. testing locally: false) enable_secure_csrf: true +# the default config for web addresses +web_address: + # Nyaa : Origin of the website + nyaa: https://nyaa.pantsu.cat + # Sukebei : Origin of the sukebei website + sukebei: https://sukebei.pantsu.cat + # Status : Origin of the status website + status: https://status.pantsu.cat # the default config for session cookies cookies: # DomainName : The host domain so the cookies can be shared across subdomains diff --git a/config/types.go b/config/types.go index d5ae08ab..826ef961 100644 --- a/config/types.go +++ b/config/types.go @@ -6,7 +6,6 @@ type Config struct { Port int `json:"port" yaml:"port,omitempty"` DBType string `json:"db_type" yaml:"db_type,omitempty"` Environment string `json:"environment" yaml:"environment,omitempty"` - WebAddress string `json:"web_address" yaml:"web_address,omitempty"` AuthTokenExpirationDay int `json:"auth_token_expiration" yaml:"auth_token_expiration,omitempty"` EnableSecureCSRF bool `json:"enable_secure_csrf" yaml:"enable_secure_csrf,omitempty"` // DBParams will be directly passed to Gorm, and its internal @@ -15,6 +14,8 @@ type Config struct { DBLogMode string `json:"db_logmode" yaml:"db_logmode,omitempty"` Version string `json:"version" yaml:"version,omitempty"` Build string `yaml:"-"` + // web address config + WebAddress WebAddressConfig `yaml:"web_address,flow,omitempty"` // cookies config Cookies CookiesConfig `yaml:"cookies,flow,omitempty"` // tracker scraper config (required) @@ -43,6 +44,13 @@ type Config struct { Models ModelsConfig `yaml:"models,flow,omitempty"` } +// WebAddressConfig : Config struct for web addresses +type WebAddressConfig struct { + Nyaa string `yaml:"nyaa,omitempty"` + Sukebei string `yaml:"sukebei,omitempty"` + Status string `yaml:"status,omitempty"` +} + // CookiesConfig : Config struct for session cookies type CookiesConfig struct { DomainName string `yaml:"domain_name,omitempty"` diff --git a/router/rss_handler.go b/router/rss_handler.go index 94f6d143..088eb5bc 100644 --- a/router/rss_handler.go +++ b/router/rss_handler.go @@ -67,7 +67,7 @@ func RSSHandler(w http.ResponseWriter, r *http.Request) { } feed := &feeds.Feed{ Title: "Nyaa Pantsu", - Link: &feeds.Link{Href: "https://" + config.Conf.WebAddress + "/"}, + Link: &feeds.Link{Href: config.Conf.WebAddress.Nyaa + "/"}, Created: createdAsTime, } feed.Items = make([]*feeds.Item, len(torrents)) @@ -75,7 +75,7 @@ func RSSHandler(w http.ResponseWriter, r *http.Request) { for i, torrent := range torrents { torrentJSON := torrent.ToJSON() feed.Items[i] = &feeds.Item{ - ID: "https://" + config.Conf.WebAddress + "/view/" + strconv.FormatUint(uint64(torrentJSON.ID), 10), + ID: config.Conf.WebAddress.Nyaa + "/view/" + strconv.FormatUint(uint64(torrentJSON.ID), 10), Title: torrent.Name, Link: &feeds.Link{Href: string("")}, Description: string(torrentJSON.Description), diff --git a/router/template_functions.go b/router/template_functions.go index aa2a9ea5..6c2738a6 100644 --- a/router/template_functions.go +++ b/router/template_functions.go @@ -170,6 +170,7 @@ var FuncMap = template.FuncMap{ // because time.* isn't available in templates... return t.Format(time.RFC3339) }, + "GetHostname": util.GetHostname, "GetCategories": func(keepParent bool) map[string]string { return categories.GetCategoriesSelect(keepParent) }, diff --git a/router/upload.go b/router/upload.go index 32099a83..5d23d87a 100644 --- a/router/upload.go +++ b/router/upload.go @@ -70,7 +70,7 @@ var errTorrentPlusMagnet = errors.New("Upload either a torrent file or magnet li var errPrivateTorrent = errors.New("Torrent is private") // error indicating a problem with its trackers -var errTrackerProblem = errors.New("Torrent does not have any (working) trackers: https://" + config.Conf.WebAddress + "/faq#trackers") +var errTrackerProblem = errors.New("Torrent does not have any (working) trackers: " + config.Conf.WebAddress.Nyaa + "/faq#trackers") // error indicating a torrent's name is invalid var errInvalidTorrentName = errors.New("Torrent name is invalid") diff --git a/service/user/verification.go b/service/user/verification.go index 6867e08b..fb13ee5f 100644 --- a/service/user/verification.go +++ b/service/user/verification.go @@ -10,6 +10,7 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/db" "github.com/NyaaPantsu/nyaa/model" + "github.com/NyaaPantsu/nyaa/util" "github.com/NyaaPantsu/nyaa/util/email" "github.com/NyaaPantsu/nyaa/util/publicSettings" "github.com/NyaaPantsu/nyaa/util/timeHelper" @@ -24,8 +25,8 @@ func SendEmailVerification(to string, token string) error { if err != nil { return err } - content := T("link") + " : https://" + config.Conf.WebAddress + "/verify/email/" + token - contentHTML := T("verify_email_content") + "
" + "" + config.Conf.WebAddress + "/verify/email/" + token + "" + content := T("link") + " : " + config.Conf.WebAddress.Nyaa + "/verify/email/" + token + contentHTML := T("verify_email_content") + "
" + "" + util.GetHostname(config.Conf.WebAddress.Nyaa) + "/verify/email/" + token + "" return email.SendEmailFromAdmin(to, T("verify_email_title"), content, contentHTML) } diff --git a/templates/FAQ.html b/templates/FAQ.html index 66d12205..a544c63a 100644 --- a/templates/FAQ.html +++ b/templates/FAQ.html @@ -8,11 +8,11 @@

{{call $.T "links_replacement_mirror"}}

- Nyaa - nyaa.pantsu.cat
- Sukebei - sukebei.pantsu.cat + Nyaa - {{GetHostname .Config.WebAddress.Nyaa}}
+ Sukebei - {{GetHostname .Config.WebAddress.Sukebei}}

{{call $.T "server_status_link"}}

- https://status.pantsu.cat/
+ Status - {{GetHostname .Config.WebAddress.Status}}

{{call $.T "what_happened"}}