razlink

package module
v0.0.0-...-119f5e9 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InstanceID = newInstanceID()

InstanceID is a unique ID for this instance (to be prepended to entry IDs)

Functions

func GetBase

func GetBase(r *http.Request) string

GetBase returns the base target for relative URLs

func GetShorthandPath

func GetShorthandPath(path string) string

GetShorthandPath returns a shorthand version of a path in case it's too long

func HasContentType

func HasContentType(header http.Header, mimetype string) bool

HasContentType determines whether the request `content-type` includes a server-acceptable mime-type

func Hash

func Hash(s string) string

Hash returns the SHA1 hash of a string

func IsPrivateIP

func IsPrivateIP(ip net.IP) bool

IsPrivateIP returns true if the given IP address belongs to private network space

func IsPrivateURL

func IsPrivateURL(rawURL string) (bool, error)

IsPrivateURL checks whether the host in the given URL resolves to a private IP address

func NewID

func NewID() string

NewID returns a new (hopefully unique) ID for entries

func WriteFavicon

func WriteFavicon(w http.ResponseWriter, _ *http.Request)

WriteFavicon writes Razlink favicon to a http.ResponseWriter

func WritePixel

func WritePixel(w http.ResponseWriter, _ *http.Request)

WritePixel writes a transparent pixel to a http.ResponseWriter

Types

type API

type API struct {
	Path string
	// contains filtered or unexported fields
}

API is lightweight frontend-less version of a page

func NewAPI

func NewAPI(page *Page) *API

NewAPI returns a new API

func (*API) GetHandler

func (api *API) GetHandler() http.HandlerFunc

GetHandler creates a http.HandlerFunc that uses Razlink layout

type DB

type DB struct {
	ExpirationTime time.Duration
	MaxLogs        int
	// contains filtered or unexported fields
}

DB ...

func NewDB

func NewDB(redisUrl string) (*DB, error)

NewDB returns a new DB

func (*DB) Close

func (db *DB) Close() error

Close closes the connection to the database

func (*DB) DeleteEntry

func (db *DB) DeleteEntry(id string) error

DeleteEntry deleted the entry with the given ID

func (*DB) DeleteLogs

func (db *DB) DeleteLogs(entryID string) error

DeleteLogs deleted all logs that belong to an entry

func (*DB) GetEntries

func (db *DB) GetEntries(pattern string) (map[string]*Entry, error)

GetEntries returns the list of entries with IDs matching the given pattern

func (*DB) GetEntry

func (db *DB) GetEntry(id string) (*Entry, error)

GetEntry returns the entry with the given ID

func (*DB) GetLogs

func (db *DB) GetLogs(entryID string, first, last int) ([]Log, error)

GetLogs returns the Nth page of logs that belong to an entry (pages are 0 based)

func (*DB) GetLogsCount

func (db *DB) GetLogsCount(entryID string) (int, error)

GetLogsCount returns the number of logs that belong to an entry

func (*DB) InsertEntry

func (db *DB) InsertEntry(id *string, e *Entry) (string, error)

InsertEntry inserts a new entry to the database If 'id' is null, the function will generate and return a unique one

func (*DB) InsertLog

func (db *DB) InsertLog(entryID string, r *http.Request) error

InsertLog inserts a new log

func (*DB) SetEntry

func (db *DB) SetEntry(id string, e *Entry) error

SetEntry inserts or rewrites the entry with the given ID

type Entry

type Entry struct {
	URL          string
	Method       ServeMethod
	Salt         string
	PasswordHash string
	Permanent    bool
}

Entry ...

func NewEntry

func NewEntry(url, password string, method ServeMethod) *Entry

NewEntry ...

func (*Entry) MatchPassword

func (entry *Entry) MatchPassword(password string) bool

MatchPassword ...

func (*Entry) SetPassword

func (entry *Entry) SetPassword(password string)

SetPassword ...

type Layout

type Layout interface {
	BindTemplate(pageTemplate string, stylesheets, scripts []string, meta map[string]string) (LayoutRenderer, error)
}

Layout is used to give pages a uniform layout

var DefaultLayout Layout = (*layout)(template.Must(template.New("layout").Parse(layoutT)))

DefaultLayout is razlink's default layout

type LayoutRenderer

type LayoutRenderer func(w http.ResponseWriter, r *http.Request, title string, data interface{}, statusCode int)

LayoutRenderer is a function that renders a html page

type Log

type Log struct {
	Time        time.Time
	IP          string
	Hostnames   []string `json:"Addresses"`
	CountryName string
	RegionName  string
	City        string
	OS          string
	Browser     string
	Referer     string
}

Log ...

func NewLog

func NewLog(r *http.Request) Log

NewLog ...

func (Log) String

func (l Log) String() string

type Page

type Page struct {
	Path            string
	Title           string
	ContentTemplate string
	Stylesheets     []string
	Scripts         []string
	Metadata        map[string]string
	Handler         func(*PageRequest) *View
}

Page ...

func (*Page) GetHandler

func (page *Page) GetHandler(layout Layout) (http.HandlerFunc, error)

GetHandler creates a http.HandlerFunc that uses the given layout to render the page

type PageRequest

type PageRequest struct {
	Request *http.Request
	RelPath string
	RelURI  string
	Title   string
	// contains filtered or unexported fields
}

PageRequest ...

func (*PageRequest) AsyncCopyView

func (r *PageRequest) AsyncCopyView(resp *http.Response, opts ...ViewOption) *View

AsyncCopyView ...

func (*PageRequest) CookieAndRedirectView

func (r *PageRequest) CookieAndRedirectView(cookie *http.Cookie, url string, opts ...ViewOption) *View

CookieAndRedirectView ...

func (*PageRequest) CopyView

func (r *PageRequest) CopyView(resp *http.Response, opts ...ViewOption) *View

CopyView ...

func (*PageRequest) EmbedView

func (r *PageRequest) EmbedView(url string, opts ...ViewOption) *View

EmbedView ...

func (*PageRequest) ErrorView

func (r *PageRequest) ErrorView(errmsg string, errcode int, opts ...ViewOption) *View

ErrorView ...

func (*PageRequest) HandlerView

func (r *PageRequest) HandlerView(handler http.HandlerFunc, opts ...ViewOption) *View

HandlerView ...

func (*PageRequest) RedirectView

func (r *PageRequest) RedirectView(url string, opts ...ViewOption) *View

RedirectView ...

func (*PageRequest) Respond

func (r *PageRequest) Respond(data interface{}, opts ...ViewOption) *View

Respond returns the default page response View

type ServeMethod

type ServeMethod int

ServeMethod defines how to serve a request

const (
	Proxy ServeMethod = iota
	Embed
	Redirect
	Track
)

Available serve methods

func GetServeMethodForURL

func GetServeMethodForURL(ctx context.Context, url string, timeout time.Duration) (ServeMethod, error)

GetServeMethodForURL tries to determine the best possible serve method for a URL

func GetServeMethodFromHeader

func GetServeMethodFromHeader(header http.Header) ServeMethod

GetServeMethodFromHeader tries to determine the best possible serve method from a http response header

type Server

type Server struct {
	FaviconPNG []byte
	Metadata   map[string]string
	// contains filtered or unexported fields
}

Server ...

func NewServer

func NewServer() *Server

NewServer creates a new Server

func (*Server) AddPage

func (srv *Server) AddPage(page *Page) error

AddPage adds a new servable page to the server

func (*Server) AddPageWithLayout

func (srv *Server) AddPageWithLayout(page *Page, layout Layout) error

AddPageWithLayout adds a new servable page with custom layout to the server

func (*Server) AddPages

func (srv *Server) AddPages(pages ...*Page)

AddPages adds multiple pages to the server and panics if anything goes wrong

func (*Server) ServeHTTP

func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type View

type View struct {
	StatusCode int
	Error      error
	Data       interface{}
	Redirect   string
	// contains filtered or unexported fields
}

View is something that a PageHandler returns and is capable of rendering a page

func AsyncCopyView

func AsyncCopyView(resp *http.Response, opts ...ViewOption) *View

AsyncCopyView ...

func CookieAndRedirectView

func CookieAndRedirectView(r *http.Request, cookie *http.Cookie, url string, opts ...ViewOption) *View

CookieAndRedirectView ...

func CopyView

func CopyView(resp *http.Response, opts ...ViewOption) *View

CopyView ...

func EmbedView

func EmbedView(url string, opts ...ViewOption) *View

EmbedView returns a View that embeds the given website

func ErrorView

func ErrorView(r *http.Request, errmsg string, errcode int, opts ...ViewOption) *View

ErrorView returns a View that represents an error

func HandlerView

func HandlerView(r *http.Request, handler http.HandlerFunc, opts ...ViewOption) *View

HandlerView ...

func RedirectView

func RedirectView(r *http.Request, url string, opts ...ViewOption) *View

RedirectView ...

func (*View) Render

func (view *View) Render(w http.ResponseWriter)

Render ...

type ViewOption

type ViewOption func(view *View)

ViewOption ...

func WithData

func WithData(data interface{}) ViewOption

WithData ...

func WithError

func WithError(err error, errcode int) ViewOption

WithError ...

func WithErrorMessage

func WithErrorMessage(errmsg string, errcode int) ViewOption

WithErrorMessage ...

Directories

Path Synopsis
cmd
razlink command

Jump to

Keyboard shortcuts

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