mirror of https://github.com/jlelse/GoBlog
Initial state of rendering
This commit is contained in:
parent
9193c4f656
commit
e761e20fdc
19
config.go
19
config.go
|
@ -9,6 +9,7 @@ type config struct {
|
||||||
server *configServer
|
server *configServer
|
||||||
db *configDb
|
db *configDb
|
||||||
cache *configCache
|
cache *configCache
|
||||||
|
blog *configBlog
|
||||||
}
|
}
|
||||||
|
|
||||||
type configServer struct {
|
type configServer struct {
|
||||||
|
@ -25,10 +26,19 @@ type configCache struct {
|
||||||
expiration int64
|
expiration int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// exposed to templates via function "blog"
|
||||||
|
type configBlog struct {
|
||||||
|
// Language of the blog, e.g. "en" or "de"
|
||||||
|
Lang string
|
||||||
|
// Title of the blog, e.g. "My blog"
|
||||||
|
Title string
|
||||||
|
}
|
||||||
|
|
||||||
var appConfig = &config{
|
var appConfig = &config{
|
||||||
server: &configServer{},
|
server: &configServer{},
|
||||||
db: &configDb{},
|
db: &configDb{},
|
||||||
cache: &configCache{},
|
cache: &configCache{},
|
||||||
|
blog: &configBlog{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() error {
|
func initConfig() error {
|
||||||
|
@ -61,6 +71,15 @@ func initConfig() error {
|
||||||
viper.SetDefault(cacheExpiration, 600)
|
viper.SetDefault(cacheExpiration, 600)
|
||||||
appConfig.cache.expiration = viper.GetInt64(cacheExpiration)
|
appConfig.cache.expiration = viper.GetInt64(cacheExpiration)
|
||||||
logConfig(cacheExpiration, appConfig.cache.expiration)
|
logConfig(cacheExpiration, appConfig.cache.expiration)
|
||||||
|
// Blog meta
|
||||||
|
blogLang := "blog.lang"
|
||||||
|
viper.SetDefault(blogLang, "en")
|
||||||
|
appConfig.blog.Lang = viper.GetString(blogLang)
|
||||||
|
logConfig(blogLang, appConfig.blog.Lang)
|
||||||
|
blogTitle := "blog.title"
|
||||||
|
viper.SetDefault(blogTitle, "My blog")
|
||||||
|
appConfig.blog.Title = viper.GetString(blogTitle)
|
||||||
|
logConfig(blogTitle, appConfig.blog.Title)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
emoji.go
1
emoji.go
|
@ -72,5 +72,4 @@ func initEmoji() {
|
||||||
emojiMaxSize = len(k)
|
emojiMaxSize = len(k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,4 +2,10 @@ server:
|
||||||
logging: false
|
logging: false
|
||||||
port: 8080
|
port: 8080
|
||||||
database:
|
database:
|
||||||
file: data/db.sqlite
|
file: data/db.sqlite
|
||||||
|
cache:
|
||||||
|
enable: true
|
||||||
|
expiration: 600
|
||||||
|
blog:
|
||||||
|
lang: en
|
||||||
|
title: My blog
|
3
main.go
3
main.go
|
@ -22,6 +22,9 @@ func main() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
log.Println("Loaded database")
|
log.Println("Loaded database")
|
||||||
|
log.Println("Initializing template rendering")
|
||||||
|
initEmoji()
|
||||||
|
initRendering()
|
||||||
log.Println("Start server")
|
log.Println("Start server")
|
||||||
err = startServer()
|
err = startServer()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
31
posts.go
31
posts.go
|
@ -9,12 +9,12 @@ import (
|
||||||
|
|
||||||
var errPostNotFound = errors.New("post not found")
|
var errPostNotFound = errors.New("post not found")
|
||||||
|
|
||||||
type post struct {
|
type Post struct {
|
||||||
path string
|
Path string
|
||||||
content string
|
Content string
|
||||||
published string
|
Published string
|
||||||
updated string
|
Updated string
|
||||||
parameters map[string]string
|
Parameters map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func servePost(w http.ResponseWriter, r *http.Request) {
|
func servePost(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -27,19 +27,16 @@ func servePost(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
htmlContent, err := renderMarkdown(post.content)
|
err = templates.ExecuteTemplate(w, templatePostName, post)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "text/html")
|
|
||||||
_, _ = w.Write(htmlContent)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPost(context context.Context, path string) (*post, error) {
|
func getPost(context context.Context, path string) (*Post, error) {
|
||||||
queriedPost := &post{}
|
queriedPost := &Post{}
|
||||||
row := appDb.QueryRowContext(context, "select path, COALESCE(content, ''), COALESCE(published, ''), COALESCE(updated, '') from posts where path=?", path)
|
row := appDb.QueryRowContext(context, "select path, COALESCE(content, ''), COALESCE(published, ''), COALESCE(updated, '') from posts where path=?", path)
|
||||||
err := row.Scan(&queriedPost.path, &queriedPost.content, &queriedPost.published, &queriedPost.updated)
|
err := row.Scan(&queriedPost.Path, &queriedPost.Content, &queriedPost.Published, &queriedPost.Updated)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, errPostNotFound
|
return nil, errPostNotFound
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -52,17 +49,17 @@ func getPost(context context.Context, path string) (*post, error) {
|
||||||
return queriedPost, nil
|
return queriedPost, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *post) fetchParameters(context context.Context) error {
|
func (p *Post) fetchParameters(context context.Context) error {
|
||||||
rows, err := appDb.QueryContext(context, "select parameter, COALESCE(value, '') from post_parameters where path=?", p.path)
|
rows, err := appDb.QueryContext(context, "select parameter, COALESCE(value, '') from post_parameters where path=?", p.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.parameters = make(map[string]string)
|
p.Parameters = make(map[string]string)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var parameter string
|
var parameter string
|
||||||
var value string
|
var value string
|
||||||
_ = rows.Scan(¶meter, &value)
|
_ = rows.Scan(¶meter, &value)
|
||||||
p.parameters[parameter] = value
|
p.Parameters[parameter] = value
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,12 @@ func serveRedirect(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, redirect, http.StatusFound)
|
w.WriteHeader(http.StatusFound)
|
||||||
|
_ = templates.ExecuteTemplate(w, templateRedirectName, struct {
|
||||||
|
Permalink string
|
||||||
|
}{
|
||||||
|
Permalink: redirect,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRedirect(context context.Context, fromPath string) (string, error) {
|
func getRedirect(context context.Context, fromPath string) (string, error) {
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const templatePostName = "post.gohtml"
|
||||||
|
const templateRedirectName = "redirect.gohtml"
|
||||||
|
|
||||||
|
var templates *template.Template
|
||||||
|
|
||||||
|
func initRendering() {
|
||||||
|
templateFunctions := template.FuncMap{
|
||||||
|
"blog": func() *configBlog {
|
||||||
|
return appConfig.blog
|
||||||
|
},
|
||||||
|
"md": func(content string) template.HTML {
|
||||||
|
htmlContent, err := renderMarkdown(content)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return template.HTML(htmlContent)
|
||||||
|
},
|
||||||
|
"title": func(post Post) string {
|
||||||
|
return post.Parameters["title"]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
templates, err = template.New("templates").Funcs(templateFunctions).ParseGlob("templates/*.gohtml")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
{{ define "base" }}
|
||||||
|
<!doctype html>
|
||||||
|
<html lang={{ blog.Lang }}>
|
||||||
|
<meta charset=utf-8>
|
||||||
|
<meta name=viewport content="width=device-width,initial-scale=1">
|
||||||
|
<meta http-equiv=x-ua-compatible content="IE=edge">
|
||||||
|
{{ template "title" . }}
|
||||||
|
{{ template "main" . }}
|
||||||
|
{{ end }}
|
|
@ -0,0 +1,14 @@
|
||||||
|
{{ template "base" . }}
|
||||||
|
|
||||||
|
{{ define "title" }}
|
||||||
|
<title>{{ with title . }}{{ . }} - {{end}}{{ blog.Title }}</title>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "main" }}
|
||||||
|
<main class=h-entry>
|
||||||
|
<article>
|
||||||
|
{{ with title . }}<h1 class=p-name>{{ . }}</h1>{{ end }}
|
||||||
|
<div class=e-content>{{ md .Content }}</div>
|
||||||
|
</article>
|
||||||
|
</main>
|
||||||
|
{{ end }}
|
|
@ -0,0 +1,7 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang={{ blog.Lang }}>
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<meta name="robots" content="noindex">
|
||||||
|
<title>{{ .Permalink }}</title>
|
||||||
|
<link rel="canonical" href="{{ .Permalink }}"/>
|
||||||
|
<meta http-equiv="refresh" content="0; url={{ .Permalink }}"/>
|
Loading…
Reference in New Issue