gem

package module
v0.0.0-...-deb5ce5 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MPL-2.0 Imports: 11 Imported by: 0

README

💎 Gem

A library to quickly build gemini capsules using go.

Installation

Installing Gem can be done easily using go get:

go get github.com/c-128/gem

Examples

package main

import (
	"io"

	"github.com/c-128/gem"
)

func helloGemini(ctx *gem.Ctx) error {
	ctx.Status(gem.StatusSuccess, "text/gemini")

	_, err := io.WriteString(ctx, "# Hello gemini!\n")
	if err != nil {
		return err
	}

	return nil
}

func main() {
	err := gem.ServeAndListen(
		"0.0.0.0:1965",
		helloGemini,
		"cert/cert.crt",
		"cert/key.key",
	)
	if err != nil {
		panic(err)
	}
}

Check out more examples in the examples folder.

Documentation

Index

Constants

View Source
const (
	StatusInput          = 10
	StatusSensitiveInput = 11

	StatusSuccess = 20

	StautsTemporaryRedirection = 30
	StatusPermanentRedirection = 31

	StatusTemporaryFailure  = 40
	StatusServerUnavailable = 41
	StautsCGIError          = 42
	StautsProxyError        = 43
	StautsSlowDown          = 44

	StatusPermanentFailure    = 50
	StatusNotFound            = 51
	StatusGone                = 52
	StatusProxyRequestRefused = 53
	StatusBadRequest          = 59

	StatusClientCert              = 60
	StatusClientCertNotAuthorized = 61
	StatusClientCertNotValid      = 62
)

Variables

View Source
var (
	NotFoundErr = NewError(StatusNotFound, "not found")
)

Functions

func ServeAndListen

func ServeAndListen(address string, handler Handler, certFile string, keyFile string) error

Types

type Cert

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

A client certificate that is sent to the server.

func (*Cert) Fingerprint

func (cert *Cert) Fingerprint() string

Calculates the SHA512 sum of the certificate and encodes it in hex.

func (*Cert) RawCert

func (cert *Cert) RawCert() *x509.Certificate

Returns the underlying x509 certificate.

func (*Cert) Valid

func (cert *Cert) Valid() bool

Returns true if the certificate is within the valid time range.

type Ctx

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

func (*Ctx) Cert

func (ctx *Ctx) Cert() *Cert

Get the client certificate.

func (*Ctx) Connection

func (ctx *Ctx) Connection() *tls.Conn

Get the underlying connection.

func (*Ctx) Local

func (ctx *Ctx) Local(key string) any

Get a local. Will return nil if the local hasn't been found.

func (*Ctx) LookupParam

func (ctx *Ctx) LookupParam(key string) (string, bool)

Lookup a route parameter.

func (*Ctx) Param

func (ctx *Ctx) Param(key string) string

Get a route parameter.

func (*Ctx) Query

func (ctx *Ctx) Query() string

Get the URL query.

func (*Ctx) SendStatus

func (ctx *Ctx) SendStatus(status int, info string) error

Sends the status to the client. If the status has already been sent, it will error. If the status isn't between [10; 99], it will error.

func (*Ctx) SetLocal

func (ctx *Ctx) SetLocal(key string, value any)

Set a local.

func (*Ctx) Status

func (ctx *Ctx) Status(status int, info string)

Sets the status that will be send upon the first .Write call.

func (*Ctx) URL

func (ctx *Ctx) URL() URL

Get the request URL.

func (*Ctx) Write

func (ctx *Ctx) Write(buffer []byte) (int, error)

Write a repsonse to the client. If the response status hasn't been sent yet using .SendStatus, this will send it. By default it responds with status code 20 and info "text/plain". These values can be overriden by calling .Status.

type Error

type Error interface {
	Status() int
	Error() string
}

func NewError

func NewError(status int, err string) Error

type Handler

type Handler func(ctx *Ctx) error

type Map

type Map map[string]any

type Mux

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

func NewMux

func NewMux() *Mux

func (*Mux) Handle

func (mux *Mux) Handle(path string, handler Handler)

func (*Mux) Handler

func (mux *Mux) Handler(ctx *Ctx) error

type URL

type URL struct {
	Scheme string
	Host   string
	Path   string
	Query  string
}

A gemini URL.

func ParseURL

func ParseURL(raw string) (*URL, error)

Parse a URL. Only supports the "gemini" scheme.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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