Fixed base path (again again again) and rewrote .env

This commit is contained in:
2022-02-02 10:28:26 +01:00
parent 83931bd2a4
commit a189b7a378
9 changed files with 19 additions and 35 deletions

9
.env
View File

@@ -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/

View File

@@ -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)

View File

@@ -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),
})
}
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -1,42 +1,27 @@
package config
import (
"fmt"
"github.com/joho/godotenv"
"os"
"path"
)
var (
ArticleRoot 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) + "/"
}

View File

@@ -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) {

View File

@@ -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,

View File

@@ -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 {