andrew

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 12 Imported by: 0

README

andrew

I wanted an http server that allows me to add a simple annotation into an index.html that is replaced with the contents of any html files that are below the current index.html in the file system hierarchy.

It's grown a little to include a small sitemap generator.

invocation

andrew -h to see the help

andrew accepts up to three arguments, in this order:

andrew [contentRoot] [address] [baseUrl]

contentRoot is the directory you're serving from, that contains your top level index.html. andrew follows apache's lead on expecting index.html in any directory as a default page.

address is the address you want to bind the server to. Specify as an address:port combination.

baseUrl is the hostname you're serving from. This is a part of sitemaps and rss feeds. It contains the protocol e.g. https://playtechnique.io

rendering the .AndrewIndexBody

Given this file system structure:

index.html
articles/
        index.html
        article-1.html
        article-2.html
        article-2.css
        article-1.js
fanfics/
        index.html
        story-1/
                potter-and-draco.html
        story-2/
                what-if-elves-rode-mice-pt1.html
                what-if-elves-rode-mice-pt2.html

if articles/index.html contains {{ .AndrewIndexBody }} anywhere, that will be replaced with:

    <a class="andrewindexbodylink" id="andrewindexbodylink0" href="article-1.html">article 1</a>
    <a class="andrewindexbodylink" id="andrewindexbodylink1" href="article-2.html">article 2</a>

if fanfics/index.html contains {{ .AndrewIndexBody }}, that'll be replaced with:

    <a class="andrewindexbodylink" id="andrewindexbodylink0" href="story-1/potter-and-draco.html">Potter and Draco</a>
    <a class="andrewindexbodylink" id="andrewindexbodylink0" href="story-2/what-if-elves-rode-mice-pt1.html">what-if-elves-rode-mice-pt1.html</a>
    <a class="andrewindexbodylink" id="andrewindexbodylink0" href="story-2/what-if-elves-rode-mice-pt1.html">what-if-elves-rode-mice-pt2.html</a>

page titles

If a page contains a <title> element, Andrew picks it up and uses that as the name of a link. If the page does not contain a <title> element, then Andrew will use the file name of that file as the link name.

ordering of pages

In this release, Andrew serves you page links ascii-betically.

sitemap.xml

When the endpoint baseUrl/sitemap.xml is visited, Andrew will automatically generate a sitemap containing paths to all html pages.

server

go install github.com/playtechnique/andrew/cmd

Documentation

Index

Constants

View Source
const (
	DefaultContentRoot = "."
	DefaultAddress     = ":8080"
	DefaultBaseUrl     = "http://localhost:8080"
)

Variables

This section is empty.

Functions

func CheckPageErrors

func CheckPageErrors(err error) (string, int)

CheckPageErrors is a helper function that will convert an error handed into it into the appropriate http error code and a message. If no specific error is found, a 500 is the default value returned.

func GenerateSiteMap

func GenerateSiteMap(f fs.FS, baseUrl string) []byte

Generates and returns a sitemap.xml.

func ListenAndServe

func ListenAndServe(contentRoot fs.FS, address string, baseUrl string) error

func Main

func Main(args []string, printDest io.Writer) int

func ParseArgs

func ParseArgs(args []string) (string, string, string)

Types

type AndrewPage added in v0.0.7

type AndrewPage struct {
	Title string
	// According to https://datatracker.ietf.org/doc/html/rfc1738#section-3.1, the subsection of a
	// URL after the procol://hostname is the UrlPath.
	UrlPath string
	//
	Content     string
	PublishTime time.Time
}

func NewPage added in v0.0.7

func NewPage(server AndrewServer, pageUrl string) (AndrewPage, error)

NewPage creates a Page from a URL by reading the corresponding file from the file system. If a page cannot be read, it just hands the error back up the stack. This is because NewPage is being called in a web server's context, and errors are handled by printing both an http.StatusCode and a well understood warning to a tcp socket, not by panicking or something. The Page constructor does not have access to the tcp socket, so it cannot actually handle the error correctly.

func (AndrewPage) SetUrlPath added in v0.0.7

func (a AndrewPage) SetUrlPath(urlPath string) AndrewPage

type AndrewServer

type AndrewServer struct {
	SiteFiles fs.FS  //The files being served
	BaseUrl   string //The URL used in any links generated for this website that should contain the hostname.
	Address   string //IpAddress:Port combo to be served on.
	// contains filtered or unexported fields
}

AndrewServer holds a reference to the paths in the fs.FS that correspond to each page that should be served. When a URL is requested, AndrewServer creates an AndrewPage for the file referenced in that URL and then serves the AndrewPage.

func NewAndrewServer

func NewAndrewServer(contentRoot fs.FS, address string, baseUrl string) (AndrewServer, error)

func (AndrewServer) GetSiblingsAndChildren added in v0.0.7

func (a AndrewServer) GetSiblingsAndChildren(pagePath string, filter func(string, fs.DirEntry) bool) ([]AndrewPage, error)

GetSiblingsAndChildren accepts a path to a file and a filter function. It infers the directory that the file resides within, and then recurses the Server's fs.FS to return all of the files both in the same directory and further down in the directory structure. To filter these down to only files that you care about, pass in a filter function. The filter is called in the context of fs.WalkDir. It is handed fs.WalkDir's path and directory entry, in that order, and is expected to return a boolean false. If that error is nil then the current file being evaluated is skipped for consideration.

func (AndrewServer) Serve

func (a AndrewServer) Serve(w http.ResponseWriter, r *http.Request)

Serve handles requests for any URL. It checks whether the request is for an index.html page or for anything else (another page, css, javascript etc). If a directory is requested, Serve defaults to finding the index.html page within that directory. Detecting this case for

func (AndrewServer) ServeSiteMap

func (a AndrewServer) ServeSiteMap(w http.ResponseWriter, r *http.Request)

SiteMap

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL