mirror of https://github.com/jlelse/GoBlog
Add optional database logging and add index for queue
This commit is contained in:
parent
f16952fad1
commit
804ff1c221
|
@ -15,7 +15,7 @@ import (
|
|||
|
||||
const asContext = "https://www.w3.org/ns/activitystreams"
|
||||
|
||||
const asRequestKey requestContextKey = "asRequest"
|
||||
const asRequestKey contextKey = "asRequest"
|
||||
|
||||
func (a *goBlog) checkActivityStreamsRequest(next http.Handler) http.Handler {
|
||||
if len(a.asCheckMediaTypes) == 0 {
|
||||
|
|
|
@ -69,7 +69,7 @@ func (a *goBlog) authMiddleware(next http.Handler) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
const loggedInKey requestContextKey = "loggedIn"
|
||||
const loggedInKey contextKey = "loggedIn"
|
||||
|
||||
func (a *goBlog) checkLoggedIn(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -44,6 +44,7 @@ type configServer struct {
|
|||
type configDb struct {
|
||||
File string `mapstructure:"file"`
|
||||
DumpFile string `mapstructure:"dumpFile"`
|
||||
Debug bool `mapstructure:"debug"`
|
||||
}
|
||||
|
||||
type configCache struct {
|
||||
|
|
10
database.go
10
database.go
|
@ -2,11 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/gchaincl/sqlhooks/v2"
|
||||
sqlite "github.com/mattn/go-sqlite3"
|
||||
"github.com/schollz/sqlite3dump"
|
||||
"golang.org/x/sync/singleflight"
|
||||
|
@ -49,7 +51,7 @@ func (a *goBlog) initDatabase() (err error) {
|
|||
func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
|
||||
// Register driver
|
||||
dbDriverName := generateRandomString(15)
|
||||
sql.Register("goblog_db_"+dbDriverName, &sqlite.SQLiteDriver{
|
||||
var dr driver.Driver = &sqlite.SQLiteDriver{
|
||||
ConnectHook: func(c *sqlite.SQLiteConn) error {
|
||||
// Depends on app
|
||||
if err := c.RegisterFunc("mdtext", a.renderText, true); err != nil {
|
||||
|
@ -67,7 +69,11 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
|
|||
}
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
if a.cfg.Db.Debug {
|
||||
dr = sqlhooks.Wrap(dr, &dbHooks{})
|
||||
}
|
||||
sql.Register("goblog_db_"+dbDriverName, dr)
|
||||
// Open db
|
||||
db, err := sql.Open("goblog_db_"+dbDriverName, file+"?cache=shared&mode=rwc&_journal_mode=WAL")
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type dbHooks struct{}
|
||||
|
||||
const dbHooksBegin contextKey = "begin"
|
||||
|
||||
func (h *dbHooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
|
||||
return context.WithValue(ctx, dbHooksBegin, time.Now()), nil
|
||||
}
|
||||
|
||||
func (h *dbHooks) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
|
||||
begin := ctx.Value(dbHooksBegin).(time.Time)
|
||||
log.Printf("SQL: %s %q (%s)\n", query, args, time.Since(begin))
|
||||
return ctx, nil
|
||||
}
|
|
@ -181,6 +181,16 @@ func migrateDb(db *sql.DB, logging bool) error {
|
|||
return err
|
||||
},
|
||||
},
|
||||
&migrator.Migration{
|
||||
Name: "00016",
|
||||
Func: func(tx *sql.Tx) error {
|
||||
_, err := tx.Exec(`
|
||||
create index index_queue_name on queue (name);
|
||||
create index index_queue_schedule on queue (schedule);
|
||||
`)
|
||||
return err
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
|
|
1
go.mod
1
go.mod
|
@ -18,6 +18,7 @@ require (
|
|||
github.com/dgraph-io/ristretto v0.1.0
|
||||
github.com/elnormous/contenttype v1.0.0
|
||||
github.com/felixge/httpsnoop v1.0.2 // indirect
|
||||
github.com/gchaincl/sqlhooks/v2 v2.0.1
|
||||
github.com/go-chi/chi/v5 v5.0.3
|
||||
github.com/go-fed/httpsig v1.1.0
|
||||
github.com/go-sql-driver/mysql v1.5.0 // indirect
|
||||
|
|
5
go.sum
5
go.sum
|
@ -120,6 +120,8 @@ github.com/fsnotify/fsnotify v1.4.3-0.20170329110642-4da3e2cfbabc/go.mod h1:jwhs
|
|||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||
github.com/garyburd/redigo v1.1.1-0.20170914051019-70e1b1943d4f/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
|
||||
github.com/gchaincl/sqlhooks/v2 v2.0.1 h1:j9WZAq1Tx/xngDfEdsgUww+o9iY3rLOYGYhYWcFxdmI=
|
||||
github.com/gchaincl/sqlhooks/v2 v2.0.1/go.mod h1:Qv7HXjGB9TehamVK52yW5H+a0RRhprIj3ESTcWOG2jw=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-chi/chi/v5 v5.0.3 h1:khYQBdPivkYG1s1TAzDQG1f6eX4kD2TItYVZexL5rS4=
|
||||
github.com/go-chi/chi/v5 v5.0.3/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
|
@ -280,6 +282,7 @@ github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECae
|
|||
github.com/lestrrat-go/strftime v1.0.4 h1:T1Rb9EPkAhgxKqbcMIPguPq8glqXTA1koF8n9BHElA8=
|
||||
github.com/lestrrat-go/strftime v1.0.4/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.9.0 h1:L8nSXQQzAYByakOFMTwpjRoHsMJklur4Gi59b6VivR8=
|
||||
github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
|
||||
|
@ -295,6 +298,7 @@ github.com/mattn/go-colorable v0.0.10-0.20170816031813-ad5389df28cd/go.mod h1:9v
|
|||
github.com/mattn/go-isatty v0.0.2/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
|
||||
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
|
||||
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
|
@ -321,6 +325,7 @@ github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
|
|||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
|
||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
github.com/paulmach/go.geojson v1.4.0 h1:5x5moCkCtDo5x8af62P9IOAYGQcYHtxz2QJ3x1DoCgY=
|
||||
github.com/paulmach/go.geojson v1.4.0/go.mod h1:YaKx1hKpWF+T2oj2lFJPsW/t1Q5e1jQI61eoQSTwpIs=
|
||||
|
|
4
http.go
4
http.go
|
@ -557,8 +557,8 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
const blogContextKey requestContextKey = "blog"
|
||||
const pathContextKey requestContextKey = "httpPath"
|
||||
const blogContextKey contextKey = "blog"
|
||||
const pathContextKey contextKey = "httpPath"
|
||||
|
||||
func (a *goBlog) refreshCSPDomains() {
|
||||
a.cspDomains = ""
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
const indieAuthScope requestContextKey = "scope"
|
||||
const indieAuthScope contextKey = "scope"
|
||||
|
||||
func (a *goBlog) checkIndieAuth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
2
posts.go
2
posts.go
|
@ -177,7 +177,7 @@ type indexConfig struct {
|
|||
summaryTemplate string
|
||||
}
|
||||
|
||||
const indexConfigKey requestContextKey = "indexConfig"
|
||||
const indexConfigKey contextKey = "indexConfig"
|
||||
|
||||
func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||
ic := r.Context().Value(indexConfigKey).(*indexConfig)
|
||||
|
|
2
tor.go
2
tor.go
|
@ -16,7 +16,7 @@ import (
|
|||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
const torUsedKey requestContextKey = "tor"
|
||||
const torUsedKey contextKey = "tor"
|
||||
|
||||
func (a *goBlog) startOnionService(h http.Handler) error {
|
||||
torDataPath, err := filepath.Abs("data/tor")
|
||||
|
|
Loading…
Reference in New Issue