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 := mux.NewRouter()
|
||||||
r.StrictSlash(true)
|
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/login", api.Login).Methods(http.MethodPost)
|
||||||
|
|
||||||
r.HandleFunc("/api/authors", api.Authors).Methods(http.MethodGet)
|
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/article", api.Article).Methods(http.MethodPost, http.MethodPatch, http.MethodDelete)
|
||||||
|
|
||||||
r.HandleFunc("/api/assets", api.Assets).Methods(http.MethodGet, http.MethodPost, 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("/article/{article}", server.Article).Methods(http.MethodGet)
|
||||||
|
|
||||||
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -42,14 +59,4 @@ func main() {
|
|||||||
server.Error404(w, r)
|
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
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"TheAdversary/config"
|
||||||
"TheAdversary/database"
|
"TheAdversary/database"
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"net/http"
|
"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) {
|
func Article(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
|
||||||
articleName := mux.Vars(r)["article"]
|
articleName := mux.Vars(r)["article"]
|
||||||
var article string
|
var article database.Article
|
||||||
if err := database.GetDB().Table("article").Select("html").Where("name = ?", articleName).First(&article).Error; err != nil {
|
if database.GetDB().Table("article").Where("link = ?", articleName).First(&article).RowsAffected == 0 {
|
||||||
if err == sql.ErrNoRows {
|
Error404(w, r)
|
||||||
Error404(w, r)
|
} else if database.GetDB().Error != nil {
|
||||||
} else {
|
Error500(w, r)
|
||||||
Error500(w, r)
|
} else {
|
||||||
}
|
var authors, tags []string
|
||||||
return
|
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)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
ta := tmplArticle{
|
||||||
fmt.Fprint(w, article)
|
Title: article.Title,
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user