diff --git a/.env b/.env index 3438d40..4bcb8a9 100644 --- a/.env +++ b/.env @@ -1,10 +1,9 @@ -ARTICLE_ROOT=./articles/ - -SERVER_ADDRESS=localhost SERVER_PORT=8080 -PROTOCOL=http -PREFIX= +# the global address of your webserver (protocol://domain[:port]). make sure this DOES NOT has a trailing slash +ADDRESS=http://localhost:8080 +# the path you serve on. must be at least a slash +Path=/ DATABASE_FILE=database.sqlite3 FRONTEND_DIR=./frontend/ diff --git a/api/article.go b/api/article.go index 38827d1..415cc36 100644 --- a/api/article.go +++ b/api/article.go @@ -102,7 +102,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } - articleSummary.Link = "/" + path.Join(config.Prefix, "article", url.PathEscape(articleSummary.Link)) + articleSummary.Link = path.Join(config.Path, "article", url.PathEscape(articleSummary.Link)) w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(articleSummary) @@ -216,7 +216,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) { } else { articleSummary.Tags = []string{} } - articleSummary.Link = "/" + path.Join(config.Prefix, "article", url.PathEscape(articleSummary.Link)) + articleSummary.Link = path.Join(config.Path, "article", url.PathEscape(articleSummary.Link)) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(articleSummary) diff --git a/api/assets.go b/api/assets.go index e0f0cae..394551f 100644 --- a/api/assets.go +++ b/api/assets.go @@ -58,7 +58,7 @@ func assetsGet(w http.ResponseWriter, r *http.Request) { request.Find(&assets) for _, asset := range assets { - asset.Link = "/" + path.Join(config.Prefix, "assets", asset.Link) + asset.Link = path.Join(config.Path, "assets", asset.Link) } w.WriteHeader(http.StatusOK) @@ -101,7 +101,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(schema.Asset{ Id: tmpDatabaseSchema.Id, Name: tmpDatabaseSchema.Name, - Link: "/" + path.Join(config.Prefix, "assets", tmpDatabaseSchema.Link), + Link: path.Join(config.Path, "assets", tmpDatabaseSchema.Link), }) } } diff --git a/api/recent.go b/api/recent.go index ae0124c..5a7a063 100644 --- a/api/recent.go +++ b/api/recent.go @@ -35,7 +35,7 @@ func Recent(w http.ResponseWriter, r *http.Request) { database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) summary.Tags = []string{} database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) - summary.Link = "/" + path.Join(config.Prefix, "article", summary.Link) + summary.Link = path.Join(config.Path, "article", summary.Link) articleSummaries[i] = summary } diff --git a/api/search.go b/api/search.go index fb5ef9d..ba65135 100644 --- a/api/search.go +++ b/api/search.go @@ -72,7 +72,7 @@ func Search(w http.ResponseWriter, r *http.Request) { database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) summary.Tags = []string{} database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) - summary.Link = "/" + path.Join(config.Prefix, "article", summary.Link) + summary.Link = path.Join(config.Path, "article", summary.Link) articleSummaries[i] = summary } diff --git a/config/config.go b/config/config.go index 926e04c..6b006ca 100644 --- a/config/config.go +++ b/config/config.go @@ -1,42 +1,27 @@ package config import ( - "fmt" "github.com/joho/godotenv" "os" - "path" ) var ( - ArticleRoot string + ServerPort string - ServerAddress string - ServerPort string - Protocol string - - Prefix string + Address string + Path string DatabaseFile string FrontendDir string - - // not specified in config - - BasePath string ) func init() { godotenv.Load() - ArticleRoot = os.Getenv("ARTICLE_ROOT") - - ServerAddress = os.Getenv("SERVER_ADDRESS") ServerPort = os.Getenv("SERVER_PORT") - Protocol = os.Getenv("PROTOCOL") - Prefix = path.Join(os.Getenv("PREFIX")) + Address = os.Getenv("ADDRESS") DatabaseFile = os.Getenv("DATABASE_FILE") FrontendDir = os.Getenv("FRONTEND_DIR") - - BasePath = Protocol + "://" + path.Join(fmt.Sprintf("%s:%s", ServerAddress, ServerPort), Prefix) + "/" } diff --git a/main.go b/main.go index fbb20f7..be54004 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,8 @@ func main() { r.StrictSlash(true) var subrouter *mux.Router - if config.Prefix != "" { - subrouter = r.PathPrefix(config.Prefix).Subrouter() + if config.Path != "/" { + subrouter = r.PathPrefix(config.Path).Subrouter() } else { subrouter = r } @@ -69,7 +69,7 @@ func setupFrontend(r *mux.Router) { r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { landingpage.Execute(w, struct { BasePath string - }{BasePath: config.BasePath}) + }{BasePath: config.Address + config.Path}) }) r.NotFoundHandler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/server/article.go b/server/article.go index a550221..12754a1 100644 --- a/server/article.go +++ b/server/article.go @@ -40,7 +40,7 @@ func Article(w http.ResponseWriter, r *http.Request) { ta := tmplArticle{ Title: article.Title, - BasePath: config.Protocol, + BasePath: config.Address + config.Path, Summary: article.Summary, Image: article.Image, Authors: authors, diff --git a/server/path.go b/server/path.go index cb2eb27..9b70807 100644 --- a/server/path.go +++ b/server/path.go @@ -9,7 +9,7 @@ import ( ) func ServePath(w http.ResponseWriter, r *http.Request) { - path := filepath.Join(config.FrontendDir, strings.TrimPrefix(r.URL.Path, config.Prefix)) + path := filepath.Join(config.FrontendDir, strings.TrimPrefix(r.URL.Path, config.Path)) if _, err := os.Stat(path); os.IsNotExist(err) { Error404(w, r) } else {