Finished subpath implementation
This commit is contained in:
@@ -30,12 +30,12 @@ func init() {
|
||||
|
||||
ServerAddress = os.Getenv("SERVER_ADDRESS")
|
||||
|
||||
Prefix = os.Getenv("PREFIX")
|
||||
Prefix = path.Join(os.Getenv("PREFIX"))
|
||||
|
||||
DatabaseFile = os.Getenv("DATABASE_FILE")
|
||||
FrontendDir = os.Getenv("FRONTEND_DIR")
|
||||
|
||||
PageBase = pageBase{
|
||||
BasePath: "http://" + path.Join(ServerAddress, Prefix),
|
||||
BasePath: "http://" + path.Join(ServerAddress, Prefix) + "/",
|
||||
}
|
||||
}
|
||||
|
||||
6
main.go
6
main.go
@@ -11,7 +11,6 @@ import (
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -56,16 +55,13 @@ func setupApi(r *mux.Router) {
|
||||
func setupFrontend(r *mux.Router) {
|
||||
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("/img/").HandlerFunc(server.ServePath)
|
||||
|
||||
landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml")))
|
||||
|
||||
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)
|
||||
} else {
|
||||
server.Error404(w, r)
|
||||
|
||||
@@ -5,10 +5,11 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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) {
|
||||
Error404(w, r)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user