https://github.com/ynori7/lilypad
A lightweight web application framework in Go
https://github.com/ynori7/lilypad
framework golang webapp
Last synced: 3 months ago
JSON representation
A lightweight web application framework in Go
- Host: GitHub
- URL: https://github.com/ynori7/lilypad
- Owner: ynori7
- License: mit
- Created: 2020-06-01T07:03:03.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-06T13:18:18.000Z (about 2 years ago)
- Last Synced: 2024-03-06T14:35:21.885Z (about 2 years ago)
- Topics: framework, golang, webapp
- Language: Go
- Homepage:
- Size: 67.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lilypad [](https://pkg.go.dev/github.com/ynori7/lilypad) [](https://travis-ci.com/github/ynori7/lilypad) [](https://coveralls.io/github/ynori7/lilypad?branch=master) [](https://goreportcard.com/report/github.com/ynori7/lilypad)
A lightweight web application framework in Go.
## Features
Here is a brief overview of the feature set provided by the Lilypad framework.
A detailed example can be found in [examples](/examples).
### Routing
The Lilypad framework provides a simple wrapper around the Gorilla mux router. This
allows you to use Lilypad's handlers to simplfy response writing and error handling.
The framework keeps a global router which allows you to register routes close to the
handlers which control them without having to pass a router around everywhere.
To add a route, simply register it like this:
```go
import "github.com/ynori7/lilypad/http"
...
http.RegisterRoutes(http.Route{
Path: "/hello/{name}",
Handler: Hello,
})
```
RegisterRoutes is variardic, so it's possible to register multiple routes at once:
```go
http.RegisterRoutes([]http.Route{
{
Path: "/hello/{name}",
Handler: Hello,
},
{
Path: "/goodbye/{name}",
Handler: Goodbye,
},
}...)
```
### Handlers
Lilypad's handler definition makes it easier to build your http handlers. You no longer
need to care about writing responses for ever place where the method exists. You simply
return either a `SuccessResponse` or an `ErrorResponse`.
For example:
```go
import (
"github.com/ynori7/lilypad/http"
)
func Hello(r http.Request) http.Response {
name := http.GetVar(r, "name")
if !isValidName(name) {
return http.ErrorResponse(errors.BadRequestError("Names should be non-empty and contain only letters"))
}
...
return SuccessResponse(resp)
```
It's also possible to return a `RedirectResponse` to redirect to another page.
You can also specify the cache duration like this:
```go
return SuccessResponse(resp).WithMaxAge(300)
```
Or any other arbitrary headers using the `WithHeaders()` method.
### Errors
The errors package provides a definition for HttpErros as well as convenient wrappers
for getting some of the most common error types.
One key attribute of the errors package is the HttpError's `Write()` method. This
method will present the error in a useful format depending on the configuration. In your
main.go, you can set errors to be presented in a JSON format, HTML format, or plaintext format.
```go
errors.UseMarkupErrors(errorHtmlTemplate)
//or
errors.UseJsonErrors()
//or
errors.UsePlaintextErrors() //the default
```
### Logging
The log package wraps the Sirupsen Logrus library and adds a few useful additions such as
the `WithRequest` method which returns a logger with some fields populated from the HTTP
request such as the client's IP address.
### Templating
The view package provides a minimalistic method to `RenderTemplate` which accepts a template
body as a string and the data used to render it.
Alternatively, you can use nested templates by registering your base layouts like this:
```go
view.SetLayoutDirectory("path/to/layouts")
```
This will configure the framework to load all files with the gohtml extension. Next you can
render a view like this:
```go
out, err := view.New("layoutName", "templates/specificTemplate.gohtml").Render(myData)
```
Here is an example layout.gohtml:
```gotemplate
{{ define "layoutName" }}
{{ template "body" . }}
```
And an example specificTemplate.gohtml:
```gotemplate
{{ define "body" }}
My Page!
blahblah
{{ end }}
```
It also has a global register of template functions which can be easily registered like this:
```go
view.RegisterGlobalTemplateFuncs(template.FuncMap{
"UppercaseFirstLetter": func(s string) string {
return strings.Title(strings.ToLower(s))
},
})
```
And from any template, the function can be used like this:
```gotemplate
{{ UppercaseFirstLetter .Str }}
```