added support for config files, openbsd, pledge and unveil
This commit is contained in:
parent
62543e44da
commit
375933c7bc
14 changed files with 138 additions and 49 deletions
9
Makefile
9
Makefile
|
|
@ -4,6 +4,7 @@ VERSION=$(shell cat VERSION)
|
|||
GCFLAGS=-gcflags '-e'
|
||||
LDFLAGS=-ldflags '-X main.version=$(VERSION) -s -w'
|
||||
PROGRAM=gowiki
|
||||
ENV=CGO_ENABLED=0
|
||||
|
||||
all: $(PROGRAM)
|
||||
|
||||
|
|
@ -11,10 +12,10 @@ setup:
|
|||
$(CC) get github.com/client9/misspell
|
||||
|
||||
vendor: clean codeqa
|
||||
CGO_ENABLED=0 $(CC) $(BUILD) -mod=vendor $(LDFLAGS)
|
||||
$(ENV) $(CC) $(BUILD) -mod=vendor $(LDFLAGS)
|
||||
|
||||
$(PROGRAM): clean codeqa
|
||||
CGO_ENABLED=0 $(CC) $(BUILD) $(LDFLAGS)
|
||||
$(ENV) $(CC) $(BUILD) $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
$(CC) clean -x
|
||||
|
|
@ -36,9 +37,13 @@ misspell:
|
|||
|
||||
test:
|
||||
ifeq ($(shell go env GOARCH), $(shell go env GOHOSTARCH))
|
||||
ifeq ($(shell go env GOOS), $(shell go env GOHOSTOS))
|
||||
$(CC) test ./...
|
||||
else
|
||||
$(info skipping tests of other platforms)
|
||||
endif
|
||||
else
|
||||
$(info skipping tests of other platforms)
|
||||
endif
|
||||
|
||||
.PHONY: setup vendor build clean codeqa generate gofmt golint misspell test
|
||||
|
|
|
|||
26
README.md
26
README.md
|
|
@ -1 +1,27 @@
|
|||
# GoWiki - a simple wiki engine written in Go
|
||||
|
||||
## Building
|
||||
|
||||
Linux:
|
||||
|
||||
```
|
||||
make
|
||||
|
||||
or offline
|
||||
|
||||
make vendor
|
||||
```
|
||||
|
||||
OpenBSD:
|
||||
|
||||
```
|
||||
GOOS=openbsd make
|
||||
|
||||
or offline
|
||||
|
||||
GOOS=openbsd make vendor
|
||||
```
|
||||
|
||||
## Install
|
||||
|
||||
See the `scripts/` directory on how to install gowiki on Linux or OpenBSD.
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.giftfish.de/ston1th/godrop/v2"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/core"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/db"
|
||||
|
|
@ -29,10 +30,11 @@ const (
|
|||
var (
|
||||
conf core.Config
|
||||
dumpFile string
|
||||
config string
|
||||
)
|
||||
|
||||
func initServer(conf core.Config) (err error) {
|
||||
if err = core.Pledge("stdio rpath wpath cpath inet fattr flock proc exec id", ""); err != nil {
|
||||
if err = godrop.PledgePromises("stdio rpath wpath cpath inet fattr flock proc exec id unveil"); err != nil {
|
||||
return
|
||||
}
|
||||
dropCfg := godrop.Config{
|
||||
|
|
@ -50,17 +52,17 @@ func initServer(conf core.Config) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
if err = core.Pledge("stdio rpath wpath cpath inet fattr flock", ""); err != nil {
|
||||
if err = godrop.PledgePromises("stdio rpath wpath cpath inet fattr flock unveil"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
log.InitLogger(conf.DataDir, conf.LogFile, conf.Debug)
|
||||
|
||||
l, err := godrop.GetListener()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
log.InitLogger(conf.DataDir, conf.LogFile, conf.Debug)
|
||||
|
||||
srv := server.NewHTTPServer(l, conf)
|
||||
err = srv.Start()
|
||||
if err != nil {
|
||||
|
|
@ -70,7 +72,7 @@ func initServer(conf core.Config) (err error) {
|
|||
sigs := make(chan os.Signal)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
sig := <-sigs
|
||||
log.Println("caught " + sig.String())
|
||||
log.Println("signal: " + sig.String())
|
||||
go func() {
|
||||
time.Sleep(time.Second * 20)
|
||||
log.Fatal("stop timed out: killing")
|
||||
|
|
@ -83,6 +85,21 @@ func initServer(conf core.Config) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func loadConfig() error {
|
||||
if config == "" {
|
||||
return nil
|
||||
}
|
||||
file, err := os.Open(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.NewDecoder(file).Decode(&conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return file.Close()
|
||||
}
|
||||
|
||||
func Run(version string) {
|
||||
app := cli.NewApp()
|
||||
app.Name = "gowiki"
|
||||
|
|
@ -94,9 +111,12 @@ func Run(version string) {
|
|||
Usage: "start gowiki server",
|
||||
Flags: defaultFlags(serverFlags()),
|
||||
Action: func(c *cli.Context) error {
|
||||
if err := loadConfig(); err != nil {
|
||||
stdlog.Fatal("server: ", err)
|
||||
}
|
||||
conf.Version = app.Version
|
||||
if err := initServer(conf); err != nil {
|
||||
stdlog.Fatal(err)
|
||||
stdlog.Fatal("server: ", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
|
@ -110,15 +130,15 @@ func Run(version string) {
|
|||
file = os.Stdout
|
||||
err error
|
||||
)
|
||||
if err = loadConfig(); err != nil {
|
||||
stdlog.Fatal("dump: ", err)
|
||||
}
|
||||
if dumpFile != "-" {
|
||||
file, err = os.OpenFile(dumpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
|
||||
if err != nil {
|
||||
stdlog.Fatal("dump: ", err)
|
||||
}
|
||||
}
|
||||
if err = os.Chdir(conf.DataDir); err != nil {
|
||||
stdlog.Fatal("dump: ", err)
|
||||
}
|
||||
DB, err := db.New(conf.DataDir)
|
||||
if err != nil {
|
||||
stdlog.Fatal("dump: ", err)
|
||||
|
|
@ -143,15 +163,15 @@ func Run(version string) {
|
|||
file = os.Stdin
|
||||
err error
|
||||
)
|
||||
if err = loadConfig(); err != nil {
|
||||
stdlog.Fatal("restore: ", err)
|
||||
}
|
||||
if dumpFile != "-" {
|
||||
file, err = os.Open(dumpFile)
|
||||
if err != nil {
|
||||
stdlog.Fatal("dump: ", err)
|
||||
stdlog.Fatal("restore: ", err)
|
||||
}
|
||||
}
|
||||
if err = os.Chdir(conf.DataDir); err != nil {
|
||||
stdlog.Fatal("restore: ", err)
|
||||
}
|
||||
DB, err := db.New(conf.DataDir)
|
||||
if err != nil {
|
||||
stdlog.Fatal("restore: ", err)
|
||||
|
|
@ -233,6 +253,11 @@ func dumpRestoreFlags() []cli.Flag {
|
|||
|
||||
func defaultFlags(f []cli.Flag) []cli.Flag {
|
||||
return append([]cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "config, c",
|
||||
Usage: "config file",
|
||||
Destination: &config,
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "data, d",
|
||||
Value: defDataDir,
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
// Copyright (C) 2018 Marius Schellenberger
|
||||
|
||||
// +build !openbsd
|
||||
|
||||
package core
|
||||
|
||||
func Pledge(promises, execpromises string) error { return nil }
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
// Copyright (C) 2018 Marius Schellenberger
|
||||
|
||||
// +build openbsd
|
||||
|
||||
package core
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
func Pledge(promises, execpromises string) error {
|
||||
return unix.Pledge(promises, execpromises)
|
||||
}
|
||||
|
|
@ -43,14 +43,14 @@ type Result struct {
|
|||
}
|
||||
|
||||
type Config struct {
|
||||
DataDir string
|
||||
User string
|
||||
Group string
|
||||
ListenAddr string
|
||||
LogFile string
|
||||
Version string
|
||||
Secret string
|
||||
Foreground bool
|
||||
SecureCookie bool
|
||||
Debug bool
|
||||
DataDir string `json:"data_dir,omitempty"`
|
||||
User string `json:"user,omitempty"`
|
||||
Group string `json:"group,omitempty"`
|
||||
ListenAddr string `json:"listen_addr,omitempty"`
|
||||
LogFile string `json:"log_file,omitempty"`
|
||||
Version string `json:"-"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
Foreground bool `json:"foreground,omitempty"`
|
||||
SecureCookie bool `json:"secure_cookie,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"git.giftfish.de/ston1th/godrop/v2"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/cache"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/core"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/index"
|
||||
|
|
@ -32,7 +33,12 @@ type DB struct {
|
|||
|
||||
func New(dir string) (db *DB, err error) {
|
||||
db = &DB{cache: cache.NewCache()}
|
||||
db.store, err = store.NewBoltStore(filepath.Join(dir, storeFile), nil)
|
||||
dbFile := filepath.Join(dir, storeFile)
|
||||
err = godrop.Unveil(dbFile, "rwc")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
db.store, err = store.NewBoltStore(dbFile, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,7 +79,6 @@ func (db *DB) CreatePage(title, section, markdown, username string, p core.Permi
|
|||
}
|
||||
st := render.StoreTitle(section, render.Title(title))
|
||||
_, err = db.getPage(st, username)
|
||||
//if err == nil || (err != nil && err != store.ErrKeyNotFound) {
|
||||
if err != store.ErrKeyNotFound {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ package index
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"git.giftfish.de/ston1th/godrop/v2"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/core"
|
||||
"github.com/blevesearch/bleve"
|
||||
"html/template"
|
||||
|
|
@ -24,6 +25,10 @@ type Index struct {
|
|||
|
||||
func NewIndex(path string) (i *Index, err error) {
|
||||
var bi bleve.Index
|
||||
err = godrop.Unveil(path, "rwc")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = os.Stat(path); os.IsNotExist(err) {
|
||||
mapping := bleve.NewIndexMapping()
|
||||
bi, err = bleve.New(path, mapping)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"git.giftfish.de/ston1th/godrop/v2"
|
||||
stdlog "log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
|
@ -18,7 +19,13 @@ func InitLogger(dir, file string, d bool) {
|
|||
if file == "-" {
|
||||
return
|
||||
}
|
||||
f, err := os.OpenFile(filepath.Join(dir, file), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0640)
|
||||
logfile := filepath.Join(dir, file)
|
||||
err := godrop.Unveil(logfile, "rwc")
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
return
|
||||
}
|
||||
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0640)
|
||||
if err != nil {
|
||||
stdlog.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,9 +109,6 @@ func openTag(j int) (ret string) {
|
|||
for i := 0; i < j; i++ {
|
||||
ret += `<ul class="toc"><li>`
|
||||
}
|
||||
//if j > 1 {
|
||||
// ret = strings.Replace(ret, "circle", "none", j-1)
|
||||
//}
|
||||
ret = ret[0 : len(ret)-4]
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"git.giftfish.de/ston1th/godrop/v2"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/core"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/db"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/log"
|
||||
|
|
@ -57,6 +58,10 @@ func (s *HTTPServer) Start() (err error) {
|
|||
if err != nil {
|
||||
return errors.New("db: " + err.Error())
|
||||
}
|
||||
err = godrop.UnveilBlock()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if s.Secret != "" {
|
||||
s.JWT, err = jwt.New(jwt.DefaultExpiry, s.DB, bytes.NewBufferString(s.Secret))
|
||||
} else {
|
||||
|
|
|
|||
32
scripts/install_openbsd.sh
Executable file
32
scripts/install_openbsd.sh
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
#!/bin/sh
|
||||
|
||||
# rc script
|
||||
cat <<'EOF'> /etc/rc.d/gowiki
|
||||
#!/bin/sh
|
||||
|
||||
daemon="/usr/local/sbin/gowiki"
|
||||
|
||||
. /etc/rc.d/rc.subr
|
||||
|
||||
rc_cmd $1
|
||||
EOF
|
||||
chmod 555 /etc/rc.d/gowiki
|
||||
rcctl enable gowiki
|
||||
rcctl set gowiki flags server -c /var/wiki/gowiki.conf
|
||||
|
||||
# user
|
||||
useradd -m -b /var -s /sbin/nologin wiki
|
||||
|
||||
# config
|
||||
cat <<EOF> /var/wiki/gowiki.conf
|
||||
{
|
||||
"data_dir": "/var/wiki",
|
||||
"secret": "$(openssl rand -hex 32)",
|
||||
"secure_cookie": true
|
||||
}
|
||||
EOF
|
||||
chmod 640 /var/wiki/gowiki.conf
|
||||
chgrp wiki /var/wiki/gowiki.conf
|
||||
|
||||
# start
|
||||
rcctl start gowiki
|
||||
Loading…
Add table
Add a link
Reference in a new issue