Finished subpath implementation

This commit is contained in:
2022-01-31 10:20:19 +01:00
parent 6ac80f8647
commit bc4c27cff3
3 changed files with 5 additions and 8 deletions

View File

@@ -30,12 +30,12 @@ func init() {
ServerAddress = os.Getenv("SERVER_ADDRESS") ServerAddress = os.Getenv("SERVER_ADDRESS")
Prefix = os.Getenv("PREFIX") Prefix = path.Join(os.Getenv("PREFIX"))
DatabaseFile = os.Getenv("DATABASE_FILE") DatabaseFile = os.Getenv("DATABASE_FILE")
FrontendDir = os.Getenv("FRONTEND_DIR") FrontendDir = os.Getenv("FRONTEND_DIR")
PageBase = pageBase{ PageBase = pageBase{
BasePath: "http://" + path.Join(ServerAddress, Prefix), BasePath: "http://" + path.Join(ServerAddress, Prefix) + "/",
} }
} }

View File

@@ -11,7 +11,6 @@ import (
"net/http" "net/http"
"path" "path"
"path/filepath" "path/filepath"
"strings"
) )
func main() { func main() {
@@ -56,16 +55,13 @@ func setupApi(r *mux.Router) {
func setupFrontend(r *mux.Router) { func setupFrontend(r *mux.Router) {
r.HandleFunc("/article/{article}", server.Article).Methods(http.MethodGet) r.HandleFunc("/article/{article}", server.Article).Methods(http.MethodGet)
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(config.FrontendDir, "img", "logodark.svg"))
})
r.PathPrefix("/sass/").HandlerFunc(server.ServePath) r.PathPrefix("/sass/").HandlerFunc(server.ServePath)
r.PathPrefix("/img/").HandlerFunc(server.ServePath) r.PathPrefix("/img/").HandlerFunc(server.ServePath)
landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml"))) landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml")))
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.TrimPrefix(r.URL.Path, path.Dir(config.Prefix)) == "/" { if path.Join(config.Prefix)+"/" == r.URL.Path {
landingpage.Execute(w, config.PageBase) landingpage.Execute(w, config.PageBase)
} else { } else {
server.Error404(w, r) server.Error404(w, r)

View File

@@ -5,10 +5,11 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
func ServePath(w http.ResponseWriter, r *http.Request) { func ServePath(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(config.FrontendDir, r.URL.Path) path := filepath.Join(config.FrontendDir, strings.TrimPrefix(r.URL.Path, config.Prefix))
if _, err := os.Stat(path); os.IsNotExist(err) { if _, err := os.Stat(path); os.IsNotExist(err) {
Error404(w, r) Error404(w, r)
} else { } else {