mirror of https://github.com/jlelse/GoBlog
Basic auth, delete posts via api, purge cache after post create / delete
This commit is contained in:
parent
b2072fddd2
commit
c69480a824
18
api.go
18
api.go
|
@ -23,3 +23,21 @@ func apiPostCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Location", post.Path)
|
w.Header().Set("Location", post.Path)
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apiPostDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer func() {
|
||||||
|
_ = r.Body.Close()
|
||||||
|
}()
|
||||||
|
post := &Post{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(post)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = deletePost(post)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
11
cache.go
11
cache.go
|
@ -90,3 +90,14 @@ func saveCache(path string, now time.Time, header map[string][]string, body []by
|
||||||
_ = tx.Commit()
|
_ = tx.Commit()
|
||||||
finishWritingToDb()
|
finishWritingToDb()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func purgeCache(path string) {
|
||||||
|
startWritingToDb()
|
||||||
|
tx, err := appDb.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, _ = tx.Exec("delete from cache where path=?", path)
|
||||||
|
_ = tx.Commit()
|
||||||
|
finishWritingToDb()
|
||||||
|
}
|
||||||
|
|
32
config.go
32
config.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
|
@ -10,6 +9,7 @@ type config struct {
|
||||||
db *configDb
|
db *configDb
|
||||||
cache *configCache
|
cache *configCache
|
||||||
blog *configBlog
|
blog *configBlog
|
||||||
|
user *configUser
|
||||||
}
|
}
|
||||||
|
|
||||||
type configServer struct {
|
type configServer struct {
|
||||||
|
@ -29,16 +29,23 @@ type configCache struct {
|
||||||
// exposed to templates via function "blog"
|
// exposed to templates via function "blog"
|
||||||
type configBlog struct {
|
type configBlog struct {
|
||||||
// Language of the blog, e.g. "en" or "de"
|
// Language of the blog, e.g. "en" or "de"
|
||||||
Lang string
|
Lang string
|
||||||
// Title of the blog, e.g. "My blog"
|
// Title of the blog, e.g. "My blog"
|
||||||
Title string
|
Title string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type configUser struct {
|
||||||
|
nick string
|
||||||
|
name string
|
||||||
|
password string
|
||||||
|
}
|
||||||
|
|
||||||
var appConfig = &config{
|
var appConfig = &config{
|
||||||
server: &configServer{},
|
server: &configServer{},
|
||||||
db: &configDb{},
|
db: &configDb{},
|
||||||
cache: &configCache{},
|
cache: &configCache{},
|
||||||
blog: &configBlog{},
|
blog: &configBlog{},
|
||||||
|
user: &configUser{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() error {
|
func initConfig() error {
|
||||||
|
@ -52,37 +59,36 @@ func initConfig() error {
|
||||||
serverLogging := "server.logging"
|
serverLogging := "server.logging"
|
||||||
viper.SetDefault(serverLogging, false)
|
viper.SetDefault(serverLogging, false)
|
||||||
appConfig.server.logging = viper.GetBool(serverLogging)
|
appConfig.server.logging = viper.GetBool(serverLogging)
|
||||||
logConfig(serverLogging, appConfig.server.logging)
|
|
||||||
serverPort := "server.port"
|
serverPort := "server.port"
|
||||||
viper.SetDefault(serverPort, 8080)
|
viper.SetDefault(serverPort, 8080)
|
||||||
appConfig.server.port = viper.GetInt(serverPort)
|
appConfig.server.port = viper.GetInt(serverPort)
|
||||||
logConfig(serverPort, appConfig.server.port)
|
|
||||||
// Database
|
// Database
|
||||||
databaseFile := "database.file"
|
databaseFile := "database.file"
|
||||||
viper.SetDefault(databaseFile, "data/db.sqlite")
|
viper.SetDefault(databaseFile, "data/db.sqlite")
|
||||||
appConfig.db.file = viper.GetString(databaseFile)
|
appConfig.db.file = viper.GetString(databaseFile)
|
||||||
logConfig(databaseFile, appConfig.db.file)
|
|
||||||
// Caching
|
// Caching
|
||||||
cacheEnable := "cache.enable"
|
cacheEnable := "cache.enable"
|
||||||
viper.SetDefault(cacheEnable, true)
|
viper.SetDefault(cacheEnable, true)
|
||||||
appConfig.cache.enable = viper.GetBool(cacheEnable)
|
appConfig.cache.enable = viper.GetBool(cacheEnable)
|
||||||
logConfig(cacheEnable, appConfig.cache.enable)
|
|
||||||
cacheExpiration := "cache.expiration"
|
cacheExpiration := "cache.expiration"
|
||||||
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)
|
|
||||||
// Blog meta
|
// Blog meta
|
||||||
blogLang := "blog.lang"
|
blogLang := "blog.lang"
|
||||||
viper.SetDefault(blogLang, "en")
|
viper.SetDefault(blogLang, "en")
|
||||||
appConfig.blog.Lang = viper.GetString(blogLang)
|
appConfig.blog.Lang = viper.GetString(blogLang)
|
||||||
logConfig(blogLang, appConfig.blog.Lang)
|
|
||||||
blogTitle := "blog.title"
|
blogTitle := "blog.title"
|
||||||
viper.SetDefault(blogTitle, "My blog")
|
viper.SetDefault(blogTitle, "My blog")
|
||||||
appConfig.blog.Title = viper.GetString(blogTitle)
|
appConfig.blog.Title = viper.GetString(blogTitle)
|
||||||
logConfig(blogTitle, appConfig.blog.Title)
|
// User
|
||||||
|
userNick := "user.nick"
|
||||||
|
viper.SetDefault(userNick, "admin")
|
||||||
|
appConfig.user.nick = viper.GetString(userNick)
|
||||||
|
userName := "user.name"
|
||||||
|
viper.SetDefault(userName, "Admin")
|
||||||
|
appConfig.user.name = viper.GetString(userName)
|
||||||
|
userPassword := "user.password"
|
||||||
|
viper.SetDefault(userPassword, "secret")
|
||||||
|
appConfig.user.password = viper.GetString(userPassword)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func logConfig(key string, value interface{}) {
|
|
||||||
log.Println(key+":", value)
|
|
||||||
}
|
|
||||||
|
|
|
@ -8,4 +8,8 @@ cache:
|
||||||
expiration: 600
|
expiration: 600
|
||||||
blog:
|
blog:
|
||||||
lang: en
|
lang: en
|
||||||
title: My blog
|
title: My blog
|
||||||
|
user:
|
||||||
|
nick: admin
|
||||||
|
name: Admin
|
||||||
|
password: secret
|
5
http.go
5
http.go
|
@ -72,8 +72,11 @@ func buildHandler() (http.Handler, error) {
|
||||||
r.Use(middleware.GetHead)
|
r.Use(middleware.GetHead)
|
||||||
|
|
||||||
r.Route("/api", func(apiRouter chi.Router) {
|
r.Route("/api", func(apiRouter chi.Router) {
|
||||||
// TODO: Auth
|
apiRouter.Use(middleware.BasicAuth("API", map[string]string{
|
||||||
|
appConfig.user.nick: appConfig.user.password,
|
||||||
|
}))
|
||||||
apiRouter.Post("/post", apiPostCreate)
|
apiRouter.Post("/post", apiPostCreate)
|
||||||
|
apiRouter.Delete("/post", apiPostDelete)
|
||||||
})
|
})
|
||||||
|
|
||||||
allPostPaths, err := allPostPaths()
|
allPostPaths, err := allPostPaths()
|
||||||
|
|
42
posts.go
42
posts.go
|
@ -76,13 +76,21 @@ func allPostPaths() ([]string, error) {
|
||||||
return postPaths, nil
|
return postPaths, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createPost(post *Post) error {
|
func checkPost(post *Post) error {
|
||||||
if post == nil {
|
if post == nil {
|
||||||
return nil
|
return errors.New("no post")
|
||||||
}
|
}
|
||||||
if post.Path == "" || !strings.HasPrefix(post.Path, "/") {
|
if post.Path == "" || !strings.HasPrefix(post.Path, "/") {
|
||||||
return errors.New("wrong path")
|
return errors.New("wrong path")
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createPost(post *Post) error {
|
||||||
|
err := checkPost(post)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
startWritingToDb()
|
startWritingToDb()
|
||||||
tx, err := appDb.Begin()
|
tx, err := appDb.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,5 +113,35 @@ func createPost(post *Post) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
finishWritingToDb()
|
finishWritingToDb()
|
||||||
|
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)
|
||||||
return reloadRouter()
|
return reloadRouter()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue