From cfe33f517ba40ead97b629aa9f69dc445fbcfdfd Mon Sep 17 00:00:00 2001 From: ByteDream Date: Wed, 2 Feb 2022 10:10:47 +0100 Subject: [PATCH] Fixed base path (again) --- .env | 1 + config/config.go | 4 +++- main.go | 6 +----- server/article.go | 7 +------ 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/.env b/.env index b050bf0..3438d40 100644 --- a/.env +++ b/.env @@ -2,6 +2,7 @@ ARTICLE_ROOT=./articles/ SERVER_ADDRESS=localhost SERVER_PORT=8080 +PROTOCOL=http PREFIX= diff --git a/config/config.go b/config/config.go index f42ac0f..5191e6c 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ var ( ServerAddress string ServerPort string + Protocol string Prefix string @@ -30,11 +31,12 @@ func init() { ServerAddress = os.Getenv("SERVER_ADDRESS") ServerPort = os.Getenv("SERVER_PORT") + Protocol = os.Getenv("PROTOCOL") Prefix = path.Join(os.Getenv("PREFIX")) DatabaseFile = os.Getenv("DATABASE_FILE") FrontendDir = os.Getenv("FRONTEND_DIR") - BasePath = path.Join(fmt.Sprintf("%s:%s", ServerAddress, ServerPort), Prefix) + "/" + BasePath = Protocol + path.Join(fmt.Sprintf("%s:%s", ServerAddress, ServerPort), Prefix) + "/" } diff --git a/main.go b/main.go index 228dee7..fbb20f7 100644 --- a/main.go +++ b/main.go @@ -67,13 +67,9 @@ func setupFrontend(r *mux.Router) { landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml"))) r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - basePath := r.URL.Scheme + "://" + config.BasePath - if r.URL.Scheme == "" { - basePath = "http" + basePath - } landingpage.Execute(w, struct { BasePath string - }{BasePath: basePath}) + }{BasePath: config.BasePath}) }) r.NotFoundHandler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/server/article.go b/server/article.go index 5398ad7..a550221 100644 --- a/server/article.go +++ b/server/article.go @@ -38,14 +38,9 @@ func Article(w http.ResponseWriter, r *http.Request) { database.GetDB().Table("author").Select("id").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", article.Id)).Find(&authors) database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags) - basePath := r.URL.Scheme + "://" + config.BasePath - if r.URL.Scheme == "" { - basePath = "http" + basePath - } - ta := tmplArticle{ Title: article.Title, - BasePath: basePath, + BasePath: config.Protocol, Summary: article.Summary, Image: article.Image, Authors: authors,