houston

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: LGPL-2.1 Imports: 11 Imported by: 1

README

Houston is an Express-like Gemini server, written in Go. Primarily because I want to. Not because it's necessary. There are plenty other Gemini servers that have all important functionality covered, and several are written in Go themselves. I just want to make my own.

That said, this aims to be lightweight and easy to use. This was initially developed to suite my own purposes, for my own capsules, as I wanted a server that I understood. Not somebody else's.

Basic Usage Example

Easiest way to learn, for me, is by reading an example. Here you go:

package main
    
import (
    "fmt"
    "git.sr.ht/~seanld/houston"
)
    
func main() {
    mainRouter := houston.BlankRouter()
    
    // Route a URL path to a static file directory, like:
    // gemini://localhost/ -> ./sandbox/index.gmi
    // gemini://localhost/hello -> ./sandbox/hello.gmi
    mainRouter.AddSandbox("/", "sandbox")
    
    // Run a function when gemini://localhost/interact is visited.
    mainRouter.AddRoute("/interact", func(ctx houston.Context) {
        // Send input response to client, and then run a function
        // with the entered input value.
        ctx.InputAndDo("Enter name", func(s string, c houston.Context) {
            ctx.SendStringf("text/gemini", "Hello, %s, you are #%d.", s, 1)
        })
    })
    
    // Provide a router, certificate file, and private key file.
    newServer := houston.NewServer(mainRouter, "cert/main.crt", "cert/my.key")
    
    fmt.Println("Starting server...")
    newServer.Start("localhost")
}

More Info

Router structs are used by Server structs to provide functionality for handling request-to-response. Routers can have Route and Sandbox instances. They can be added to a router by doing Router.AddRoute(url, func (net.Conn) {}) or Router.AddSandbox(url, sandboxDirPath).

Route instances connect a URL path to a function that is executed when it's visited.

Sandbox instances connect a URL path to a local directory that holds static files. For example, if you connect /hello to local dir /hello-static, and /hello-static has a file named index.gmi in it, and someone visits /hello, it will attempt to load the file /hello-static/index.gmi. Or any other file specified from that dir.

Context instances provide the URL of a connection, the actual net.Conn of the connection, some methods for conveniently sending responses, and other features.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanURLPath added in v0.1.1

func CleanURLPath(targetUrl string) string

func GetMimetypeFromPath

func GetMimetypeFromPath(targetPath string) string

func GetSharedPath

func GetSharedPath(path1 string, path2 string) string

Get the common part of two paths.

func HandleConnection

func HandleConnection(s *Server, c net.Conn)

func URLToSandboxPath

func URLToSandboxPath(targetUrl string, sandbox Sandbox) (string, error)

Match a URL path to a local path.

Types

type Context

type Context struct {
	URL        string
	Connection net.Conn
}

func NewContext

func NewContext(newUrl string, newConn net.Conn) Context

func (*Context) BadRequest

func (ctx *Context) BadRequest(info string)

func (*Context) CGIError

func (ctx *Context) CGIError(info string)

func (*Context) CertNotAuthorized

func (ctx *Context) CertNotAuthorized(info string)

func (*Context) CertNotValid

func (ctx *Context) CertNotValid(info string)

func (*Context) ClientCertRequired

func (ctx *Context) ClientCertRequired(info string)

func (*Context) GetQuery

func (c *Context) GetQuery() string

func (*Context) Gone

func (ctx *Context) Gone(info string)

func (*Context) Input

func (ctx *Context) Input(prompt string)

func (*Context) InputAndDo

func (ctx *Context) InputAndDo(prompt string, handler InputHandler)

func (*Context) NotFound

func (ctx *Context) NotFound(info string)

func (*Context) PermFailure

func (ctx *Context) PermFailure(info string)

func (*Context) ProxyError

func (ctx *Context) ProxyError(info string)

func (*Context) ProxyRequestRefused

func (ctx *Context) ProxyRequestRefused(info string)

func (*Context) RedirectPerm

func (ctx *Context) RedirectPerm(url string)

func (*Context) RedirectTemp

func (ctx *Context) RedirectTemp(url string)

func (*Context) SendBytes

func (ctx *Context) SendBytes(mimeType string, content []byte)

Send content data to the client.

func (*Context) SendFile

func (ctx *Context) SendFile(mimeType string, path string) error

func (*Context) SendString

func (ctx *Context) SendString(mimeType string, str string)

func (*Context) SendStringf

func (ctx *Context) SendStringf(mimeType string, str string, values ...interface{})

func (*Context) SensitiveInput

func (ctx *Context) SensitiveInput(prompt string)

func (*Context) ServerUnavailable

func (ctx *Context) ServerUnavailable(info string)

func (*Context) SlowDown

func (ctx *Context) SlowDown(waitSeconds int)

func (*Context) Success

func (ctx *Context) Success(mimeType string)

func (*Context) TempFail

func (ctx *Context) TempFail(info string)

type InputHandler

type InputHandler func(string, Context)

type Route

type Route struct {
	Path    string
	Handler RouteHandler
}

type RouteHandler

type RouteHandler func(Context)

type Router

type Router struct {
	Routes       []Route
	Sandboxes    []Sandbox
	ErrorHandler RouteHandler
}

func BlankRouter

func BlankRouter() Router

func NewRouter

func NewRouter(config RouterOpts) Router

func (*Router) AddRoute

func (r *Router) AddRoute(targetPath string, handler RouteHandler)

func (*Router) AddSandbox

func (r *Router) AddSandbox(targetPath string, sandboxDirPath string)

func (*Router) GetRouteHandler

func (r *Router) GetRouteHandler(targetPath string) RouteHandler

Get the handler for a given route. If no route matches a handler, then return the default error handler.

type RouterOpts

type RouterOpts struct {
	ErrorHandler RouteHandler
}

type Sandbox

type Sandbox struct {
	Path      string
	LocalPath string
}

Sandboxes represent "static" file directories where having code execute upon URL visitation is not necessary.

type Server

type Server struct {
	TLSConfig *tls.Config
	Router    Router
}

func NewServer

func NewServer(router Router, certificatePath string, keyPath string) Server

func (*Server) Start

func (s *Server) Start(args ...interface{})

Start a configured server. Arguments: 1st?: Hostname 2nd?: Port

Jump to

Keyboard shortcuts

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