Documentation
¶
Index ¶
- Constants
- func CheckPageErrors(err error) (string, int)
- func GenerateRssFeed(f fs.FS, baseUrl string, rssChannelTitle string, rssChannelDescription string) []byte
- func GenerateSiteMap(f fs.FS, baseUrl string) []byte
- func GetMetaElements(htmlContent []byte) (map[string]string, error)
- func ListenAndServe(contentRoot fs.FS, address string, hostname string, certInfo *CertInfo, ...) error
- func Main(args []string, printDest io.Writer) int
- func ParseArgs(args []string) (string, string, string)
- func ParseOpts(args []string, printDest io.Writer) (*CertInfo, *RssInfo, []string, error)
- func RenderTemplates(siblings []Page, startingPage Page) ([]byte, error)
- type CertInfo
- type Page
- type PageSortFunc
- type RssInfo
- type Server
- func (a *Server) Close() error
- func (a Server) GetSiblingsAndChildren(pagePath string) ([]Page, error)
- func (a *Server) ListenAndServe() error
- func (a *Server) ListenAndServeTLS(certPath string, privateKeyPath string) error
- func (a Server) Serve(w http.ResponseWriter, r *http.Request)
- func (a Server) ServeRssFeed(w http.ResponseWriter, r *http.Request)
- func (a Server) ServeSiteMap(w http.ResponseWriter, r *http.Request)
- type TagInfo
Constants ¶
const ( DefaultContentRoot = "." DefaultAddress = ":8080" DefaultBaseUrl = "http://localhost:8080" DefaultRssFeedTitle = "Home" DefaultrssFeedDescription = "Writings" )
Variables ¶
This section is empty.
Functions ¶
func CheckPageErrors ¶
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 GenerateRssFeed ¶ added in v0.2.0
func GenerateRssFeed(f fs.FS, baseUrl string, rssChannelTitle string, rssChannelDescription string) []byte
The RSS format's pretty simple. First we add a constant header identifying the vesion of the RSS feed. Then we add the "channel" information. A "channel" is this RSS document. Inside the "channel", we add all of the "items". For Andrew, an "item" is synonymous with a page that is not an index.html page. Finally, we close the channel. It's sort of an anachronistic site to visit, but https://www.rssboard.org/rss-specification is the reference for what I'm including in these items and the channel.
func GenerateSiteMap ¶
Generates and returns a sitemap.xml.
func GetMetaElements ¶ added in v0.1.0
func ListenAndServe ¶
func ListenAndServe(contentRoot fs.FS, address string, hostname string, certInfo *CertInfo, rssInfo *RssInfo) error
ListenAndServe creates a server in the contentRoot, listening at the address, with links on autogenerated pages to the baseUrl. contentRoot - an fs.FS at some location, whether that's a virtual fs.FS such as an fs.Testfs or an
fs.FS at a location on your file system such as os.DirFS.
contentRoot - an initialised fs.FS. Some implementation details sometimes differ amongst different fs.FS; Andrew internally uses an os.DirFS and tests with an fstest.MapFS, so those two have some code examples herein. address - an ip:port combination. The AndrewServer will bind an http server here. baseUrl - the hostname that you are hosting from. certInfo - certificate info type. If the members are empty, Andrew serves http.
func Main ¶
Main is the implementation of main. It's here to get main's logic into a testable package.
func ParseArgs ¶
ParseArgs ensures command line arguments override the default settings for a new Andrew server.
func ParseOpts ¶ added in v0.1.3
ParseOpts parses command-line options and returns a CertInfo struct, remaining arguments, and an error if any.
The args parameter contains the command-line arguments, and printDest is where the help message is written if `-h` or `--help` is specified.
Supported options:
- -c, --cert: Path to the SSL certificate file. Must be used with `--privatekey`.
- -p, --privatekey: Path to the private key file. Must be used with `--cert`.
- -h, --help: Displays the help message and returns a specific error.
If only one of `--cert` or `--privatekey` is provided, an error is returned.
Returns a CertInfo struct containing the SSL certificate and key paths, the remaining arguments, and any error encountered.
func RenderTemplates ¶ added in v0.1.2
RenderTemplates receives the path to a file, currently normally an index file. It traverses the file system starting at the directory containing that file, finds all html files that are _not_ index.html files and returns them as a list of html links to those pages.
Types ¶
type CertInfo ¶ added in v0.1.3
CertInfo tracks SSL certificate information. Andrew can optionally serve HTTPS traffic, but to do so it has to know how to find both the path to the certificate and to the private key.
type Page ¶ added in v0.1.0
type Page struct { // Page title 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 }
Page tracks the content of a specific file and various pieces of metadata about it. The Page makes creating links and serving content convenient, as it lets me offload the parsing of any elements into a constructor, so that when I need to present those elements to an end-user they're easy for me to reason about.
func DefaultPageSort ¶ added in v0.3.0
DefaultPageSort provides the default sorting behavior for pages (current implementation preserved as a separate function)
func NewPage ¶ added in v0.0.7
NewPage creates a Page from a URL by reading the corresponding file from the AndrewServer's SiteFiles. NewPage does this by reading the page content from disk, then parsing out various metadata that are convenient to have quick access to, such as the page title or the publish time.
func SetUrlPath ¶ added in v0.1.2
SetUrlPath updates the UrlPath on a pre-existing Page.
func SortPagesByDate ¶ added in v0.2.0
type PageSortFunc ¶ added in v0.3.0
PageSortFunc is a function type that defines how to sort Pages within a directory
type Server ¶ added in v0.1.0
type Server 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. Andrewtableofcontentstemplate string // The string we're searching for inside a Page that should be replaced with a template. Mightn't belong in the Server. RssTitle string // The title of your RSS feed. RssDescription string // The description of your RSS feed. Go wild. HTTPServer *http.Server }
Server holds a reference to the paths in the fs.FS that correspond to each page that should be served. When a URL is requested, Server creates an Page for the file referenced in that URL and then serves the Page.
func NewServer ¶ added in v0.1.0
NewServer builds your web server. contentRoot: an fs.FS of the files that you're serving. address: The ip address to bind this web server to. baseUrl: https://example.com or http://www.example.com rssTitle: The title of the RSS feed that shares your site. rssDescription: The description for your RSS feed. Jazz it up. Returns an Server.
func (Server) GetSiblingsAndChildren ¶ added in v0.1.0
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.
func (*Server) ListenAndServe ¶ added in v0.1.0
func (*Server) ListenAndServeTLS ¶ added in v0.2.3
func (Server) Serve ¶ added in v0.1.0
func (a Server) 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 (Server) ServeRssFeed ¶ added in v0.2.0
func (a Server) ServeRssFeed(w http.ResponseWriter, r *http.Request)
func (Server) ServeSiteMap ¶ added in v0.1.0
func (a Server) ServeSiteMap(w http.ResponseWriter, r *http.Request)
SiteMap