|
2 veckor sedan | |
---|---|---|
auth | 6 månader sedan | |
cmd | 3 veckor sedan | |
config | 3 veckor sedan | |
docs | 4 månader sedan | |
errors | 6 månader sedan | |
extension | 3 veckor sedan | |
fixtures/templates | 1 år sedan | |
installer | 3 veckor sedan | |
internal | 5 månader sedan | |
launcher | 2 veckor sedan | |
mappers | 1 år sedan | |
middleware | 8 månader sedan | |
render | 10 månader sedan | |
session | 6 månader sedan | |
status | 1 år sedan | |
tests | 3 veckor sedan | |
utils | 10 månader sedan | |
vfs | 1 år sedan | |
.gitignore | 1 år sedan | |
LICENSE | 1 månad sedan | |
Makefile | 6 månader sedan | |
README.md | 4 månader sedan | |
api.go | 3 veckor sedan | |
application.go | 3 veckor sedan | |
capstan_test.go | 1 år sedan | |
context.go | 9 månader sedan | |
extension.go | 2 månader sedan | |
funcs.go | 1 månad sedan | |
go.mod | 2 veckor sedan | |
go.sum | 2 veckor sedan | |
http.go | 1 år sedan | |
netlisten_unix.go | 5 månader sedan | |
netlisten_windows.go | 1 år sedan | |
network.go | 5 månader sedan | |
network_unix.go | 1 år sedan | |
network_windows.go | 1 år sedan | |
package_test.go | 6 månader sedan | |
proxy.go | 3 veckor sedan | |
route.go | 3 veckor sedan | |
router.go | 1 månad sedan | |
router_bind.go | 1 månad sedan | |
router_listen.go | 5 månader sedan | |
router_private.go | 1 månad sedan | |
router_trie.go | 1 år sedan | |
router_utils.go | 1 år sedan | |
routergroup.go | 1 månad sedan | |
service.go | 5 månader sedan | |
signals.go | 3 veckor sedan | |
trie.go | 4 månader sedan | |
trie_test.go | 5 månader sedan | |
types.go | 6 månader sedan | |
urls.go | 2 veckor sedan | |
urls_test.go | 1 år sedan | |
vfs.go | 1 år sedan |
Capstan is a framework that aims to simplify complex use cases that reach beyond basic HTTP handlers, such as dependency management, and reduces the amount of code required as handlers grow and evolve beyond what is possible with the standard library. It provides helpers for common (and not so common) tasks, including graceful shutdowns, rebinding mount points, and more. Capstan uses go-chi for its route muxer. Philosophically, Capstan is inspired in part by Flask and Flask-Classy.
Capstan is not for you if you require handlers that retain compatibility with
net/http
. Though this could (and arguably should) be rememdied by using the
context
package, there are no plans currently to reimplement Capstan in this
fashion. Be ware! The Capstan API may appear simple at first blush, but it has a
depth and complexity that may not be viable for all projects. If you need a
simple API server, consider using go-chi
directly.
Bear in mind that the project is still very much in its infancy, and APIs are subject to change. There are numerous rough patches and sharp objects in the code.
Capstan's implementation comprises these features (and some anti-features):
Get
or Post
. Controllers therefore should be highly focused to a
single endpoint. (This isn't strictly necessary; see below.)Binder
interface,
MapperHandler
interface (work-in-progress), or by naming functions
according to the pattern <Method><Name><SlashState>
. This will become
clearer later in the documentation once it's written.Templater
interface. We
primarily encourage the use of pongo2 but provide wrappers for Go's built in
template rendering library. (Note: Using anything but pongo2 may limit
features available to templates, and using multiple template types in a
single project will present unique challenges and difficulties that, while
not insurmountable, are unnecessary and will be frought with sleepless
nights.)Router.Unmount
) or
renamed (Router.ReplacePath
).url()
and external()
. URLs are typically
mapped as [<prefix>.]<name>.<suffix>
; for example, if you have a controller
IndexController
and were accessing its Get
method, you would retrieve the
mapped URL by calling url("IndexController:get")
. Custom url()
binding is
provided by Router.ManualBind
. The <prefix>
is dictated by router groups
or through manual declaration at controller bind time. Naming conflicts can
be resolved during controller initialization manually, by providing a
symbolic name for the controller, or automatically through a numeric suffix
(e.g. IndexController2
).?
, !
or +
syntax. ?
indicates a trailing slash is optional. !
indicates a trailing slash must
not be provided (and a 404 is generated). +
indicates a trailing slash is
mandatory for subroute declarations (but is typically unnecessary).
Otherwise, if a route is declared with a trailing slash it is considered
mandatory and requests made without the trailing slash will result in a 404.
Optional slashes (indicated by a trailing ?
) can either have a
slash-provided or slash-absent default state; if a ?
is immediately
preceded by a slash, e.g. /?
, a redirection route will be created that
directs requests minus the trailing slash to a route that has the trailing
slash appended. Likewise, if there is no slash preceding the ?
, the
redirection route will direct requests with a trailing /
to the route
without it. You should use optional slash routes sparingly since this does
necessitate the generation of (potentially) superfluous routes.<
and >
to delineate route variables and deliberately introduce
incompatibilities with go-chi
to prevent confusion. Variable types are
declared with a :<type>
suffix, e.g. /path/<name:string>
, which would
create a parameterized variable of the name name
and type string
. Regex
is passed through to go-chi
.go-chi
's groups and provide methods namespacing them from
other routes both for url()
template function(s) and for isolation.go-chi
and provides slight
modifications for middleware supplied by go-chi
(such as supporting the
NO_COLOR
envvar for go-chi
's logger).RunAs
configuration option and running initially as the root user will provoke
Capstan into first binding to its configured ports then dropping privileges.
This is useful for listening on lower ports, such as port 80 and 443, and
although Capstan is fully capable of doing so, it is advisable to use reverse
proxies in front of a Capstan application.utils
subpackage.package main
import (
"git.destrealm.org/go/capstan"
)
type IndexController struct {
capstan.BaseController
Server capstan.Server `inject:"app"`
}
func (c *IndexController) Index(ctx capstan.Context) error {
return ctx.Write([]byte("Hello! Listening on address: "+c.Server.Config().ListenAddress))
}
func main() {
app := capstan.New(&capstan.ServerConfig{
ListenAddress: ":8080",
})
app.Bind(&IndexController{
BaseController: capstan.BaseController{
Path: "/",
},
})
app.Listen()
}
The Capstan source distribution contains documentation under the docs/
subdirectory. Presently, this documentation is somewhat sparse but the
basic-usage.md
file contains a beginner's guide adequate enough to get
started. There is also a small but mostly complete discussion on Capstan's
dependency injection framework.
Capstan is licensed under the fairly liberal and highly permissive NCSA license. We prefer this license as it combines the best of BSD and MIT licenses while also providing coverage for associated documentation and other works that are not strictly considered original source code. Consequently, all Capstan documentation is likewise covered under the same license as the codebase itself.
As with BSD-like licenses, attribution is, of course, required.