Implemented subpath support
This commit is contained in:
2
.env
2
.env
@@ -1,9 +1,9 @@
|
|||||||
ARTICLE_ROOT=./articles/
|
ARTICLE_ROOT=./articles/
|
||||||
|
|
||||||
|
SERVER_URL=localhost:8080
|
||||||
SERVER_PORT=8080
|
SERVER_PORT=8080
|
||||||
|
|
||||||
PREFIX=
|
PREFIX=
|
||||||
|
|
||||||
DATABASE_FILE=database.sqlite3
|
DATABASE_FILE=database.sqlite3
|
||||||
ARTICLE_TEMPLATE=./frontend/html/article.gohtml
|
|
||||||
FRONTEND_DIR=./frontend/
|
FRONTEND_DIR=./frontend/
|
||||||
|
|||||||
@@ -3,18 +3,25 @@ package config
|
|||||||
import (
|
import (
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type pageBase struct {
|
||||||
|
BasePath string
|
||||||
|
}
|
||||||
|
|
||||||
|
var PageBase pageBase
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ArticleRoot string
|
ArticleRoot string
|
||||||
|
|
||||||
|
ServerURL string
|
||||||
ServerPort string
|
ServerPort string
|
||||||
|
|
||||||
Prefix string
|
Prefix string
|
||||||
|
|
||||||
DatabaseFile string
|
DatabaseFile string
|
||||||
ArticleTemplate string
|
FrontendDir string
|
||||||
FrontendDir string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -22,11 +29,15 @@ func init() {
|
|||||||
|
|
||||||
ArticleRoot = os.Getenv("ARTICLE_ROOT")
|
ArticleRoot = os.Getenv("ARTICLE_ROOT")
|
||||||
|
|
||||||
|
ServerURL = os.Getenv("SERVER_URL")
|
||||||
ServerPort = os.Getenv("SERVER_PORT")
|
ServerPort = os.Getenv("SERVER_PORT")
|
||||||
|
|
||||||
Prefix = os.Getenv("PREFIX")
|
Prefix = os.Getenv("PREFIX")
|
||||||
|
|
||||||
DatabaseFile = os.Getenv("DATABASE_FILE")
|
DatabaseFile = os.Getenv("DATABASE_FILE")
|
||||||
ArticleTemplate = os.Getenv("ARTICLE_TEMPLATE")
|
|
||||||
FrontendDir = os.Getenv("FRONTEND_DIR")
|
FrontendDir = os.Getenv("FRONTEND_DIR")
|
||||||
|
|
||||||
|
PageBase = pageBase{
|
||||||
|
BasePath: path.Join(ServerURL, Prefix),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"TheAdversary/server"
|
"TheAdversary/server"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
@@ -24,7 +25,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
database.SetGlobDB(db)
|
database.SetGlobDB(db)
|
||||||
|
|
||||||
if err := http.ListenAndServe(fmt.Sprintf(":%s", config.ServerPort), r); err != nil {
|
if err := http.ListenAndServe(fmt.Sprintf("%s:%s", config.ServerURL, config.ServerPort), r); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,9 +53,11 @@ func setupFrontend(r *mux.Router) {
|
|||||||
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")))
|
||||||
|
|
||||||
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path == "/" {
|
if r.URL.Path == "/" {
|
||||||
http.ServeFile(w, r, filepath.Join(config.FrontendDir, "html", "landingpage.html"))
|
landingpage.Execute(w, config.PageBase)
|
||||||
} else {
|
} else {
|
||||||
server.Error404(w, r)
|
server.Error404(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,16 @@ import (
|
|||||||
"TheAdversary/database"
|
"TheAdversary/database"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var tmpl = template.Must(template.ParseFiles(config.ArticleTemplate))
|
var tmpl = template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "article.gohtml")))
|
||||||
|
|
||||||
type tmplArticle struct {
|
type tmplArticle struct {
|
||||||
Title string
|
Title string
|
||||||
|
BasePath string
|
||||||
Summary string
|
Summary string
|
||||||
Image string
|
Image string
|
||||||
Authors []string
|
Authors []string
|
||||||
@@ -37,12 +39,13 @@ func Article(w http.ResponseWriter, r *http.Request) {
|
|||||||
database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags)
|
database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags)
|
||||||
|
|
||||||
ta := tmplArticle{
|
ta := tmplArticle{
|
||||||
Title: article.Title,
|
Title: article.Title,
|
||||||
Summary: article.Summary,
|
BasePath: config.PageBase.BasePath,
|
||||||
Image: article.Image,
|
Summary: article.Summary,
|
||||||
Authors: authors,
|
Image: article.Image,
|
||||||
Tags: tags,
|
Authors: authors,
|
||||||
Content: article.Html,
|
Tags: tags,
|
||||||
|
Content: article.Html,
|
||||||
}
|
}
|
||||||
if article.Modified > 0 {
|
if article.Modified > 0 {
|
||||||
ta.Date = time.Unix(article.Modified, 0).Format("Monday, 2. January 2006 | 15:04")
|
ta.Date = time.Unix(article.Modified, 0).Format("Monday, 2. January 2006 | 15:04")
|
||||||
|
|||||||
Reference in New Issue
Block a user