shrt

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: ISC Imports: 8 Imported by: 0

README

Shrt
====

This is a URL shortener. There are many like it, but this is mine. Try
putting all the vowels in "shrt" and enjoy yourself!

Also handles go-get redirects.

HTTP Handler
------------

This Go http.Handler module is imported as djmo.ch/go-shrt.

To use, initialize Shrt with a shrt.Config objec. Drop this Handler
into your site's http.ServeMux and start serving shortlinks and go-get
redirects.

CLI Reference Implementation
----------------------------

The command line interface (CLI) in this repository is useful if you
wish to run Shrt as a standalone server. It can is installed in the
usual manner:

    $ go install djmo.ch/go-shrt/cmd/shrt@latest

From there you can run "shrt help" to read the CLI documentation.

License
-------

ISC. See the LICNSE file for full copyright and license details.

Documentation

Overview

Package shrt implements a simple (perhaps simplistic) URL shortener. It also handles go-get requests.

Shortlinks are recorded in the database, and any request path not matching a shortlink is assumed to be a go-get request. This is by design, but can result in specious redirects. Additionally, subdirectory paths are not allowed.

Shortlinks generate an HTTP 301 response. Go-get requests generate an HTTP 200 response. If configured, requests to the base path (i.e., "/") generate an HTTP 302 response.

The database file is human-readable. See [Shrtfile] for the full specification.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.3.0

type Config struct {
	// Server name of the Shrt host
	SrvName string
	// SCM (or VCS) type
	ScmType string
	// SCM repository suffix, if required by repository host
	Suffix string
	// The server name of the repository host
	RdrName string
	// Where requests with an empty path should redirect
	BareRdr string
	// The path to the [ShrtFile]-formatted database file.
	DbPath string
	// The string to append to the URL for go-get redirects to
	// form the directory entry in the go-source meta tag. This
	// key is experimental and may be removed in a future release.
	GoSourceDir string
	// The string to append to the URL for go-get redirects to
	// form the file entry in the go-source meta tag.  This
	// key is experimental and may be removed in a future release.
	GoSourceFile string
}

Config contains all of the global configuration for Shrt. All values except BareRdr and DbPath are used in the go-import meta tag values for go-get requests.

type ShrtEntry added in v0.3.0

type ShrtEntry struct {
	URL  string
	Type ShrtType
}

ShrtEntry is a ShrtFile entry.

type ShrtFile

type ShrtFile struct {
	// contains filtered or unexported fields
}

The ShrtFile struct contains the data read from a specially-formatted file. The syntax of the file is human readable. Each line represents a key-value pair. The key is everything to the left of the first equals sign, and the value is everything to the right. The value is then split around the first occurence of the colon character, with the left side representing the type, and the right side representing the URL. Whitespace is trimmed from the beginning and end of all fields.

ShrtFile is safe for concurrent use across multiple goroutines.

func NewShrtFile

func NewShrtFile() *ShrtFile

The NewShrtFile function returns a new ShrtFile.

func (*ShrtFile) Get

func (s *ShrtFile) Get(key string) (ShrtEntry, error)

The Get method gets the value of the specified key. If the key does not exist, an error is returned.

func (*ShrtFile) ReadShrtFile added in v0.3.0

func (s *ShrtFile) ReadShrtFile(f fs.File) error

The ReadShrtFile function reads an existing ShrtFile from f and returns a pointer to a ShrtFile object. The provided file is closed before returning.

type ShrtHandler added in v0.3.0

type ShrtHandler struct {
	ShrtFile *ShrtFile
	Config   Config
	FS       fs.FS
}

ShrtHandler is the core http.Handler for go-shrt.

func (*ShrtHandler) ServeHTTP added in v0.3.0

func (s *ShrtHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

Handle implements the http.Handler interface.

type ShrtType added in v0.3.0

type ShrtType int

ShrtType is the type of a ShrtFile entry. Their textual representations within the ShrtFile are listed in NoneType.

const (
	NoneType  ShrtType = iota
	ShortLink          // shrtlnk
	GoGet              // goget
)

Directories

Path Synopsis
cmd
shrt
usage: shrt <command> [arguments]
usage: shrt <command> [arguments]
shrt/internal/base
Package base defines the foundational structures required to build out the Shrt command suite.
Package base defines the foundational structures required to build out the Shrt command suite.
shrt/internal/env
Package env implements the "shrt env" command
Package env implements the "shrt env" command
shrt/internal/help
Package help implements the "shrt help" command
Package help implements the "shrt help" command
shrt/internal/serve
Package serve implements the "shrt serve" command
Package serve implements the "shrt serve" command
shrt/internal/version
Package version implements the "shrt version" command
Package version implements the "shrt version" command
devtools

Jump to

Keyboard shortcuts

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