Updated article loader and added test entry to database
This commit is contained in:
BIN
database.sqlite3
BIN
database.sqlite3
Binary file not shown.
13
database/schema.go
Normal file
13
database/schema.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package database
|
||||
|
||||
type Article struct {
|
||||
Id int `gorm:"primaryKey"`
|
||||
Title string
|
||||
Summary string
|
||||
Image string
|
||||
Created int64
|
||||
Modified int64
|
||||
Link string
|
||||
Markdown string
|
||||
Html string
|
||||
}
|
||||
27
main.go
27
main.go
@@ -15,6 +15,21 @@ func main() {
|
||||
r := mux.NewRouter()
|
||||
r.StrictSlash(true)
|
||||
|
||||
setupApi(r)
|
||||
setupFrontend(r)
|
||||
|
||||
db, err := database.NewSqlite3Connection(config.DatabaseFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
database.SetGlobDB(db)
|
||||
|
||||
if err := http.ListenAndServe(fmt.Sprintf(":%s", config.ServerPort), r); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupApi(r *mux.Router) {
|
||||
r.HandleFunc("/api/login", api.Login).Methods(http.MethodPost)
|
||||
|
||||
r.HandleFunc("/api/authors", api.Authors).Methods(http.MethodGet)
|
||||
@@ -26,7 +41,9 @@ func main() {
|
||||
r.HandleFunc("/api/article", api.Article).Methods(http.MethodPost, http.MethodPatch, http.MethodDelete)
|
||||
|
||||
r.HandleFunc("/api/assets", api.Assets).Methods(http.MethodGet, http.MethodPost, http.MethodDelete)
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -42,14 +59,4 @@ func main() {
|
||||
server.Error404(w, r)
|
||||
}
|
||||
})
|
||||
|
||||
db, err := database.NewSqlite3Connection(config.DatabaseFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
database.SetGlobDB(db)
|
||||
|
||||
if err := http.ListenAndServe(fmt.Sprintf(":%s", config.ServerPort), r); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,57 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"TheAdversary/config"
|
||||
"TheAdversary/database"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
// var tmpl = template.Must(template.ParseFiles(config.ArticleTemplate))
|
||||
var tmpl = template.Must(template.ParseFiles(config.ArticleTemplate))
|
||||
|
||||
type tmplArticle struct {
|
||||
Title 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 string
|
||||
if err := database.GetDB().Table("article").Select("html").Where("name = ?", articleName).First(&article).Error; err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
var article database.Article
|
||||
if database.GetDB().Table("article").Where("link = ?", articleName).First(&article).RowsAffected == 0 {
|
||||
Error404(w, r)
|
||||
} else {
|
||||
} else if database.GetDB().Error != nil {
|
||||
Error500(w, r)
|
||||
} else {
|
||||
var authors, tags []string
|
||||
database.GetDB().Table("author").Select("id").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,
|
||||
Summary: article.Summary,
|
||||
Image: article.Image,
|
||||
Authors: authors,
|
||||
Tags: tags,
|
||||
Content: article.Html,
|
||||
}
|
||||
return
|
||||
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)
|
||||
fmt.Fprint(w, article)
|
||||
tmpl.Execute(w, ta)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user