22 Commits
v1.6 ... v1.8

Author SHA1 Message Date
Alex Schroeder
8e35336cb3 No new features for release 1.8 2024-02-26 07:37:47 +01:00
Alex Schroeder
2a44c2a74f Make some of the types private
Don't export types unless necessary. Mostly because that looks weird
in Go documentation: when the functions acting on the types are
private the types themselves might as well be private.

Consequently, only the types that are used to write templates are
exported! For those types, the documentation has been improved (mostly
for Feed and Item).

In order to avoid nameclashes when hiding the types that didn't need
exporting, new naming schema was used. As these types were contains
for maps plus a mutex, they are called "stores" controlling access to
the maps: Accounts → accountStore, Index → indexStore, Template →
templateStore, Watches → watchStore.
2024-02-24 17:56:02 +01:00
Alex Schroeder
fe9a621f1e Document HTTP handlers in the README
Change the heading from Source to Hacking because I suspect the README
is were the notes on the source code go. The reason I don't want to
put them in code comments is because almost none of the Oddmu symbols
are exported.
2024-02-24 16:58:02 +01:00
Alex Schroeder
be663eed32 Makefile improvements
Introduce a PREFIX variable for the install target so one can override
this. Document this in the README.

Add rules for a build target and for a binary target that depends on
all the Go files so that these can be used as prerequisites for other
targets. The goal is to avoid unnecessary recompilations.

Designate all the non-file targets as phony targets.
2024-02-24 16:54:31 +01:00
Alex Schroeder
86ef305e9c Fix oddmu-list man page format
The optional string provided on the command line is set in italics.
2024-02-24 16:52:46 +01:00
Alex Schroeder
1fd97ae717 Document -full for the version subcommand 2024-02-23 12:32:27 +01:00
Alex Schroeder
d0fdf8c3c6 Highlight template name in the man page 2024-02-23 12:32:27 +01:00
Alex Schroeder
1786050e72 Reorganize the list of man page links
Use two groups, one for the web server and one for command-line use.
2024-02-23 12:32:27 +01:00
Alex Schroeder
f12252e148 Add missing source file to README
Add a test for that, too.
2024-02-23 00:28:41 +01:00
Alex Schroeder
f5f997261e Add missing man page link to the README
Add a test for that!
2024-02-23 00:23:52 +01:00
Alex Schroeder
43408707c5 Add package documentation 2024-02-23 00:09:07 +01:00
Alex Schroeder
50ce79d60d Update RELEASE instructions 2024-02-21 07:32:41 +01:00
Alex Schroeder
745500f09f Describe features of release 1.7 2024-02-20 21:54:14 +01:00
Alex Schroeder
f02491dda0 Add -full to version command 2024-02-20 21:47:59 +01:00
Alex Schroeder
0001583044 Go fmt 2024-02-20 21:47:50 +01:00
Alex Schroeder
4a5b7d52cd Use exiffix library to fix image orientation
If you upload an unedited image from an iPhone, the EXIF data
indicates how it should be oriented. Oddmu used to resize images strip
the EXIF data, resulting in resized images that were not oriented
correctly. If the EXIF data is stripped, the image has to be rotated.
In order to do this, the exiffix single-function library is used to
work around "image/jpeg: correct for EXIF orientation? #4341", opened
in 2012. See https://github.com/golang/go/issues/4341 for more
information and a link to https://github.com/edwvee/exiffix.
2024-02-20 21:45:51 +01:00
Alex Schroeder
c65f3ea386 Switch image manipulation library
Switch from github.com/anthonynsimon/bild/imgio
to github.com/disintegration/imaging. The goal is to get some sort of
auto-orientation going.

Image resizing now uses Lanczos instead of Linear.
2024-02-20 20:13:46 +01:00
Alex Schroeder
d2adffed6e Test HEIC upload
The bashdrew/goheif library is now used implicity and image decoding
figures out how to do it on its own.

The bashdrew/goheif only does decoding, not encoding, and so the HEIC
testfile is base64 encoded.
2024-02-20 13:09:22 +01:00
Alex Schroeder
d8e1d79127 Wording changes for the upload.html template 2024-02-19 22:44:33 +01:00
Alex Schroeder
d839219f96 Upload multiple files in one go
This requires an update to the upload.html template.
2024-02-19 22:27:56 +01:00
Alex Schroeder
103d1f4609 Add oddmu-nginx man page 2024-02-19 17:38:22 +01:00
Alex Schroeder
28a63e7479 Return 404 for feed requests of non-existing pages
Otherwise, there's a panic if requesting the RSS feed of a
non-existing page. This was caused by a recent rewrite of the
viewHandler.
2024-02-19 11:10:21 +01:00
43 changed files with 801 additions and 315 deletions

View File

@@ -1,4 +1,7 @@
SHELL=/bin/bash
PREFIX=${HOME}/.local
.PHONY: help build test run upload docs install missing
help:
@echo Help for Oddmu
@@ -13,7 +16,7 @@ help:
@echo make docs
@echo " create man pages from text files"
@echo
@echo go build
@echo make build
@echo " just build it"
@echo
@echo make install
@@ -22,14 +25,18 @@ help:
@echo make upload
@echo " this is how I upgrade my server"
run:
go run .
build: oddmu
oddmu: *.go
go build
test:
go test -shuffle on .
upload:
go build
run:
go run .
upload: build
rsync --itemize-changes --archive oddmu sibirocobombus.root:/home/oddmu/
ssh sibirocobombus.root "systemctl restart oddmu; systemctl restart alex; systemctl restart claudia; systemctl restart campaignwiki"
@echo Changes to the template files need careful consideration
@@ -37,11 +44,6 @@ upload:
docs:
cd man; make
install:
make docs
for n in 1 5 7; do install -D -t $$HOME/.local/share/man/man$$n man/*.$$n; done
go build
install -D -t $$HOME/.local/bin oddmu
missing:
for f in man/*.txt; do grep --quiet "$$f" README.md || echo $$f is not in the README; done
install: build docs
for n in 1 5 7; do install -D -t ${PREFIX}/share/man/man$$n man/*.$$n; done
install -D -t ${PREFIX}/.local/bin oddmu

View File

@@ -48,6 +48,18 @@ links. Local links must use percent encoding for page names so there
is a section about percent encoding. The man page also explains how
feeds are generated.
[oddmu-releases(7)](/oddmu.git/blob/main/man/oddmu-releases.7.txt):
This man page lists all the Oddmu versions and their user-visible
changes.
[oddmu-releases(7)](/oddmu.git/blob/main/man/oddmu-releases.7.txt):
This man page lists all the Oddmu versions and their user-visible
changes.
[oddmu-version(1)](/oddmu.git/blob/main/man/oddmu-version.1.txt): This
man page documents the "version" subcommand which you can use to get
installed Oddmu version.
[oddmu-list(1)](/oddmu.git/blob/main/man/oddmu-list.1.txt): This man
page documents the "list" subcommand which you can use to get page
names and page titles.
@@ -91,8 +103,12 @@ This man page documents how the templates can be changed (how they
templates.
[oddmu-apache(5)](/oddmu.git/blob/main/man/oddmu-apache.5.txt): This
man page documents how to set up the web server for various common
tasks such as using logins to limit what visitors can edit.
man page documents how to set up the Apache web server for various
common tasks such as using logins to limit what visitors can edit.
[oddmu-nginx(5)](/oddmu.git/blob/main/man/oddmu-nginx.5.txt): This man
page documents how to set up the freenginx web server for various
common tasks such as using logins to limit what visitors can edit.
[oddmu.service(5)](/oddmu.git/blob/main/man/oddmu.service.5.txt): This
man page documents how to setup a systemd unit and have it manage
@@ -100,15 +116,28 @@ Oddmu. “Great configurability brings great burdens.”
## Building
To build the binary:
```sh
go build
```
The man pages are already built. If you want to rebuild them, you need
to have [scdoc](https://git.sr.ht/~sircmpwn/scdoc) installed.
```sh
make docs
```
The `Makefile` in the `man` directory has targets to create Markdown
and HTML files.
## Running
The working directory is where pages are saved and where templates are
loaded from. You need a copy of the template files in this directory.
Here's how to start it in the source directory:
Here's how to build and run straight from the source directory:
```sh
go run .
@@ -117,11 +146,43 @@ go run .
The program serves the local directory as a wiki on port 8080. Point
your browser to http://localhost:8080/ to use it.
Once the `oddmu` binary is built, you can run it instead:
```sh
./oddmu
```
To read the main man page witihout installing Oddmu:
```sh
man -l man/oddmu.1
```
## Installing
This installs `oddmu` into `$HOME/.local/bin` and the manual pages
into `$HOME/.local/share/man/`.
```sh
make install
```
To install it elsewhere, here's an example using [GNU
Stow](https://www.gnu.org/software/stow/) to install it into
`/usr/local/stow` in a way that allows you to uninstall it later:
```sh
sudo mkdir /usr/local/stow/oddmu
sudo make install PREFIX=/usr/local/stow/oddmu/
cd /usr/local/stow
sudo stow oddmu
```
## Bugs
If you spot any, [contact](https://alexschroeder.ch/wiki/Contact) me.
## Source
## Hacking
If you're interested in making changes to the code, here's a
high-level introduction to the various source files.
@@ -134,6 +195,8 @@ high-level introduction to the various source files.
account link destinations with the URI provided by webfinger
- `add_append.go` implements the `/add` and `/append` handlers
- `archive.go` implements the `/archive` handler
- `changes.go` implements the "notifications": the automatic addition
of links to index, changes and hashtag files when pages are edited
- `diff.go` implements the `/diff` handler
- `edit_save.go` implements the `/edit` and `/save` handlers
- `feed.go` implements the feed for a page based on the links it lists
@@ -153,6 +216,8 @@ high-level introduction to the various source files.
- `watch.go` implements the filesystem notification watch
- `wiki.go` implements the main function
### Changing the markup rules
If you want to change the markup rules, your starting point should be
`parser.go`. Make sure you read the documentation of [Go
Markdown](https://github.com/gomarkdown/markdown) and note that it
@@ -161,6 +226,8 @@ that the MathJax Javascript gets loaded) and
[MMark](https://mmark.miek.nl/post/syntax/) support, and it shows how
extensions can be added.
### Filenames and URL path
One of the sad parts of the code is the distinction between path and
filepath. On a Linux system, this doesn't matter. I suspect that it
also doesn't matter on MacOS and Windows because the file systems
@@ -178,6 +245,21 @@ If you need to access the page name in code that is used from a
template, you have to decode the path. See the code in `diff.go` for
an example.
### HTTP handlers
The URL paths all have the form `/action/directory/pagename` (with
directory being optional and pagename sometimes being optional). If
you need to limit access in Apache or nginx or some other web server
acting as a [reverse
proxy](https://en.wikipedia.org/wiki/Reverse_proxy), you can do that.
See `man oddmu-apache` and `man oddmu-nginx` for some configuration
examples.
This is how you can prevent some actions by simply not passing them on
to Oddmu, or you can require authentication for certain actions.
Furthermore, you can do the same for directories, allowing you to use
subdirectories as separate sites, each with their own editors.
## References
[Writing Web Applications](https://golang.org/doc/articles/wiki/)

View File

@@ -3,10 +3,12 @@ When preparing a new release
1. Run tests
2. Make docs
2. Update man/oddmu-releases.7.txt
3. Make sure all files are checked in
3. make docs
4. Update man/oddmu-releases.7.txt
4. Make sure all files are checked in
5. Tag the release and push the tag to all remotes
6. cd man && make upload

View File

@@ -15,8 +15,8 @@ import (
// environment variable ODDMU_WEBFINGER to "1".
var useWebfinger = false
// Accounts contains the map used to set the usernames. Make sure to lock and unlock as appropriate.
type Accounts struct {
// accountStore controlls access to the usernames. Make sure to lock and unlock as appropriate.
type accountStore struct {
sync.RWMutex
// uris is a map, mapping account names likes "@alex@alexschroeder.ch" to URIs like
@@ -25,7 +25,7 @@ type Accounts struct {
}
// accounts holds the global mapping of accounts to profile URIs.
var accounts Accounts
var accounts accountStore
// This is called once at startup and therefore does not need to be locked. On every restart, this map starts empty and
// is slowly repopulated as pages are visited.
@@ -36,11 +36,11 @@ func init() {
}
}
// account links a social media account like @account@domain to a profile page like https://domain/user/account. Any
// account seen for the first time uses a best guess profile URI. It is also looked up using webfinger, in parallel. See
// accountLink links a social media accountLink like @accountLink@domain to a profile page like https://domain/user/accountLink. Any
// accountLink seen for the first time uses a best guess profile URI. It is also looked up using webfinger, in parallel. See
// lookUpAccountUri. If the lookup succeeds, the best guess is replaced with the new URI so on subsequent requests, the
// URI is correct.
func account(p *parser.Parser, data []byte, offset int) (int, ast.Node) {
func accountLink(p *parser.Parser, data []byte, offset int) (int, ast.Node) {
data = data[offset:]
i := 1 // skip @ of username
n := len(data)
@@ -105,7 +105,7 @@ func lookUpAccountUri(account, domain string) {
log.Printf("Failed to read from %s: %s", account, err)
return
}
var wf WebFinger
var wf webFinger
err = json.Unmarshal([]byte(body), &wf)
if err != nil {
log.Printf("Failed to parse the JSON from %s: %s", account, err)
@@ -121,24 +121,24 @@ func lookUpAccountUri(account, domain string) {
accounts.uris[account] = uri
}
// Link a link in the WebFinger JSON.
type Link struct {
// link a link in the WebFinger JSON.
type link struct {
Rel string `json:"rel"`
Type string `json:"type"`
Href string `json:"href"`
}
// WebFinger is a structure used to unmarshall JSON.
type WebFinger struct {
// webFinger is a structure used to unmarshall JSON.
type webFinger struct {
Subject string `json:"subject"`
Aliases []string `json:"aliases"`
Links []Link `json:"links"`
Links []link `json:"links"`
}
// parseWebFinger parses the web finger JSON and returns the profile page URI. For unmarshalling the JSON, it uses the
// Link and WebFinger structs.
func parseWebFinger(body []byte) (string, error) {
var wf WebFinger
var wf webFinger
err := json.Unmarshal(body, &wf)
if err != nil {
return "", err

View File

@@ -27,34 +27,34 @@ func archiveHandler(w http.ResponseWriter, r *http.Request, path string) {
matches := re.MatchString(path)
dir := filepath.Dir(filepath.FromSlash(path))
z := zip.NewWriter(w)
err = filepath.Walk(dir, func (path string, info fs.FileInfo, err error) error {
err = filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if path != "." && strings.HasPrefix(filepath.Base(path), ".") {
return filepath.SkipDir
}
} else if !strings.HasPrefix(filepath.Base(path), ".") &&
(matches || !re.MatchString(path)) {
zf, err := z.Create(path)
if err != nil {
log.Println(err)
return err
}
if info.IsDir() {
if path != "." && strings.HasPrefix(filepath.Base(path), ".") {
return filepath.SkipDir
}
} else if !strings.HasPrefix(filepath.Base(path), ".") &&
(matches || !re.MatchString(path)) {
zf, err := z.Create(path)
if err != nil {
log.Println(err)
return err
}
file, err := os.Open(path)
if err != nil {
log.Println(err)
return err
}
_, err = io.Copy(zf, file)
if err != nil {
log.Println(err)
return err
}
file, err := os.Open(path)
if err != nil {
log.Println(err)
return err
}
return nil
})
_, err = io.Copy(zf, file)
if err != nil {
log.Println(err)
return err
}
}
return nil
})
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@@ -4,8 +4,8 @@ import (
"archive/zip"
"github.com/stretchr/testify/assert"
"os"
"testing"
"strings"
"testing"
)
func TestArchive(t *testing.T) {

17
feed.go
View File

@@ -11,16 +11,33 @@ import (
"time"
)
// Item is a Page plus a Date.
type Item struct {
// Page is the page being used as the feed item.
Page
// Date is the last modification date of the file storing the page. As the pages used by Oddmu are plain
// Markdown files, they don't contain any metadata. Instead, the last modification date of the file is used.
// This makes it work well with changes made to the files outside of Oddmu.
Date string
}
// Feed is an Item used for the feed itself, plus an array of items based on the linked pages.
type Feed struct {
// Item is the page containing the list of links. It's title is used for the feed and it's last modified time is
// used for the publication date. Thus, if linked pages change but the page with the links doesn't change, the
// publication date remains unchanged.
Item
// Items are based on the pages linked in list items starting with an asterisk ("*"). Links in
// list items starting with a minus ("-") are ignored!
Items []Item
}
// feed returns a RSS 2.0 feed for any page. The feed items it contains are the pages linked from in list items starting
// with an asterisk ("*").
func feed(p *Page, ti time.Time) *Feed {
feed := new(Feed)
feed.Name = p.Name

View File

@@ -2,6 +2,7 @@ package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
@@ -11,6 +12,11 @@ func TestFeed(t *testing.T) {
"Welcome to Oddµ")
}
func TestNoFeed(t *testing.T) {
assert.HTTPStatusCode(t,
makeHandler(viewHandler, true), "GET", "/view/no-feed.rss", nil, http.StatusNotFound)
}
func TestFeedItems(t *testing.T) {
cleanup(t, "testdata/feed")
index.load()

5
go.mod
View File

@@ -3,9 +3,11 @@ module alexschroeder.ch/cgit/oddmu
go 1.21.0
require (
github.com/anthonynsimon/bild v0.13.0
github.com/bashdrew/goheif v0.0.0-20230406184952-7a08ca9c9bdd
github.com/charmbracelet/lipgloss v0.9.1
github.com/disintegration/imaging v1.6.2
github.com/edwvee/exiffix v0.0.0-20210922235313-0f6cbda5e58f
github.com/fsnotify/fsnotify v1.7.0
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47
github.com/google/subcommands v1.2.0
github.com/hexops/gotextdiff v1.0.3
@@ -20,7 +22,6 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect

33
go.sum
View File

@@ -1,7 +1,3 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/anthonynsimon/bild v0.13.0 h1:mN3tMaNds1wBWi1BrJq0ipDBhpkooYfu7ZFSMhXt1C8=
github.com/anthonynsimon/bild v0.13.0/go.mod h1:tpzzp0aYkAsMi1zmfhimaDyX1xjn2OUc1AJZK/TF0AE=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
@@ -10,14 +6,13 @@ github.com/bashdrew/goheif v0.0.0-20230406184952-7a08ca9c9bdd h1:SxkQeH4jjXT0zMg
github.com/bashdrew/goheif v0.0.0-20230406184952-7a08ca9c9bdd/go.mod h1:p1sbxRy+MY71fEWHcfRmerC8WUYXDFCExF9A7aXwp98=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/edwvee/exiffix v0.0.0-20210922235313-0f6cbda5e58f h1:RMnUwTnNR070mFAEIoqMYjNirHj8i0h79VXTYyBCyVA=
github.com/edwvee/exiffix v0.0.0-20210922235313-0f6cbda5e58f/go.mod h1:KoE3Ti1qbQXCb3s/XGj0yApHnbnNnn1bXTtB5Auq/Vc=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 h1:k4Tw0nt6lwro3Uin8eqoET7MDA4JnT8YgbCjc/g5E3k=
@@ -28,10 +23,8 @@ github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -39,7 +32,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
@@ -47,13 +39,10 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pemistahl/lingua-go v1.4.0 h1:ifYhthrlW7iO4icdubwlduYnmwU37V1sbNrwhKBR4rM=
github.com/pemistahl/lingua-go v1.4.0/go.mod h1:ECuM1Hp/3hvyh7k8aWSqNCPlTxLemFZsRjocUf3KgME=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -62,35 +51,23 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

View File

@@ -16,9 +16,8 @@ import (
type docid uint
// Index contains the two maps used for search. Make sure to lock and
// unlock as appropriate.
type Index struct {
// indexStore controls access to the maps used for search. Make sure to lock and unlock as appropriate.
type indexStore struct {
sync.RWMutex
// next_id is the number of the next document added to the index
@@ -34,14 +33,14 @@ type Index struct {
titles map[string]string
}
var index Index
var index indexStore
func init() {
index.reset()
}
// reset the index. This assumes that the index is locked. It's useful for tests.
func (idx *Index) reset() {
func (idx *indexStore) reset() {
idx.next_id = 0
idx.token = make(map[string][]docid)
idx.documents = make(map[docid]string)
@@ -49,7 +48,7 @@ func (idx *Index) reset() {
}
// addDocument adds the text as a new document. This assumes that the index is locked!
func (idx *Index) addDocument(text []byte) docid {
func (idx *indexStore) addDocument(text []byte) docid {
id := idx.next_id
idx.next_id++
for _, token := range hashtags(text) {
@@ -66,7 +65,7 @@ func (idx *Index) addDocument(text []byte) docid {
}
// deleteDocument deletes all references to the id. The id can no longer be used. This assumes that the index is locked.
func (idx *Index) deleteDocument(id docid) {
func (idx *indexStore) deleteDocument(id docid) {
// Looping through all tokens makes sense if there are few tokens (like hashtags). It doesn't make sense if the
// number of tokens is large (like for full-text search or a trigram index).
for token, ids := range idx.token {
@@ -87,7 +86,7 @@ func (idx *Index) deleteDocument(id docid) {
// deletePageName determines the document id based on the page name and calls deleteDocument to delete all references.
// This assumes that the index is unlocked.
func (idx *Index) deletePageName(name string) {
func (idx *indexStore) deletePageName(name string) {
idx.Lock()
defer idx.Unlock()
var id docid
@@ -106,12 +105,12 @@ func (idx *Index) deletePageName(name string) {
}
// remove the page from the index. Do this when deleting a page. This assumes that the index is unlocked.
func (idx *Index) remove(p *Page) {
func (idx *indexStore) remove(p *Page) {
idx.deletePageName(p.Name)
}
// load loads all the pages and indexes them. This takes a while. It returns the number of pages indexed.
func (idx *Index) load() (int, error) {
func (idx *indexStore) load() (int, error) {
idx.Lock()
defer idx.Unlock()
err := filepath.Walk(".", idx.walk)
@@ -123,7 +122,7 @@ func (idx *Index) load() (int, error) {
}
// walk reads a file and adds it to the index. This assumes that the index is locked.
func (idx *Index) walk(path string, info fs.FileInfo, err error) error {
func (idx *indexStore) walk(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
@@ -149,7 +148,7 @@ func (idx *Index) walk(path string, info fs.FileInfo, err error) error {
}
// addPage adds a page to the index. This assumes that the index is locked.
func (idx *Index) addPage(p *Page) {
func (idx *indexStore) addPage(p *Page) {
id := idx.addDocument(p.Body)
idx.documents[id] = p.Name
p.handleTitle(false)
@@ -157,14 +156,14 @@ func (idx *Index) addPage(p *Page) {
}
// add a page to the index. This assumes that the index is unlocked.
func (idx *Index) add(p *Page) {
func (idx *indexStore) add(p *Page) {
idx.Lock()
defer idx.Unlock()
idx.addPage(p)
}
// dump prints the index to the log for debugging.
func (idx *Index) dump() {
func (idx *indexStore) dump() {
idx.RLock()
defer idx.RUnlock()
for token, ids := range idx.token {
@@ -173,14 +172,14 @@ func (idx *Index) dump() {
}
// updateIndex updates the index for a single page.
func (idx *Index) update(p *Page) {
func (idx *indexStore) update(p *Page) {
idx.remove(p)
idx.add(p)
}
// search searches the index for a query string and returns page
// names.
func (idx *Index) search(q string) []string {
func (idx *indexStore) search(q string) []string {
idx.RLock()
defer idx.RUnlock()
names := make([]string, 0)

View File

@@ -7,7 +7,7 @@ import (
)
func TestIndexAdd(t *testing.T) {
idx := &Index{}
idx := &indexStore{}
idx.reset()
idx.Lock()
defer idx.Unlock()

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-APACHE" "5" "2024-02-17"
.TH "ODDMU-APACHE" "5" "2024-02-19"
.PP
.SH NAME
.PP
@@ -390,7 +390,7 @@ such that ever domain acts as a reverse proxy to a different Oddmu instance.\&
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1), \fIoddmu-filter\fR(7)
\fIoddmu\fR(1), \fIoddmu-filter\fR(7), \fIoddmu-nginx\fR(5)
.PP
"Apache Core Features".\&
https://httpd.\&apache.\&org/docs/current/mod/core.\&html

View File

@@ -337,7 +337,7 @@ such that ever domain acts as a reverse proxy to a different Oddmu instance.
# SEE ALSO
_oddmu_(1), _oddmu-filter_(7)
_oddmu_(1), _oddmu-filter_(7), _oddmu-nginx_(5)
"Apache Core Features".
https://httpd.apache.org/docs/current/mod/core.html

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-FILTER" "7" "2024-02-17"
.TH "ODDMU-FILTER" "7" "2024-02-19"
.PP
.SH NAME
.PP
@@ -52,11 +52,11 @@ always does: search is limited to "project/" and its subdirectories.\&
If the subdirectory is a private site, then you need to use ODDMU_FILTER to
exclude it from directory tree actions in the main site, and you need to
configure your web server such that it doesn'\&t allow visitors access to the
directory tree without authentication.\& See \fIoddmu-apache\fR(5).\&
directory tree without authentication.\&
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1), \fIoddmu-search\fR(7), \fIoddmu-apache\fR(5)
\fIoddmu\fR(1), \fIoddmu-search\fR(7), \fIoddmu-apache\fR(5), \fIoddmu-nginx\fR(5)
.PP
.SH AUTHORS
.PP

View File

@@ -45,11 +45,11 @@ always does: search is limited to "project/" and its subdirectories.
If the subdirectory is a private site, then you need to use ODDMU_FILTER to
exclude it from directory tree actions in the main site, and you need to
configure your web server such that it doesn't allow visitors access to the
directory tree without authentication. See _oddmu-apache_(5).
directory tree without authentication.
# SEE ALSO
_oddmu_(1), _oddmu-search_(7), _oddmu-apache_(5)
_oddmu_(1), _oddmu-search_(7), _oddmu-apache_(5), _oddmu-nginx_(5)
# AUTHORS

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-HTML" "1" "2024-02-17"
.TH "ODDMU-HTML" "1" "2024-02-26"
.PP
.SH NAME
.PP

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-LIST" "1" "2024-02-17"
.TH "ODDMU-LIST" "1" "2024-02-24"
.PP
.SH NAME
.PP
@@ -13,7 +13,7 @@ oddmu-list - list page names and titles from the command-line
.PP
.SH SYNOPSIS
.PP
\fBoddmu list\fR [-dir string]
\fBoddmu list\fR [-dir \fIstring\fR]
.PP
.SH DESCRIPTION
.PP

View File

@@ -6,7 +6,7 @@ oddmu-list - list page names and titles from the command-line
# SYNOPSIS
*oddmu list* [-dir string]
*oddmu list* [-dir _string_]
# DESCRIPTION

91
man/oddmu-nginx.5 Normal file
View File

@@ -0,0 +1,91 @@
.\" Generated by scdoc 1.11.3
.\" Complete documentation for this program is not available as a GNU info page
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-NGINX" "5" "2024-02-19"
.PP
.SH NAME
.PP
oddmu-nginx - how to setup Nginx as a reverse proxy for Oddmu
.PP
.SS DESCRIPTION
.PP
The oddmu program serves the current working directory as a wiki on port 8080.\&
This is an unpriviledged port so an ordinary user account can do this.\&
.PP
This page explains how to setup NGINX on Debian to act as a reverse proxy for
Oddmu.\& Once this is done, you can use NGINX to provide HTTPS, request users to
authenticate themselves, and so on.\&
.PP
.SS CONFIGURATION
.PP
The site is defined in "/etc/nginx/sites-available/default", in the \fIserver\fR
section.\& Add a new \fIlocation\fR section after the existing \fIlocation\fR section:
.PP
.nf
.RS 4
location ~ ^/(view|diff|edit|save|add|append|upload|drop|search|archive)/ {
proxy_pass http://localhost:8080;
}
.fi
.RE
.PP
If you remove an action from the regular expression, those requests no longer
get passed on to Oddmu.\& They are essentially disabled.\& Somebody on the same
machine pointing their browser at http://localhost:8080/ directly would still
have access to all the actions, of course.\&
.PP
To restrict access to some actions, use two different \fIlocation\fR sections:
.PP
.nf
.RS 4
# public
location ~ ^/(view|diff|search)/ {
proxy_pass http://localhost:8080;
}
# password required
location ~ ^/(edit|save|add|append|upload|drop|archive)/ {
auth_basic "Oddmu author";
auth_basic_user_file /etc/nginx/conf\&.d/htpasswd;
proxy_pass http://localhost:8080;
}
.fi
.RE
.PP
The passwords in "/etc/nginx/conf.\&d/htpasswd" are generated using \fIopenssl\fR(1).\&
Assuming the password is "CPTk&qO[Y@?\&M~L>qKOkd", this is how you encrypt it:
.PP
.nf
.RS 4
openssl passwd \&'CPTk&qO[Y@?M~L>qKOkd\&'
.fi
.RE
.PP
The output gets used in "/etc/nginx/conf.\&d/htpasswd".\& Here'\&s the user "alex"
using this password:
.PP
.nf
.RS 4
alex:$1$DOwphABk$W4VmR9p8t2\&.htxF6ctXHX\&.
.fi
.RE
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1), \fIoddmu-apache\fR(5)
.PP
"freenginx"
http://freenginx.\&org/
.PP
"freenginx ngx_http_proxy_module", proxy_pass
http://freenginx.\&org/en/docs/http/ngx_http_proxy_module.\&html#proxy_pass
.PP
"freenginx ngx_http_auth_basic_module"
http://freenginx.\&org/en/docs/http/ngx_http_auth_basic_module.\&html
.PP
.SH AUTHORS
.PP
Maintained by Alex Schroeder <alex@gnu.\&org>.\&

76
man/oddmu-nginx.5.txt Normal file
View File

@@ -0,0 +1,76 @@
ODDMU-NGINX(5)
# NAME
oddmu-nginx - how to setup Nginx as a reverse proxy for Oddmu
## DESCRIPTION
The oddmu program serves the current working directory as a wiki on port 8080.
This is an unpriviledged port so an ordinary user account can do this.
This page explains how to setup NGINX on Debian to act as a reverse proxy for
Oddmu. Once this is done, you can use NGINX to provide HTTPS, request users to
authenticate themselves, and so on.
## CONFIGURATION
The site is defined in "/etc/nginx/sites-available/default", in the _server_
section. Add a new _location_ section after the existing _location_ section:
```
location ~ ^/(view|diff|edit|save|add|append|upload|drop|search|archive)/ {
proxy_pass http://localhost:8080;
}
```
If you remove an action from the regular expression, those requests no longer
get passed on to Oddmu. They are essentially disabled. Somebody on the same
machine pointing their browser at http://localhost:8080/ directly would still
have access to all the actions, of course.
To restrict access to some actions, use two different _location_ sections:
```
# public
location ~ ^/(view|diff|search)/ {
proxy_pass http://localhost:8080;
}
# password required
location ~ ^/(edit|save|add|append|upload|drop|archive)/ {
auth_basic "Oddmu author";
auth_basic_user_file /etc/nginx/conf.d/htpasswd;
proxy_pass http://localhost:8080;
}
```
The passwords in "/etc/nginx/conf.d/htpasswd" are generated using _openssl_(1).
Assuming the password is "CPTk&qO[Y@?M~L>qKOkd", this is how you encrypt it:
```
openssl passwd 'CPTk&qO[Y@?M~L>qKOkd'
```
The output gets used in "/etc/nginx/conf.d/htpasswd". Here's the user "alex"
using this password:
```
alex:$1$DOwphABk$W4VmR9p8t2.htxF6ctXHX.
```
# SEE ALSO
_oddmu_(1), _oddmu-apache_(5)
"freenginx"
http://freenginx.org/
"freenginx ngx_http_proxy_module", proxy_pass
http://freenginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
"freenginx ngx_http_auth_basic_module"
http://freenginx.org/en/docs/http/ngx_http_auth_basic_module.html
# AUTHORS
Maintained by Alex Schroeder <alex@gnu.org>.

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-RELEASES" "7" "2024-02-17"
.TH "ODDMU-RELEASES" "7" "2024-02-26"
.PP
.SH NAME
.PP
@@ -13,9 +13,26 @@ oddmu-releases - what'\&s new in this releases?\&
.PP
.SH DESCRIPTION
.PP
This page lists user-visible features.\&
This page lists user-visible features and template changes to consider.\&
.PP
.SS 1.6 (unrelease)
.SS 1.8 (2024)
.PP
No user-visible changes.\& Documentation and code comments got better.\&
.PP
.SS 1.7 (2024)
.PP
Allow upload of multiple files.\& This requires an update to the \fIupload.\&html\fR
template: Add the \fImultiple\fR attribute to the file input element and change the
label from "file" to "files".\&
.PP
Fix orientation of uploaded images.\& JPG and HEIC images have EXIF data telling a
viewer how to orient the image.\& Oddmu now uses this information to rotate the
image correctly before stripping it.\&
.PP
The version command now displays much less information unless given the -full
argument.\&
.PP
.SS 1.6 (2024)
.PP
Add \fIarchive\fR action to serve a zip file.\&
.PP

View File

@@ -6,11 +6,24 @@ oddmu-releases - what's new in this releases?
# DESCRIPTION
This page lists user-visible features.
This page lists user-visible features and template changes to consider.
## Next
## 1.8 (2024)
No user-visible changes. Documentation and code comments got better.
## 1.7 (2024)
Allow upload of multiple files. This requires an update to the _upload.html_
template: Add the _multiple_ attribute to the file input element and change the
label from "file" to "files".
Fix orientation of uploaded images. JPG and HEIC images have EXIF data telling a
viewer how to orient the image. Oddmu now uses this information to rotate the
image correctly before stripping it.
The version command now displays much less information unless given the -full
argument.
## 1.6 (2024)

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-SEARCH" "7" "2024-02-17"
.TH "ODDMU-SEARCH" "7" "2024-02-19"
.PP
.SH NAME
.PP
@@ -95,12 +95,12 @@ directory that doesn'\&t match, subdirectories that do match are skipped.\& See
\fIoddmu-filter\fR(7).\&
.PP
To prevent access to a private directory tree, you must configure the web server
in addition to setting the ODDMU_FILTER environment variable.\& See
\fIoddmu-apache\fR(5) for more.\&
in addition to setting the ODDMU_FILTER environment variable.\&
.PP
.SH SEE ALSO
.PP
\fIoddmu\fR(1), \fIoddmu-search\fR(1), \fIoddmu-filter\fR(7), \fIoddmu-apache\fR(5)
\fIoddmu\fR(1), \fIoddmu-search\fR(1), \fIoddmu-filter\fR(7), \fIoddmu-apache\fR(5),
\fIoddmu-nginx\fR(5)
.PP
.SH AUTHORS
.PP

View File

@@ -75,12 +75,12 @@ directory that doesn't match, subdirectories that do match are skipped. See
_oddmu-filter_(7).
To prevent access to a private directory tree, you must configure the web server
in addition to setting the ODDMU_FILTER environment variable. See
_oddmu-apache_(5) for more.
in addition to setting the ODDMU_FILTER environment variable.
# SEE ALSO
_oddmu_(1), _oddmu-search_(1), _oddmu-filter_(7), _oddmu-apache_(5)
_oddmu_(1), _oddmu-search_(1), _oddmu-filter_(7), _oddmu-apache_(5),
_oddmu-nginx_(5)
# AUTHORS

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-STATIC" "1" "2024-02-17"
.TH "ODDMU-STATIC" "1" "2024-02-26"
.PP
.SH NAME
.PP

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU-VERSION" "1" "2024-02-17"
.TH "ODDMU-VERSION" "1" "2024-02-23"
.PP
.SH NAME
.PP
@@ -13,22 +13,22 @@ oddmu-version - print build info on the command-line
.PP
.SH SYNOPSIS
.PP
\fBoddmu version\fR
\fBoddmu version\fR [-full]
.PP
.SH DESCRIPTION
.PP
The "version" subcommand prints a lot of stuff used to build the binary,
including the git revision, git repository, versions of dependencies used and
more.\&
The "version" subcommand prints information related to the version control
system state when it was built: what remote was used, what commit was checked
out, whether there were any local changes were made.\&
.PP
It'\&s the equivalent of running this:
.SH OPTIONS
.PP
.nf
\fB-full\fR
.RS 4
go version -m oddmu
.fi
.RE
Print a lot more information, including the versions of dependencies
used.\& It'\&s the equivalent of running "go version -m oddmu".\&
.PP
.RE
.SH SEE ALSO
.PP
\fIoddmu\fR(1)

View File

@@ -6,19 +6,19 @@ oddmu-version - print build info on the command-line
# SYNOPSIS
*oddmu version*
*oddmu version* [-full]
# DESCRIPTION
The "version" subcommand prints a lot of stuff used to build the binary,
including the git revision, git repository, versions of dependencies used and
more.
The "version" subcommand prints information related to the version control
system state when it was built: what remote was used, what commit was checked
out, whether there were any local changes were made.
It's the equivalent of running this:
# OPTIONS
```
go version -m oddmu
```
*-full*
Print a lot more information, including the versions of dependencies
used. It's the equivalent of running "go version -m oddmu".
# SEE ALSO

View File

@@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "ODDMU" "1" "2024-02-17"
.TH "ODDMU" "1" "2024-02-23"
.PP
.SH NAME
.PP
@@ -350,35 +350,49 @@ Note that some HTML file names are special: they act as templates.\& See
.PP
.PD 0
.IP \(bu 4
\fIoddmu\fR(5), about the markup syntax and how feeds are generated based on link lists
\fIoddmu\fR(5), about the markup syntax and how feeds are generated based on link
lists
.IP \(bu 4
\fIoddmu.\&service\fR(5), on how to run the service under systemd
.IP \(bu 4
\fIoddmu-apache\fR(5), on how to set up a web server such as Apache
\fIoddmu-releases\fR(7), on what features are part of the latest release
.IP \(bu 4
\fIoddmu-filter\fR(7), on how to treat subdirectories as separate sites
.IP \(bu 4
\fIoddmu-search\fR(7), on how search works
.IP \(bu 4
\fIoddmu-templates\fR(5), on how to write the HTML templates
.PD
.PP
If you run Oddmu as a web server:
.PP
.PD 0
.IP \(bu 4
\fIoddmu-apache\fR(5), on how to set up Apache as a reverse proxy
.IP \(bu 4
\fIoddmu-nginx\fR(5), on how to set up freenginx as a reverse proxy
.IP \(bu 4
\fIoddmu.\&service\fR(5), on how to run the service under systemd
.PD
.PP
If you run Oddmu as a static site generator or pages offline and sync them with
Oddmu running as a webserver:
.PP
.PD 0
.IP \(bu 4
\fIoddmu-html\fR(1), on how to render a page from the command-line
.IP \(bu 4
\fIoddmu-list\fR(1), on how to list pages and titles from the command-line
.IP \(bu 4
\fIoddmu-missing\fR(1), on how to find broken local links from the command-line
.IP \(bu 4
\fIoddmu-releases\fR(7), on what features are part of the latest release
\fIoddmu-notify\fR(1), on updating index, changes and hashtag pages from the
command-line
.IP \(bu 4
\fIoddmu-replace\fR(1), on how to search and replace text from the command-line
.IP \(bu 4
\fIoddmu-search\fR(1), on how to run a search from the command-line
.IP \(bu 4
\fIoddmu-search\fR(7), on how search works
.IP \(bu 4
\fIoddmu-static\fR(1), on generating a static site from the command-line
.IP \(bu 4
\fIoddmu-notify\fR(1), on updating index, changes and hashtag pages from the
command-line
.IP \(bu 4
\fIoddmu-templates\fR(5), on how to write the HTML templates
.IP \(bu 4
\fIoddmu-version\fR(1), on how to get all the build information from the binary
.PD
.PP

View File

@@ -293,21 +293,30 @@ _oddmu-templates_(5) for their names and their use.
# SEE ALSO
- _oddmu_(5), about the markup syntax and how feeds are generated based on link lists
- _oddmu.service_(5), on how to run the service under systemd
- _oddmu-apache_(5), on how to set up a web server such as Apache
- _oddmu_(5), about the markup syntax and how feeds are generated based on link
lists
- _oddmu-releases_(7), on what features are part of the latest release
- _oddmu-filter_(7), on how to treat subdirectories as separate sites
- _oddmu-search_(7), on how search works
- _oddmu-templates_(5), on how to write the HTML templates
If you run Oddmu as a web server:
- _oddmu-apache_(5), on how to set up Apache as a reverse proxy
- _oddmu-nginx_(5), on how to set up freenginx as a reverse proxy
- _oddmu.service_(5), on how to run the service under systemd
If you run Oddmu as a static site generator or pages offline and sync them with
Oddmu running as a webserver:
- _oddmu-html_(1), on how to render a page from the command-line
- _oddmu-list_(1), on how to list pages and titles from the command-line
- _oddmu-missing_(1), on how to find broken local links from the command-line
- _oddmu-releases_(7), on what features are part of the latest release
- _oddmu-replace_(1), on how to search and replace text from the command-line
- _oddmu-search_(1), on how to run a search from the command-line
- _oddmu-search_(7), on how search works
- _oddmu-static_(1), on generating a static site from the command-line
- _oddmu-notify_(1), on updating index, changes and hashtag pages from the
command-line
- _oddmu-templates_(5), on how to write the HTML templates
- _oddmu-replace_(1), on how to search and replace text from the command-line
- _oddmu-search_(1), on how to run a search from the command-line
- _oddmu-static_(1), on generating a static site from the command-line
- _oddmu-version_(1), on how to get all the build information from the binary
# AUTHORS

View File

@@ -3,17 +3,17 @@ package main
import (
"github.com/stretchr/testify/assert"
"io/fs"
"path/filepath"
"os"
"path/filepath"
"strings"
"testing"
)
func TestManPages(t *testing.T) {
b, err := os.ReadFile("man/oddmu.1.txt");
b, err := os.ReadFile("man/oddmu.1.txt")
main := string(b)
assert.NoError(t, err)
filepath.Walk("man", func (path string, info fs.FileInfo, err error) error {
filepath.Walk("man", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
@@ -29,3 +29,34 @@ func TestManPages(t *testing.T) {
})
}
func TestReadme(t *testing.T) {
b, err := os.ReadFile("README.md")
main := string(b)
assert.NoError(t, err)
filepath.Walk("man", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".txt") {
s := strings.TrimPrefix(path, "man/")
s = strings.TrimSuffix(s, ".txt")
i := strings.LastIndex(s, ".")
ref := "[" + s[:i] + "(" + s[i+1:] + ")]"
assert.Contains(t, main, ref, ref)
}
return nil
})
filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".go") &&
!strings.HasSuffix(path, "_test.go") &&
!strings.HasSuffix(path, "_cmd.go") {
s := strings.TrimPrefix(path, "./")
ref := "`" + s + "`"
assert.Contains(t, main, ref, ref)
}
return nil
})
}

View File

@@ -88,7 +88,7 @@ func (p *Page) save() error {
// to it ("~"). This is true even if the file refers to a binary file like "image.png" and most applications don't know
// what to do with a file called "image.png~". This expects a file path. Use filepath.FromSlash(path) if necessary.
func backup(fp string) error {
_, err := os.Stat(fp)
_, err := os.Stat(fp)
if err != nil {
return nil
}
@@ -105,7 +105,7 @@ func backup(fp string) error {
// undefined (there is no caching).
func loadPage(path string) (*Page, error) {
path = strings.TrimPrefix(path, "./") // result of a filepath.TreeWalk starting with "."
body, err := os.ReadFile(filepath.FromSlash(path+".md"))
body, err := os.ReadFile(filepath.FromSlash(path + ".md"))
if err != nil {
return nil, err
}

View File

@@ -68,7 +68,7 @@ func wikiParser() (*parser.Parser, *[]string) {
fn, hashtags := hashtag()
parser.RegisterInline('#', fn)
if useWebfinger {
parser.RegisterInline('@', account)
parser.RegisterInline('@', accountLink)
}
return parser, hashtags
}

View File

@@ -17,15 +17,19 @@ import (
var templateFiles = []string{"edit.html", "add.html", "view.html",
"diff.html", "search.html", "static.html", "upload.html", "feed.html"}
// templates are the parsed HTML templates used. See renderTemplate and loadTemplates. Subdirectories may contain their
// own templates which override the templates in the root directory. If so, they are not filepaths. Use
// filepath.ToSlash() if necessary.
type Template struct {
// templateStore controls access to map of parsed HTML templates. Make sure to lock and unlock as appropriate. See
// renderTemplate and loadTemplates.
type templateStore struct {
sync.RWMutex
// template is a map of parsed HTML templates. The key is their path name. By default, the map only contains
// top-level templates like "view.html". Subdirectories may contain their own templates which override the
// templates in the root directory. If so, they are paths like "dir/view.html", not filepaths. Use
// filepath.ToSlash() if necessary.
template map[string]*template.Template
}
var templates Template
var templates templateStore
// loadTemplates loads the templates. If templates have already been loaded, return immediately.
func loadTemplates() {

View File

@@ -14,7 +14,7 @@ label { display: inline-block; width: 20ch }
</style>
</head>
<body lang="en">
<h1>Upload File</h1>
<h1>Upload Files</h1>
{{if ne .Last ""}}
<p>Previous upload: <a href="/view/{{.Last}}">{{.Last}}</a></p>
{{if .Image}}
@@ -23,23 +23,24 @@ label { display: inline-block; width: 20ch }
{{end}}
<form action="/drop/{{.Dir}}" method="POST" enctype="multipart/form-data">
<p>When uploading pictures from a phone, its filename is going to be something cryptic like IMG_1234.JPG.
Please provide your own filename. End the filename with "-1" to auto-increment.
Please provide your own filename. End the base name with "-1" to auto-increment.
Use <tt>.jpg</tt> or <tt>.png</tt> as the extension.
<p><label for="text">Filename to use:</label>
<input id="text" name="name" value="{{.Name}}" type="text" placeholder="image-1.jpg" autofocus required>
<p>If the uploaded file is a picture from a phone, it is going to be too big for your site.
Sadly, resizing only works for JPG and PNG files. Luckily, most pictures from a phone camera are JPG images.
Sadly, resizing only works when uploading <tt>.jpeg</tt>, <tt>.heic</tt> and <tt>.png</tt> files.
Feel free to specify a max width of 1200 pixels, for example.
<p><label for="maxwidth">Max width:</label>
<input id="maxwidth" name="maxwidth" value="{{.MaxWidth}}" type="number" min="10" placeholder="1200">
<p>If the uploaded file is a JPEG-encoded picture, like most pictures from a phone, you can specify a quality.
<p>If the filename you provided above ends in <tt>.jpg</tt>, you can specify a quality.
Typically, a quality of 60 is not too bad and a quality of 90 is more than enough.
<p><label for="quality">Quality:</label>
<input id="quality" name="quality" value="{{.Quality}}" type="number" min="1" max="99" placeholder="75">
<p>Finally, pick the file or photo to upload.
Picture metadata is only removed if the picture gets resized.
<p>Finally, pick the files or photos to upload.
Picture metadata is only removed if the pictures gets resized.
Providing a new max width is recommended for all pictures.
<p><label for="file">Pick file to upload:</label>
<input type="file" name="file" required>
<p><label for="file">Pick files to upload:</label>
<input type="file" name="file" required multiple>
<p><input type="submit" value="Save">
<a href="/view/index"><button type="button">Cancel</button></a></p>
</form>

View File

@@ -1,11 +1,12 @@
package main
// The imaging library uses image.Decode internally. This function can use all image decoders available at that time.
// This is why we import goheif for side effects: HEIC files are read correctly.
import (
"github.com/anthonynsimon/bild/imgio"
"github.com/anthonynsimon/bild/transform"
"github.com/bashdrew/goheif"
"image/jpeg"
"image/png"
_ "github.com/bashdrew/goheif"
"github.com/disintegration/imaging"
"github.com/edwvee/exiffix"
"io"
"log"
"net/http"
@@ -18,7 +19,7 @@ import (
"strings"
)
type Upload struct {
type upload struct {
Dir string
Name string
Last string
@@ -33,7 +34,7 @@ var lastRe = regexp.MustCompile(`^(.*)([0-9]+)(.*)$`)
// parameters are used to copy name, maxwidth and quality from the previous upload. If the previous name contains a
// number, this is incremented by one.
func uploadHandler(w http.ResponseWriter, r *http.Request, dir string) {
data := &Upload{Dir: dir}
data := &upload{Dir: dir}
maxwidth := r.FormValue("maxwidth")
if maxwidth != "" {
data.MaxWidth = maxwidth
@@ -52,17 +53,24 @@ func uploadHandler(w http.ResponseWriter, r *http.Request, dir string) {
data.Image = true
}
data.Last = path.Join(dir, last)
m := lastRe.FindStringSubmatch(last)
if m != nil {
n, err := strconv.Atoi(m[2])
if err == nil {
data.Name = m[1] + strconv.Itoa(n+1) + m[3]
}
}
data.Name, _ = next(last)
}
renderTemplate(w, dir, "upload", data)
}
// next returns the next name for a string matching lastRe. The last number in the given string is incremented by one
// ("a2b" → "a3b"). The second return value indicates whether such a replacement was made or not.
func next(s string) (string, bool) {
m := lastRe.FindStringSubmatch(s)
if m != nil {
n, err := strconv.Atoi(m[2])
if err == nil {
return m[1] + strconv.Itoa(n+1) + m[3], true
}
}
return s, false
}
// dropHandler takes the "name" form field and the "file" form file and saves the file under the given name. The browser
// is redirected to the view of that file. Some errors are for the users and some are for users and the admins. Those
// later errors are printed, too.
@@ -80,116 +88,125 @@ func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
}
data := url.Values{}
name := r.FormValue("name")
data.Set("last", name)
filename := filepath.Base(name)
// no overwriting of hidden files or adding subdirectories
if strings.HasPrefix(filename, ".") || filepath.Dir(name) != "." {
http.Error(w, "no filename", http.StatusForbidden)
return
}
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
path := filepath.Join(d, filename)
watches.ignore(path)
err = backup(path)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dst, err := os.Create(path)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
// if a resize was requested
// prepare for image encoding (saving) with the encoder based on the desired file name extensions
var format imaging.Format
quality := 75
maxwidth := r.FormValue("maxwidth")
mw := 0
if len(maxwidth) > 0 {
mw, err := strconv.Atoi(maxwidth)
mw, err = strconv.Atoi(maxwidth)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data.Add("maxwidth", maxwidth)
// determine how the file will be written
ext := strings.ToLower(filepath.Ext(path))
var encoder imgio.Encoder
ext := strings.ToLower(filepath.Ext(filename))
switch ext {
case ".png":
encoder = imgio.PNGEncoder()
format = imaging.PNG
case ".jpg", ".jpeg":
q := jpeg.DefaultQuality
quality := r.FormValue("quality")
if len(quality) > 0 {
q, err = strconv.Atoi(quality)
q := r.FormValue("quality")
if len(q) > 0 {
quality, err = strconv.Atoi(q)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data.Add("quality", quality)
data.Add("quality", q)
}
encoder = imgio.JPEGEncoder(q)
format = imaging.JPEG
default:
http.Error(w, "Resizing images requires a .png, .jpg or .jpeg extension for the filename", http.StatusBadRequest)
return
}
// try and decode the data in various formats
img, err := jpeg.Decode(file)
}
first := true
for _, fhs := range r.MultipartForm.File["file"] {
file, err := fhs.Open()
if err != nil {
img, err = png.Decode(file)
}
if err != nil {
img, err = goheif.Decode(file)
}
if err != nil {
http.Error(w, "The image could not be decoded (only PNG, JPG and HEIC formats are supported for resizing)", http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rect := img.Bounds()
width := rect.Max.X - rect.Min.X
if width > mw {
height := (rect.Max.Y - rect.Min.Y) * mw / width
img = transform.Resize(img, mw, height, transform.Linear)
if err := imgio.Save(path, img, encoder); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
defer file.Close()
if !first {
s, ok := next(filename)
if ok {
filename = s
} else {
ext := filepath.Ext(s)
filename = s[:len(s)-len(ext)] + "-1" + ext
}
} else {
http.Error(w, "The file is too small for this", http.StatusBadRequest)
return
}
} else {
// just copy the bytes
n, err := io.Copy(dst, file)
first = false
path := filepath.Join(d, filename)
watches.ignore(path)
err = backup(path)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// if zero bytes were copied, delete the file instead
if n == 0 {
err := os.Remove(path)
dst, err := os.Create(path)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
if mw > 0 {
// do not use imaging.Decode(file, imaging.AutoOrientation(true)) because that only works for JPEG files
img, fmt, err := exiffix.Decode(file)
if err != nil {
http.Error(w, "The image could not be decoded (only PNG, JPG and HEIC formats are supported for resizing)", http.StatusBadRequest)
return
}
log.Println("Decoded", fmt, "file")
res := imaging.Resize(img, mw, 0, imaging.Lanczos) // preserve aspect ratio
// imaging functions don't return errors but empty images…
if !res.Rect.Empty() {
img = res
}
// images are always reencoded, so image quality goes down
err = imaging.Encode(dst, img, format, imaging.JPEGQuality(quality))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Println("Delete", path)
} else {
// just copy the bytes
n, err := io.Copy(dst, file)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// if zero bytes were copied, delete the file instead
if n == 0 {
err := os.Remove(path)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Println("Delete", path)
}
}
username, _, ok := r.BasicAuth()
if ok {
log.Println("Save", path, "by", username)
} else {
log.Println("Save", path)
}
updateTemplate(path)
}
username, _, ok := r.BasicAuth()
if ok {
log.Println("Save", path, "by", username)
} else {
log.Println("Save", path)
}
updateTemplate(path)
data.Set("last", filename) // has no slashes
http.Redirect(w, r, "/upload/"+dir+"?"+data.Encode(), http.StatusFound)
}

View File

@@ -2,6 +2,7 @@ package main
import (
"bytes"
"encoding/base64"
"github.com/stretchr/testify/assert"
"image"
"image/jpeg"
@@ -68,6 +69,34 @@ func TestUploadJpg(t *testing.T) {
writer.FormDataContentType(), form, "/upload/testdata/jpg/?last=ok.jpg")
}
func TestUploadHeic(t *testing.T) {
cleanup(t, "testdata/heic")
// for uploads, the directory is not created automatically
os.MkdirAll("testdata/heic", 0755)
form := new(bytes.Buffer)
writer := multipart.NewWriter(form)
field, _ := writer.CreateFormField("name")
field.Write([]byte("ok.jpg")) // target
file, _ := writer.CreateFormFile("file", "ok.heic") // source
// convert -size 1x1 canvas: heic:- | base64
imgBase64 := `
AAAAGGZ0eXBoZWljAAAAAG1pZjFoZWljAAABqm1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAHBpY3QA
AAAAAAAAAAAAAAAAAAAADnBpdG0AAAAAAAIAAAAQaWRhdAAAAAAAAQABAAAAOGlsb2MBAAAAREAA
AgABAAAAAAAAAcoAAQAAAAAAAAAtAAIAAQAAAAAAAAABAAAAAAAAAAgAAAA4aWluZgAAAAAAAgAA
ABVpbmZlAgAAAQABAABodmMxAAAAABVpbmZlAgAAAAACAABncmlkAAAAANVpcHJwAAAAs2lwY28A
AABzaHZjQwEDcAAAAAAAAAAAAB7wAPz9+PgAAA8DIAABABhAAQwB//8DcAAAAwCQAAADAAADAB66
AkAhAAEAJ0IBAQNwAAADAJAAAAMAAAMAHqAggQWW6q6a5sCAAAADAIAAAAMAhCIAAQAGRAHBc8GJ
AAAAFGlzcGUAAAAAAAAAQAAAAEAAAAAUaXNwZQAAAAAAAAABAAAAAQAAABBwaXhpAAAAAAMICAgA
AAAaaXBtYQAAAAAAAAACAAECgQIAAgIDhAAAABppcmVmAAAAAAAAAA5kaW1nAAIAAQABAAAANW1k
YXQAAAApKAGvEyE1mvXho5qH3STtzcWnOxedwNIXAKNDaJNqz3uONoCHeUhi/HA=`
img, err := base64.StdEncoding.DecodeString(imgBase64)
assert.NoError(t, err)
file.Write(img)
writer.Close()
HTTPUploadAndRedirectTo(t, makeHandler(dropHandler, false), "/drop/testdata/heic/",
writer.FormDataContentType(), form, "/upload/testdata/heic/?last=ok.jpg")
}
func TestDeleteFile(t *testing.T) {
cleanup(t, "testdata/delete")
os.MkdirAll("testdata/delete", 0755)
@@ -176,3 +205,52 @@ There is no answer`)}
body = assert.HTTPBody(makeHandler(uploadHandler, false), "GET", url.Path, values)
assert.Contains(t, body, `src="/view/testdata/dir/test.jpg"`)
}
func TestUploadTwoInOne(t *testing.T) {
cleanup(t, "testdata/two")
os.MkdirAll("testdata/two", 0755)
form := new(bytes.Buffer)
writer := multipart.NewWriter(form)
field, _ := writer.CreateFormField("name")
field.Write([]byte("2024-02-19-hike-1.jpg"))
file1, _ := writer.CreateFormFile("file", "one.jpg")
img1 := image.NewRGBA(image.Rect(0, 0, 10, 10))
jpeg.Encode(file1, img1, &jpeg.Options{Quality: 90})
file2, _ := writer.CreateFormFile("file", "two.jpg")
img2 := image.NewRGBA(image.Rect(0, 0, 20, 20))
jpeg.Encode(file2, img2, &jpeg.Options{Quality: 90})
writer.Close()
location := HTTPUploadLocation(t, makeHandler(dropHandler, false), "/drop/testdata/two/",
writer.FormDataContentType(), form)
url, _ := url.Parse(location)
assert.Equal(t, "/upload/testdata/two/", url.Path, "Redirect to upload location")
values := url.Query()
assert.Equal(t, "2024-02-19-hike-2.jpg", values.Get("last"))
// check the files
assert.FileExists(t, "testdata/two/2024-02-19-hike-1.jpg")
assert.FileExists(t, "testdata/two/2024-02-19-hike-2.jpg")
}
func TestUploadTwoInOneAgain(t *testing.T) {
cleanup(t, "testdata/zwei")
os.MkdirAll("testdata/zwei", 0755)
form := new(bytes.Buffer)
writer := multipart.NewWriter(form)
field, _ := writer.CreateFormField("name")
field.Write([]byte("image.jpg")) // cannot be incremented!
file1, _ := writer.CreateFormFile("file", "one.jpg")
img1 := image.NewRGBA(image.Rect(0, 0, 10, 10))
jpeg.Encode(file1, img1, &jpeg.Options{Quality: 90})
file2, _ := writer.CreateFormFile("file", "two.jpg")
img2 := image.NewRGBA(image.Rect(0, 0, 20, 20))
jpeg.Encode(file2, img2, &jpeg.Options{Quality: 90})
writer.Close()
location := HTTPUploadLocation(t, makeHandler(dropHandler, false), "/drop/testdata/zwei/",
writer.FormDataContentType(), form)
url, _ := url.Parse(location)
assert.Equal(t, "/upload/testdata/zwei/", url.Path, "Redirect to upload location")
values := url.Query()
assert.Equal(t, "image-1.jpg", values.Get("last"))
// check the files
assert.FileExists(t, "testdata/zwei/image.jpg")
assert.FileExists(t, "testdata/zwei/image-1.jpg")
}

View File

@@ -8,32 +8,43 @@ import (
"io"
"os"
"runtime/debug"
"strings"
)
type versionCmd struct {
full bool
}
func (cmd *versionCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&cmd.full, "full", false, "show all the debug information")
}
func (*versionCmd) Name() string { return "version" }
func (*versionCmd) Synopsis() string { return "report build information" }
func (*versionCmd) Usage() string {
return `version:
Report all the debug information about this build.
return `version [-full]:
Report the exact version control commit this is built from, or the
full debug information about this build.
`
}
func (cmd *versionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
return versionCli(os.Stdout, f.Args())
return versionCli(os.Stdout, cmd.full, f.Args())
}
func versionCli(w io.Writer, args []string) subcommands.ExitStatus {
if len(args) > 0 {
fmt.Fprintln(os.Stderr, "Version takes no arguments.")
return subcommands.ExitFailure
func versionCli(w io.Writer, full bool, args []string) subcommands.ExitStatus {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("This binary contains no debug info.")
} else if full {
fmt.Println(info)
} else {
fmt.Println(info.Path)
for _, setting := range info.Settings {
if strings.HasPrefix(setting.Key, "vcs") {
fmt.Printf("%s=%s\n", setting.Key, setting.Value)
}
}
}
info, _ := debug.ReadBuildInfo()
fmt.Println(info)
return subcommands.ExitSuccess
}

View File

@@ -9,7 +9,7 @@ import (
func TestVersionCmd(t *testing.T) {
b := new(bytes.Buffer)
s := versionCli(b, nil)
s := versionCli(b, false, nil)
assert.Equal(t, subcommands.ExitSuccess, s)
assert.Contains(t, "vcs.revision", b.String())
}

10
view.go
View File

@@ -46,7 +46,7 @@ func viewHandler(w http.ResponseWriter, r *http.Request, path string) {
t = rss
}
fp := filepath.FromSlash(path)
fi, err := os.Stat(fp+".md")
fi, err := os.Stat(fp + ".md")
if err == nil {
if fi.IsDir() {
t = dir // directory ending in ".md"
@@ -55,6 +55,10 @@ func viewHandler(w http.ResponseWriter, r *http.Request, path string) {
}
// otherwise t == rss
} else {
if t == rss {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if fp == "" {
fp = "." // make sure Stat works
}
@@ -107,10 +111,6 @@ func viewHandler(w http.ResponseWriter, r *http.Request, path string) {
}
p, err := loadPage(path)
if err != nil {
if t == rss {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Redirect(w, r, "/edit/"+path, http.StatusFound)
return
}

View File

@@ -1,29 +1,44 @@
package main
import (
"github.com/fsnotify/fsnotify"
"github.com/fsnotify/fsnotify"
"io/fs"
"log"
"log"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
"slices"
"strings"
"sync"
"time"
)
// Watches holds a map and a mutex. The map contains the template names that have been requested and the exact time at
// which they have been requested. Adding the same file multiple times, such as when the watch function sees multiple
// Write events for the same file, the time keeps getting updated so that when the go routine runs, it only acts on
// files that haven't been updated in the last second. The go routine is what forces us to use the RWMutex for the map.
type Watches struct {
sync.RWMutex
// watchStore controls access to the maps used by the filesystem watches. Make sure to lock and unlock as appropriate.
// The maps are used to control a sort of queue for files that need reloading (if a template) or reindexing (if a page).
// File system notifications add files to the queue in order to handle changes made without Oddmu, while Oddmu is
// running.
type watchStore struct {
sync.RWMutex
// files contains the filenames that have been queued for reloading (if a template) or reindexing (if a page)
// and the exact time at which they have been added. When the same file is added multiple times, such as when
// the watchStore function sees multiple Write events for the same file, the time keeps getting updated so that
// when the watchTimer runs, it only acts on files that haven't been updated in the last second.
files map[string]time.Time
// ignores contains the files that some code intends to change, knowing that subsequent writes events would
// result in file system notifications that would end up adding the filenames to the queue for reloading (if a
// template) or reindexing (if a page). When Oddmu is making the changes, it can ignore the corresponding
// notifications by the file system. Those notifications are consequences of Oddmu doing its job. In other
// words, Oddmu does not rely on file system notification even it is Oddmu doing the changes. This avoids a 1s
// when changing templates, for example.
ignores map[string]time.Time
files map[string]time.Time
// watcher is the pointer to the actual watcher doing the file system watching. It watches a set of paths.
// Whenever Oddmu creates a new subdirectory, it adds the path for this subdirectory to the watcher.
watcher *fsnotify.Watcher
}
var watches Watches
var watches watchStore
func init() {
watches.ignores = make(map[string]time.Time)
@@ -31,7 +46,7 @@ func init() {
}
// install initializes watches and installs watchers for all directories and subdirectories.
func (w *Watches) install() (int, error) {
func (w *watchStore) install() (int, error) {
// create a watcher for the root directory and never close it
var err error
w.watcher, err = fsnotify.NewWatcher()
@@ -48,7 +63,7 @@ func (w *Watches) install() (int, error) {
}
// add installs a watch for every directory that isn't hidden. Note that the root directory (".") is not skipped.
func (w *Watches) add(path string, info fs.FileInfo, err error) error {
func (w *watchStore) add(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
@@ -72,7 +87,7 @@ func (w *Watches) add(path string, info fs.FileInfo, err error) error {
// adds an entry to the files map, or updates the file's time, and starts a go routine. Example: If a file gets three
// consecutive Write events, the first two go routine invocations won't do anything, since the time kept getting
// updated. Only the last invocation will act upon the event.
func (w *Watches) watch() {
func (w *watchStore) watch() {
for {
select {
case err, ok := <-w.watcher.Errors:
@@ -94,15 +109,15 @@ func (w *Watches) watch() {
// Incidentally, this also prevents rsync updates from generating activity ("stat ./.index.md.tTfPFg: no such file or
// directory"). Note the painful details: If moving a file into a watched directory, a Create event is received. If a
// new file is created in a watched directory, a Create event and one or more Write events is received.
func (w *Watches) watchHandle(e fsnotify.Event) {
func (w *watchStore) watchHandle(e fsnotify.Event) {
path := strings.TrimPrefix(e.Name, "./")
if strings.HasPrefix(filepath.Base(path), ".") {
return;
return
}
// log.Println(e)
w.Lock()
defer w.Unlock()
if e.Op.Has(fsnotify.Create | fsnotify.Write) &&
if e.Op.Has(fsnotify.Create|fsnotify.Write) &&
(strings.HasSuffix(path, ".html") &&
slices.Contains(templateFiles, filepath.Base(path)) ||
strings.HasSuffix(path, ".md")) {
@@ -130,7 +145,7 @@ func (w *Watches) watchHandle(e fsnotify.Event) {
// watchTimer checks if the file hasn't been updated in 1s and if so, it calls watchDoUpdate. If another write has
// updated the file, do nothing because another watchTimer will run at the appropriate time and check again.
func (w *Watches) watchTimer(path string) {
func (w *watchStore) watchTimer(path string) {
t, ok := w.files[path]
if ok && t.Add(time.Second).Before(time.Now().Add(time.Nanosecond)) {
delete(w.files, path)
@@ -141,7 +156,7 @@ func (w *Watches) watchTimer(path string) {
// Do the right thing right now. For Create events such as directories being created or files being moved into a watched
// directory, this is the right thing to do. When a file is being written to, watchHandle will have started a timer and
// will call this function after 1s of no more writes. If, however, the path is in the ignores map, do nothing.
func (w *Watches) watchDoUpdate(path string) {
func (w *watchStore) watchDoUpdate(path string) {
_, ignored := w.ignores[path]
if ignored {
return
@@ -170,7 +185,7 @@ func (w *Watches) watchDoUpdate(path string) {
// watchDoRemove removes files from the index or discards templates. If the path in question is in the ignores map, do
// nothing.
func (w *Watches) watchDoRemove(path string) {
func (w *watchStore) watchDoRemove(path string) {
_, ignored := w.ignores[path]
if ignored {
return
@@ -189,7 +204,7 @@ func (w *Watches) watchDoRemove(path string) {
// ignore is before code that is known suspected save files and trigger watchHandle eventhough the code already handles
// this. This is achieved by adding the path to the ignores map for 1s.
func (w *Watches) ignore(path string) {
func (w *watchStore) ignore(path string) {
w.Lock()
defer w.Unlock()
w.ignores[path] = time.Now()

View File

@@ -4,7 +4,7 @@ import (
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
"time"
)
func TestWatchedPageUpdate(t *testing.T) {
@@ -64,7 +64,7 @@ the smell is everywhere
assert.Contains(t,
assert.HTTPBody(makeHandler(viewHandler, true), "GET", "/view/testdata/watched-template/raclette", nil),
"Skip navigation")
// save a new view handler directly
assert.NoError(t,
os.WriteFile(path,
@@ -82,8 +82,8 @@ the smell is everywhere
watches.Unlock()
watches.watchTimer(path)
body := assert.HTTPBody(makeHandler(viewHandler, true), "GET", "/view/" + name, nil)
body := assert.HTTPBody(makeHandler(viewHandler, true), "GET", "/view/"+name, nil)
assert.Contains(t, body, "<h1>Raclette</h1>") // page text is still there
assert.NotContains(t, body, "Skip") // but the header is not
assert.NotContains(t, body, "Skip") // but the header is not
}

23
wiki.go
View File

@@ -1,3 +1,6 @@
// Oddmu is a wiki web server and a static site generator.
//
// The types exported are the ones needed to write the templates.
package main
import (
@@ -139,6 +142,24 @@ func scheduleInstallWatcher() {
}
}
// serve starts the web server using [http.Serve]. The listener is determined via [getListener]. The various handlers
// are created using [makeHandler] if their path starts with an action segment. For example, the URL path "/view/index"
// is understood to contain the "view" action and so [viewHandler] is called with the argument "index". The one handler
// that doesn't need this is [rootHandler].
//
// The handlers often come in pairs. One handler to show the user interface and one handler to make the change:
// - [editHandler] shows the edit form and [saveHandler] saves changes to a page
// - [addHandler] shows the add form and [appendHandler] appends the addition to a page
// - [uploadHandler] shows the upload form and [dropHandler] saves the uploaded files
//
// Some handlers only do something and the links or forms to call them is expected to be part of the view template:
// - [archiveHandler] zips up the current directory
// - [diffHandler] shows the changes made in the last 60min to a page
// - [searchHandler] shows search results
//
// At the same time as the server starts up, pages are indexed via [scheduleLoadIndex], languages are loaded via
// [scheduleLoadLanguages] and the current directory and its subdirectories is watched for changes using watchers
// installed via [scheduleInstallWatcher].
func serve() {
http.HandleFunc("/", rootHandler)
http.HandleFunc("/archive/", makeHandler(archiveHandler, true))
@@ -185,6 +206,8 @@ func commands() {
os.Exit(int(subcommands.Execute(ctx)))
}
// main runs [serve] if called without arguments and it runs [commands] if called with arguments.
// The first argument is the subcommand.
func main() {
if len(os.Args) == 1 {
serve()