snap

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: GPL-3.0 Imports: 23 Imported by: 0

README

snap

Typical layout for snap servers

    web/
        templates/
        static/
              app/
                 js/...
              skin1/...
              skin2/...
              favicon.ico              

Example:

package main

import (
	"git.twelvetwelve.org/library/snap"
	"git.twelvetwelve.org/library/snap/auth"
)

func handler(c *snap.Context) {
	c.Reply("snap/example/simple 1.0")
}

func handlerAuthenticated(c *snap.Context) {
	c.Reply("snap/example/simple 1.0 (authenticated)")
}

func mustRunServer(auth auth.Authenticator) {
	s := snap.New("localhost:9000", "web", auth)
	s.SetDebug(true)
	s.SetTemplatePath("web/templates")
	s.WithStaticFiles("/static", "web/static" )
	s.WithTheme("skin1")
	
	s.HandleFunc("/", handler)
	s.HandleFuncAuthenticated("/auth", "", handlerAuthenticated)

	s.Serve()
}

func main() {
	auth := auth.NewAuth("basic")
	auth.AddUser("admin", "admin", "password")
	mustRunServer(auth)
}	

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Redirect

func Redirect(to string) func(c *Context)

func Walk

func Walk(fs http.FileSystem, base string, walkFunc func(path string, info os.FileInfo, err error) error) error

Types

type CompoundOptions

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

func (*CompoundOptions) Get

func (c *CompoundOptions) Get(key string) (string, bool)

type Context

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

func (*Context) Error

func (c *Context) Error(code int, msg string)

func (*Context) ErrorObject

func (c *Context) ErrorObject(code int, obj interface{})

func (*Context) Form

func (c *Context) Form() (*Options, error)

func (*Context) GetObject

func (c *Context) GetObject(obj interface{}) error

func (*Context) GetRequest

func (c *Context) GetRequest() *http.Request

func (*Context) GetUser

func (c *Context) GetUser() string

func (*Context) ParseVars

func (c *Context) ParseVars() *Options

func (*Context) QueryHas

func (c *Context) QueryHas(key string) bool

func (*Context) QueryValue

func (c *Context) QueryValue(key string) string

func (*Context) QueryValueWithDefault

func (c *Context) QueryValueWithDefault(key string, def string) string

func (*Context) ReadObject

func (c *Context) ReadObject(object interface{}) error

func (*Context) Redirect

func (c *Context) Redirect(url string)

func (*Context) Render

func (c *Context) Render(tmpl string, content interface{})

func (*Context) RenderEx

func (c *Context) RenderEx(tmpl string, content interface{})

func (*Context) RenderWithMeta

func (c *Context) RenderWithMeta(tmpl string, meta map[string]string, content interface{})

func (*Context) Reply

func (c *Context) Reply(msg string)

func (*Context) ReplyObject

func (c *Context) ReplyObject(obj interface{})

func (*Context) ReplyWithHeaders

func (c *Context) ReplyWithHeaders(msg string, kv map[string]string)

func (*Context) Writer

func (c *Context) Writer() http.ResponseWriter

type Form

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

Form wraps utility functions around HTTP Form values

func (*Form) Get

func (f *Form) Get(k string) (string, bool)

type FormValueProvider

type FormValueProvider interface {
	FormValue(string) string
}

FormValueProvider we only care about something that can provide us the K/V lookup

type Options

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

func (*Options) GetVar

func (o *Options) GetVar(k string) (string, bool)

func (*Options) GetVarAsSlice

func (o *Options) GetVarAsSlice(k string, delim string, def []string) []string

func (*Options) GetVarDefault

func (o *Options) GetVarDefault(k string, def string) string

func (*Options) StringSlice

func (o *Options) StringSlice(k string, delim string) ([]string, bool)

StringSlice returns multiple values assigned to the same key as a string slice

func (*Options) StringValue

func (o *Options) StringValue(k string) (string, bool)

StringValue returns form value as a string

func (*Options) TimeValue

func (o *Options) TimeValue(key, timeFormat string, defaultValue *time.Time) time.Time

TimeValue returns a parsed time value for the specified key,

if the key does not exist the but defaultValue was set it will return the defaultValue
otherwise it will return the passed in time or an error if parsing failed

func (*Options) Uint64Value

func (o *Options) Uint64Value(k string) (uint64, bool)

Uint64Value returns the first value in a set as uint64

type OptionsProvider

type OptionsProvider interface {
	Get(key string) (string, bool)
}

type Query

type Query struct {
	Values url.Values
}

func (*Query) Get

func (q *Query) Get(key string) (string, bool)

type RouteBuilder

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

func NewRouteBuilder

func NewRouteBuilder() *RouteBuilder

func (*RouteBuilder) BuildRoute

func (r *RouteBuilder) BuildRoute(s *Server) http.HandlerFunc

func (*RouteBuilder) WithAuth

func (r *RouteBuilder) WithAuth(auth auth.Authenticator) *RouteBuilder

func (*RouteBuilder) WithContent

func (r *RouteBuilder) WithContent(contentHandler func(c *Context)) *RouteBuilder

func (*RouteBuilder) WithLoginHandler

func (r *RouteBuilder) WithLoginHandler(loginHandler func(c *Context) bool) *RouteBuilder

type Server

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

func New

func New(address string, path string, auth auth.Authenticator) *Server

func (*Server) AddRoute

func (s *Server) AddRoute(path string, r *RouteBuilder) *mux.Route

func (*Server) Dump

func (s *Server) Dump()

func (*Server) EnableStatus

func (s *Server) EnableStatus(path string)

func (*Server) EnableTestMode

func (s *Server) EnableTestMode(enable bool) *Server

func (*Server) HandleFunc

func (s *Server) HandleFunc(path string, f func(c *Context)) *mux.Route

func (*Server) HandleFuncAuthenticated

func (s *Server) HandleFuncAuthenticated(path, redirect string, f func(c *Context)) *mux.Route

func (*Server) HandleFuncAuthenticatedWithLogin

func (s *Server) HandleFuncAuthenticatedWithLogin(path string, loginHandler func(c *Context) bool, contentHandler func(c *Context)) *mux.Route

func (*Server) HandleFuncCustomAuth

func (s *Server) HandleFuncCustomAuth(auth auth.Authenticator, path, redirect string, f func(c *Context)) *mux.Route

func (*Server) HandleServiceFunc

func (s *Server) HandleServiceFunc(path string, f func(c *ServiceRequest) *ServiceResponse) *mux.Route

func (*Server) HandleServiceFuncWithAuth

func (s *Server) HandleServiceFuncWithAuth(path string, f func(c *ServiceRequest) *ServiceResponse) *mux.Route

func (*Server) LoadTemplatesFS

func (s *Server) LoadTemplatesFS(fs http.FileSystem, base string) (*template.Template, error)

func (*Server) Router

func (s *Server) Router() *mux.Router

func (*Server) Serve

func (s *Server) Serve() error

Serve content forever

func (*Server) ServeTLS

func (s *Server) ServeTLS(keyPath string, certPath string) error

func (*Server) ServeTLSRedirect

func (s *Server) ServeTLSRedirect(address string) error

func (*Server) SetDebug

func (s *Server) SetDebug(enable bool)

func (*Server) SetLogger added in v0.2.1

func (s *Server) SetLogger(logger log.Logger)

func (*Server) SetTemplatePath

func (s *Server) SetTemplatePath(tmplPath string)

func (*Server) SetTheme

func (s *Server) SetTheme(themePath string)

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context)

func (*Server) WithAuth

func (s *Server) WithAuth(auth auth.Authenticator) *Server

func (*Server) WithDebug

func (s *Server) WithDebug(debugURL string) *Server

func (*Server) WithHealthCheck

func (s *Server) WithHealthCheck(version, date string, status func() (bool, string))

func (*Server) WithMetadata

func (s *Server) WithMetadata(meta map[string]string) *Server

func (*Server) WithRootFileSystem

func (s *Server) WithRootFileSystem(fs http.FileSystem) *Server

func (*Server) WithStaticFiles

func (s *Server) WithStaticFiles(prefix string) *Server

func (*Server) WithTemplateFuncs

func (s *Server) WithTemplateFuncs(funcs template.FuncMap) *Server

func (*Server) WithTheme

func (s *Server) WithTheme(themeURL string) *Server

func (*Server) WithVersion added in v0.2.2

func (s *Server) WithVersion(version int64) *Server

type ServiceRequest

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

func (*ServiceRequest) Form

func (c *ServiceRequest) Form() (*Options, error)

func (*ServiceRequest) GetObject

func (c *ServiceRequest) GetObject(object interface{}) error

func (*ServiceRequest) GetRequest

func (c *ServiceRequest) GetRequest() *http.Request

func (*ServiceRequest) GetUser

func (c *ServiceRequest) GetUser() string

func (*ServiceRequest) Options

func (c *ServiceRequest) Options() *Options

Options get compound options

func (*ServiceRequest) Query

func (c *ServiceRequest) Query() *Options

func (*ServiceRequest) ReplyMessage

func (c *ServiceRequest) ReplyMessage(code int, msg string) *ServiceResponse

func (*ServiceRequest) ReplyObject

func (c *ServiceRequest) ReplyObject(code int, obj interface{}) *ServiceResponse

func (*ServiceRequest) Vars

func (c *ServiceRequest) Vars() *Options

type ServiceResponse

type ServiceResponse struct {
	Code    int
	Message []byte
}

func (*ServiceResponse) WriteResponse

func (s *ServiceResponse) WriteResponse(w http.ResponseWriter)

type ServiceStatus

type ServiceStatus struct {
	Message string
}

type SnapBaseContent

type SnapBaseContent struct {
	Theme   string
	Content interface{}
}

type SnapContent

type SnapContent struct {
	User           string
	UserProperties map[string]string
	Theme          string
	Meta           map[string]string
	Content        interface{}
	Version        int64
}

type Vars

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

func (Vars) Get

func (c Vars) Get(k string) (string, bool)

Directories

Path Synopsis
examples
pkg

Jump to

Keyboard shortcuts

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