Documentation ¶
Index ¶
- Constants
- func CheckPageErrors(err error) (string, int)
- func GenerateSiteMap(f fs.FS, baseUrl string) []byte
- func ListenAndServe(contentRoot fs.FS, address string, baseUrl string) error
- func Main(args []string, printDest io.Writer) int
- func ParseArgs(args []string) (string, string, string)
- type AndrewPage
- type AndrewServer
Constants ¶
const ( DefaultContentRoot = "." DefaultAddress = ":8080" DefaultBaseUrl = "http://localhost:8080" )
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 GenerateSiteMap ¶
Generates and returns a sitemap.xml.
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 (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