~gpanders/garchive

9e136c985c1ef8fa4e0c2e174e01dc8e92787908 — Greg Anders 1 year, 7 months ago v0.1.0
Initial commit
9 files changed, 281 insertions(+), 0 deletions(-)

A .gitignore
A Dockerfile
A LICENSE
A Makefile
A README.md
A bin/fetch
A index.html
A main.go
A static/style.css
A  => .gitignore +1 -0
@@ 1,1 @@
garchive

A  => Dockerfile +15 -0
@@ 1,15 @@
FROM golang:alpine as build

WORKDIR /go/src/app
COPY . .

RUN go install -i

FROM alpine

COPY --from=build /go/bin/garchive /go/bin/

WORKDIR /app
COPY index.html .
COPY static static/
ENTRYPOINT ["/go/bin/garchive", "--port", "80"]

A  => LICENSE +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2020 Greg Anders

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => Makefile +6 -0
@@ 1,6 @@
LDFLAGS := -s -w

all: garchive

garchive: main.go
	go build -ldflags "$(LDFLAGS)"

A  => README.md +97 -0
@@ 1,97 @@
# garchive

Ultra-simple front-end for a web archive.

## Description

This tool provides a web front-end for a repository of archived web pages,
typically generated via `wget`. For example, you can use `wget` to mirror a
website using

    $ wget -kmpc https://gpanders.com

This will create a folder `gpanders.com` in your current directory with a copy
of `gpanders.com` that you can open locally. This is a very simple way to
maintain archives of websites and fight [link rot][].

## Installation

The easiest method is to use [Docker](#using-docker). If you can't or don't
want to use Docker, you can build `garchive` yourself:

    $ git clone https://github.com/gpanders/garchive
    $ cd garchive
    $ go build

## Usage

You must have a tab-delimited CSV file with each line containing a link
title/description and the corresponding URL, e.g.

    Personal website of Greg Anders	https://gpanders.com
                                   ^ literal tab character (\t)

A tab character (`\t`) is used as the delimiter as it can be reasonably
expected not to appear in the title of any webpage (which cannot be said for
more traditional CSV delimiters such as commas).

You can use the included [`fetch`][fetch] utility to parse this CSV file and
clone all of the URLs into a local directory:

    $ bin/fetch links.csv data

This will use `wget` to archive all of the URLs in `links.csv` under the `data`
directory.

Once you have your `links.csv` file and your archived websites, use `garchive`
to serve up a simple front-end to access those archives:

    $ garchive -links links.csv -archive data

By default, `garchive` will bind to address `0.0.0.0` and port `8080`. Use the
`-addr` and `-port` commandline flags to change those settings.

Your archive will now be available on `localhost` at port `8080`.

## Importing from Pinboard

You can easily import bookmarks from Pinboard into your `links.csv` file using
`curl` and `jq`:

    $ curl -s "https://api.pinboard.in/v1/posts/all?auth_token=$PINBOARD_API_KEY&format=json" | jq -r '.[] | "\(.description)\t\(.href)"' > links.csv

## Using Docker

First, build the Docker image. You only need to do this once.

    $ git clone https://github.com/gpanders/garchive
    $ cd garchive
    $ docker build -t garchive .

Provide the path to your `links.csv` and archive directory as volumes to the
Docker container:

    $ docker run -v /path/to/links.csv:/app/links.csv -v /path/to/archive:/app/data -p 8080:8080 garchive

You can create a `docker-compose.yml` file to easily generate a `garchive`
container:

```yaml
version: "3"

services:
  garchive:
    container_name: garchive
    image: garchive
    build: ./
    ports:
      - 8080
    volumes:
      - /path/to/links.csv:/app/links.csv
      - /path/to/archive/data:/app/data
```

[link rot]: https://en.wikipedia.org/wiki/Link_rot
[go]: https://golang.org/doc/install
[fetch]: ./bin/fetch
[release]: https://github.com/gpanders/garchive/releases

A  => bin/fetch +25 -0
@@ 1,25 @@
#!/bin/sh

if [ $# -lt 2 ]; then
    echo "Usage: $(basename "$0") <links.csv> <dir>" >&2
    exit 1
fi

awk -F'\t' '{print $2}' "$1" | while IFS= read -r url; do
    wget \
        --adjust-extension \
        --timestamping \
        --span-hosts \
        --background \
        --convert-links \
        --page-requisites \
        --directory-prefix="$2" \
        --continue \
        --quiet \
        --append-output=wget.log \
        --wait=1 \
        --random-wait \
        --user-agent="" \
        --execute robots=off \
        "$url"
done

A  => index.html +15 -0
@@ 1,15 @@
<!DOCTYPE html>
<html>
  <head>
    <title>Archive</title>
    <link rel="stylesheet" type="text/css" href="/static/style.css">
  </head>
  <body>
    <h1>Archive</h1>
    <ul>
      {{ range . }}
      <li><a href="/ar/{{.Url}}">{{.Title}}</a></li>
      {{ end }}
    </ul>
  </body>
</html>

A  => main.go +81 -0
@@ 1,81 @@
package main

import (
	"encoding/csv"
	"flag"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"os"
	"regexp"
)

type Link struct {
	Title string
	Url   string
}

var links string
var protocolRe = regexp.MustCompile("^(?:http|https|ftp)://")

func serveIndex(w http.ResponseWriter, r *http.Request) {
	url := r.URL.Path[1:]
	if url != "" {
		http.Redirect(w, r, "/", http.StatusSeeOther)
		return
	}

	t, err := template.ParseFiles("index.html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	p, err := getLinks()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, p)
}

func getLinks() ([]Link, error) {
	f, err := os.Open(links)
	if err != nil {
		return nil, err
	}

	r := csv.NewReader(f)
	r.Comma = '\t'
	r.FieldsPerRecord = 2

	var links []Link
	rows, err := r.ReadAll()
	if err != nil {
		return nil, err
	}

	for _, row := range rows {
		title := row[0]
		url := protocolRe.ReplaceAllLiteralString(row[1], "")
		links = append(links, Link{Title: title, Url: url})
	}

	return links, nil
}

func main() {
	addr := flag.String("addr", "0.0.0.0", "bind address")
	port := flag.Int("port", 8080, "bind port")
	archive := flag.String("archive", "data", "absolute path to archive directory")
	flag.StringVar(&links, "links", "links.csv", "path to links CSV file")

	flag.Parse()

	http.Handle("/ar/", http.StripPrefix("/ar/", http.FileServer(http.Dir(*archive))))
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.HandleFunc("/", serveIndex)
	log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *addr, *port), nil))
}

A  => static/style.css +20 -0
@@ 1,20 @@
body {
    font-family: "Helvetica", "Arial", sans-serif;
    line-height: 1.5;
    margin: 0 auto;
    max-width: 80ch;
    padding: 2ch;
}
ul {
    margin: 0;
    padding: 0;
    list-style: none;
}
ul > li {
    padding: 0.5em 0;
    margin-bottom: 1em;
}
li > a {
    text-decoration: none;
    color: inherit;
}