Added backend
This commit is contained in:
61
backend/server/article.go
Normal file
61
backend/server/article.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"TheAdversary/config"
|
||||
"TheAdversary/database"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
var tmpl = template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "article.gohtml")))
|
||||
|
||||
type tmplArticle struct {
|
||||
Title string
|
||||
BasePath string
|
||||
Summary string
|
||||
Image string
|
||||
Authors []string
|
||||
Tags []string
|
||||
Date string
|
||||
Modified bool
|
||||
Content string
|
||||
}
|
||||
|
||||
func Article(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
|
||||
articleName := mux.Vars(r)["article"]
|
||||
var article database.Article
|
||||
if database.GetDB().Table("article").Where("link = ?", articleName).First(&article).RowsAffected == 0 {
|
||||
Error404(w, r)
|
||||
} else if database.GetDB().Error != nil {
|
||||
Error500(w, r)
|
||||
} else {
|
||||
var authors, tags []string
|
||||
database.GetDB().Table("author").Select("name").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", article.Id)).Find(&authors)
|
||||
database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags)
|
||||
|
||||
ta := tmplArticle{
|
||||
Title: article.Title,
|
||||
BasePath: config.Address + path.Join("/", config.SubPath) + "/",
|
||||
Summary: article.Summary,
|
||||
Image: article.Image,
|
||||
Authors: authors,
|
||||
Tags: tags,
|
||||
Content: article.Html,
|
||||
}
|
||||
if article.Modified > 0 {
|
||||
ta.Date = time.Unix(article.Modified, 0).Format("Monday, 2. January 2006 | 15:04")
|
||||
ta.Modified = true
|
||||
} else {
|
||||
ta.Date = time.Unix(article.Created, 0).Format("Monday, 2. January 2006 | 15:04")
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
tmpl.Execute(w, ta)
|
||||
}
|
||||
}
|
||||
23
backend/server/assets.go
Normal file
23
backend/server/assets.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"github.com/gorilla/mux"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
)
|
||||
|
||||
func Assets(w http.ResponseWriter, r *http.Request) {
|
||||
assetName := mux.Vars(r)["asset"]
|
||||
var buf []interface{}
|
||||
database.GetDB().Table("assets").Select("data").Find(&buf, "link = ?", assetName)
|
||||
|
||||
if buf == nil {
|
||||
Error404(w, r)
|
||||
} else {
|
||||
data := buf[0].([]byte)
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(assetName)))
|
||||
w.Write(data)
|
||||
}
|
||||
}
|
||||
25
backend/server/error.go
Normal file
25
backend/server/error.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"TheAdversary/config"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// disable default log output because http.ServeFile prints
|
||||
// a message if a header is written 2 times or more
|
||||
log.Default().SetOutput(io.Discard)
|
||||
}
|
||||
|
||||
func Error404(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
http.ServeFile(w, r, filepath.Join(config.FrontendDir, "error", "404.html"))
|
||||
}
|
||||
|
||||
func Error500(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
http.ServeFile(w, r, filepath.Join(config.FrontendDir, "error", "500.html"))
|
||||
}
|
||||
1
backend/server/landingpage.go
Normal file
1
backend/server/landingpage.go
Normal file
@@ -0,0 +1 @@
|
||||
package server
|
||||
18
backend/server/path.go
Normal file
18
backend/server/path.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"TheAdversary/config"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ServePath(w http.ResponseWriter, r *http.Request) {
|
||||
path := filepath.Join(config.FrontendDir, strings.TrimPrefix(r.URL.Path, config.SubPath))
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
Error404(w, r)
|
||||
} else {
|
||||
http.ServeFile(w, r, path)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user