2020-07-28 19:17:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-29 15:17:48 +00:00
|
|
|
"context"
|
2020-07-28 19:17:07 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2020-07-31 19:44:16 +00:00
|
|
|
"strings"
|
2020-07-28 19:17:07 +00:00
|
|
|
)
|
|
|
|
|
2020-07-30 19:18:13 +00:00
|
|
|
var errPostNotFound = errors.New("post not found")
|
2020-07-28 19:17:07 +00:00
|
|
|
|
2020-07-31 13:48:01 +00:00
|
|
|
type Post struct {
|
2020-07-31 19:44:16 +00:00
|
|
|
Path string `json:"path"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Published string `json:"published"`
|
|
|
|
Updated string `json:"updated"`
|
|
|
|
Parameters map[string]string `json:"parameters"`
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:41:36 +00:00
|
|
|
func servePost(w http.ResponseWriter, r *http.Request) {
|
2020-07-30 19:18:13 +00:00
|
|
|
path := slashTrimmedPath(r)
|
|
|
|
post, err := getPost(r.Context(), path)
|
|
|
|
if err == errPostNotFound {
|
2020-07-31 19:02:47 +00:00
|
|
|
serve404(w, r)
|
2020-07-29 14:41:36 +00:00
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
} else if err != nil {
|
2020-07-29 14:41:36 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
2020-07-31 19:02:47 +00:00
|
|
|
render(w, templatePost, post)
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 13:48:01 +00:00
|
|
|
func getPost(context context.Context, path string) (*Post, error) {
|
2020-08-05 16:41:45 +00:00
|
|
|
posts, err := getPosts(context, path)
|
2020-07-29 16:28:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-05 16:41:45 +00:00
|
|
|
} else if len(posts) == 0 {
|
|
|
|
return nil, errPostNotFound
|
2020-07-29 16:28:51 +00:00
|
|
|
}
|
2020-08-05 16:41:45 +00:00
|
|
|
return posts[0], nil
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
2020-07-29 15:17:48 +00:00
|
|
|
|
2020-08-05 16:41:45 +00:00
|
|
|
func getAllPosts(context context.Context) (posts []*Post, err error) {
|
|
|
|
return getPosts(context, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPosts(context context.Context, path string) (posts []*Post, err error) {
|
|
|
|
paths := make(map[string]int)
|
|
|
|
var rows *sql.Rows
|
|
|
|
if path != "" {
|
|
|
|
rows, err = appDb.QueryContext(context, "select p.path, COALESCE(content, ''), COALESCE(published, ''), COALESCE(updated, ''), COALESCE(parameter, ''), COALESCE(value, '') from posts p left outer join post_parameters pp on p.path = pp.path where p.path=?", path)
|
|
|
|
} else {
|
|
|
|
rows, err = appDb.QueryContext(context, "select p.path, COALESCE(content, ''), COALESCE(published, ''), COALESCE(updated, ''), COALESCE(parameter, ''), COALESCE(value, '') from posts p left outer join post_parameters pp on p.path = pp.path")
|
|
|
|
}
|
2020-07-29 16:28:51 +00:00
|
|
|
if err != nil {
|
2020-08-05 16:41:45 +00:00
|
|
|
return nil, err
|
2020-07-29 16:28:51 +00:00
|
|
|
}
|
2020-08-05 16:41:45 +00:00
|
|
|
defer func() {
|
|
|
|
_ = rows.Close()
|
|
|
|
}()
|
2020-07-29 16:28:51 +00:00
|
|
|
for rows.Next() {
|
2020-08-05 16:41:45 +00:00
|
|
|
post := &Post{}
|
|
|
|
var parameterName, parameterValue string
|
|
|
|
err = rows.Scan(&post.Path, &post.Content, &post.Published, &post.Updated, ¶meterName, ¶meterValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if paths[post.Path] == 0 {
|
|
|
|
index := len(posts)
|
|
|
|
paths[post.Path] = index + 1
|
|
|
|
post.Parameters = make(map[string]string)
|
|
|
|
posts = append(posts, post)
|
|
|
|
}
|
|
|
|
if parameterName != "" && posts != nil {
|
|
|
|
posts[paths[post.Path]-1].Parameters[parameterName] = parameterValue
|
|
|
|
}
|
2020-07-29 16:28:51 +00:00
|
|
|
}
|
2020-08-05 16:41:45 +00:00
|
|
|
return posts, nil
|
2020-07-29 16:28:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 15:17:48 +00:00
|
|
|
func allPostPaths() ([]string, error) {
|
|
|
|
var postPaths []string
|
|
|
|
rows, err := appDb.Query("select path from posts")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var path string
|
|
|
|
_ = rows.Scan(&path)
|
|
|
|
postPaths = append(postPaths, path)
|
|
|
|
}
|
|
|
|
return postPaths, nil
|
|
|
|
}
|
2020-07-31 19:44:16 +00:00
|
|
|
|
2020-08-01 13:16:21 +00:00
|
|
|
func checkPost(post *Post) error {
|
2020-07-31 19:44:16 +00:00
|
|
|
if post == nil {
|
2020-08-01 13:16:21 +00:00
|
|
|
return errors.New("no post")
|
2020-07-31 19:44:16 +00:00
|
|
|
}
|
|
|
|
if post.Path == "" || !strings.HasPrefix(post.Path, "/") {
|
|
|
|
return errors.New("wrong path")
|
|
|
|
}
|
2020-08-01 13:16:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createPost(post *Post) error {
|
|
|
|
err := checkPost(post)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-31 19:44:16 +00:00
|
|
|
startWritingToDb()
|
|
|
|
tx, err := appDb.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec("insert into posts (path, content, published, updated) values (?, ?, ?, ?)", post.Path, post.Content, post.Published, post.Updated)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for param, value := range post.Parameters {
|
|
|
|
_, err = tx.Exec("insert into post_parameters (path, parameter, value) values (?, ?, ?)", post.Path, param, value)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
finishWritingToDb()
|
2020-08-01 13:16:21 +00:00
|
|
|
go purgeCache(post.Path)
|
|
|
|
return reloadRouter()
|
|
|
|
}
|
|
|
|
|
|
|
|
func deletePost(post *Post) error {
|
|
|
|
err := checkPost(post)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
startWritingToDb()
|
|
|
|
tx, err := appDb.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec("delete from posts where path=?", post.Path)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec("delete from post_parameters where path=?", post.Path)
|
|
|
|
if err != nil {
|
|
|
|
_ = tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
finishWritingToDb()
|
|
|
|
go purgeCache(post.Path)
|
2020-07-31 19:44:16 +00:00
|
|
|
return reloadRouter()
|
|
|
|
}
|