Initial state of rendering

This commit is contained in:
Jan-Lukas Else 2020-07-31 15:48:01 +02:00
parent 9193c4f656
commit e761e20fdc
10 changed files with 116 additions and 20 deletions

View File

@ -9,6 +9,7 @@ type config struct {
server *configServer
db *configDb
cache *configCache
blog *configBlog
}
type configServer struct {
@ -25,10 +26,19 @@ type configCache struct {
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{
server: &configServer{},
db: &configDb{},
cache: &configCache{},
blog: &configBlog{},
}
func initConfig() error {
@ -61,6 +71,15 @@ func initConfig() error {
viper.SetDefault(cacheExpiration, 600)
appConfig.cache.expiration = viper.GetInt64(cacheExpiration)
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
}

View File

@ -72,5 +72,4 @@ func initEmoji() {
emojiMaxSize = len(k)
}
}
}

View File

@ -2,4 +2,10 @@ server:
logging: false
port: 8080
database:
file: data/db.sqlite
file: data/db.sqlite
cache:
enable: true
expiration: 600
blog:
lang: en
title: My blog

View File

@ -22,6 +22,9 @@ func main() {
}
}()
log.Println("Loaded database")
log.Println("Initializing template rendering")
initEmoji()
initRendering()
log.Println("Start server")
err = startServer()
if err != nil {

View File

@ -9,12 +9,12 @@ import (
var errPostNotFound = errors.New("post not found")
type post struct {
path string
content string
published string
updated string
parameters map[string]string
type Post struct {
Path string
Content string
Published string
Updated string
Parameters map[string]string
}
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)
return
}
htmlContent, err := renderMarkdown(post.content)
err = templates.ExecuteTemplate(w, templatePostName, post)
if err != nil {
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) {
queriedPost := &post{}
func getPost(context context.Context, path string) (*Post, error) {
queriedPost := &Post{}
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 {
return nil, errPostNotFound
} else if err != nil {
@ -52,17 +49,17 @@ func getPost(context context.Context, path string) (*post, error) {
return queriedPost, nil
}
func (p *post) fetchParameters(context context.Context) error {
rows, err := appDb.QueryContext(context, "select parameter, COALESCE(value, '') from post_parameters where path=?", p.path)
func (p *Post) fetchParameters(context context.Context) error {
rows, err := appDb.QueryContext(context, "select parameter, COALESCE(value, '') from post_parameters where path=?", p.Path)
if err != nil {
return err
}
p.parameters = make(map[string]string)
p.Parameters = make(map[string]string)
for rows.Next() {
var parameter string
var value string
_ = rows.Scan(&parameter, &value)
p.parameters[parameter] = value
p.Parameters[parameter] = value
}
return nil
}

View File

@ -18,7 +18,12 @@ func serveRedirect(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
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) {

37
render.go Normal file
View File

@ -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)
}
}

9
templates/base.gohtml Normal file
View File

@ -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 }}

14
templates/post.gohtml Normal file
View File

@ -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 }}

View File

@ -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 }}"/>