All Projects → zpatrick → fireball

zpatrick / fireball

Licence: MIT License
Go web framework with a natural feel

Programming Languages

go
31211 projects - #10 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to fireball

CoreLooper
No description or website provided.
Stars: ✭ 34 (-40.35%)
Mutual labels:  handler
GCommands
Powerful and flexible discord.js framework.
Stars: ✭ 38 (-33.33%)
Mutual labels:  handler
http-multiserver.cr
Mount multiple web applications 🚦
Stars: ✭ 23 (-59.65%)
Mutual labels:  handler
jwt-session
JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN. The implementation follow the SessionHandlerInterface.
Stars: ✭ 42 (-26.32%)
Mutual labels:  handler
Hikari
simple discord.js music bot using distube 🎵 | Stage channel support!
Stars: ✭ 19 (-66.67%)
Mutual labels:  handler
breaker
🚧 Flexible mechanism to make execution flow interruptible.
Stars: ✭ 100 (+75.44%)
Mutual labels:  awesome-go
jackliu-golang-notes
Jack Liu's Golang personal summary main points notes, notes to fast understand golang
Stars: ✭ 17 (-70.18%)
Mutual labels:  awesome-go
ApexTriggerHandler
Another library implements Apex trigger handler design pattern.
Stars: ✭ 40 (-29.82%)
Mutual labels:  handler
awesome-latam
Colección de contenidos y recursos en Español para desarrolladores de Golang. Awesome oficial: https://awesome-go.com
Stars: ✭ 81 (+42.11%)
Mutual labels:  awesome-go
chef-handler-sns
Chef report handler to send Amazon SNS notifications on failures or changes.
Stars: ✭ 12 (-78.95%)
Mutual labels:  handler
xml-spac
Handle streaming XML data with declarative, composable parsers
Stars: ✭ 39 (-31.58%)
Mutual labels:  handler
WSocket
Simple WSGI HTTP + Websocket Server, Framework, Middleware And App.
Stars: ✭ 19 (-66.67%)
Mutual labels:  handler
awesome
collection of awesome lists
Stars: ✭ 178 (+212.28%)
Mutual labels:  awesome-go
gonreli
use go http handlers in nodejs
Stars: ✭ 14 (-75.44%)
Mutual labels:  handler
fine
🧹 Gracefully shutdown Node.js application: help you handle exit signals and cleanup
Stars: ✭ 20 (-64.91%)
Mutual labels:  handler
Android-System-Related-Charts
各种与Android系统相关的框架图、流程图
Stars: ✭ 52 (-8.77%)
Mutual labels:  handler
go-pattern-match
Pattern matchings for Go.
Stars: ✭ 182 (+219.3%)
Mutual labels:  awesome-go
node-htmlmetaparser
A `htmlparser2` handler for parsing rich metadata from HTML. Includes HTML metadata, JSON-LD, RDFa, microdata, OEmbed, Twitter cards and AppLinks.
Stars: ✭ 44 (-22.81%)
Mutual labels:  handler
mongoose-slug-plugin
Slugs for Mongoose with history and i18n support (uses speakingurl by default, but you can use any slug library such as limax, slugify, mollusc, or slugme)
Stars: ✭ 21 (-63.16%)
Mutual labels:  handler
telegram
📚 Golang bindings for Telegram API
Stars: ✭ 15 (-73.68%)
Mutual labels:  awesome-go

Fireball

MIT License Go Report Card Go Doc

Overview

Fireball is a package for Go web applications. The primary goal of this package is to make routing, response writing, and error handling as easy as possible for developers, so they can focus more on their application logic, and less on repeated patterns.

Installation

To install this package, run:

go get github.com/zpatrick/fireball

Getting Started

The following snipped shows a simple "Hello, World" application using Fireball:

package main

import (
  "github.com/zpatrick/fireball"
  "net/http"
)

func index(c *fireball.Context) (fireball.Response, error) {
  return fireball.NewResponse(200, []byte("Hello, World!"), nil), nil
}

func main() {
  indexRoute := &fireball.Route{
    Path: "/",
    Handlers: fireball.Handlers{
      "GET": index,
    },
  }

  routes := []*fireball.Route{indexRoute}
  app := fireball.NewApp(routes)
  http.ListenAndServe(":8000", app)
}

This will run a new webserver at localhost:8000

Handlers

Handlers perform the business logic associated with requests. Handlers take a Context object and returns either a Response or an error.

HTTP Response

The HTTP Response is a simple object that implements the Response interface. When the Write call is executed, the specified Body, Status, and Headers will be written to the http.ResponseWriter.

Examples:

func Index(c *fireball.Context) (fireball.Response, error) {
    return fireball.NewResponse(200, []byte("Hello, World"), nil), nil
}
func Index(c *fireball.Context) (fireball.Response, error) {
    html := []byte("<h1>Hello, World</h1>")
    return fireball.NewResponse(200, html, fireball.HTMLHeaders), nil
}

HTTP Error

If a Handler returns a non-nil error, the Fireball Application will call its ErrorHandler function. By default (if your Application object uses the DefaultErrorHandler), the Application will check if the error implements the Response interface. If so, the the error's Write function will be called. Otherwise, a 500 with the content of err.Error() will be written.

The HTTPError is a simple object that implements both the Error and Response interfaces. When the Write is executed, the specified status, error, and headers will be written to the http.ResponseWriter.

Examples:

func Index(c *fireball.Context) (fireball.Response, error) {
    return nil, fmt.Errorf("an error occurred")
}
func Index(c *fireball.Context) (fireball.Response, error) {
    if err := do(); err != nil {
        return nil, fireball.NewError(500, err, nil)
    }
    
    ...
}

Routing

Basic Router

By default, Fireball uses the BasicRouter object to match requests to Route objects. The Route's Path field determines which URL patterns should be dispached to your Route. The Route's Handlers field maps different HTTP methods to different Handlers.

You can use :variable notation in the Path to match any string that doesn't contain a "/" character. The variables defined in the Route's Path field can be accessed using the Context object.

Example:

route := &Fireball.Route{
  Path: "/users/:userID/orders/:orderID",
  Methods: fireball.Handlers{
    "GET": printUserOrder,
  },
}

func printUserOrder(c *fireball.Context) (fireball.Response, error) {
    userID := c.PathVariables["userID"]
    orderID := c.PathVariables["orderID"]
    message := fmt.Sprintf("User %s ordered item %s", userID, orderID)
    
    return fireball.NewResponse(200, []byte(message), nil)
}

Static Routing

The built-in FileServer can be used to serve static content. The follow snippet would serve files from the static directory:

  app := fireball.NewApp(...)
  http.Handle("/", app)

  fs := http.FileServer(http.Dir("static"))
  http.Handle("/static/", http.StripPrefix("/static", fs))
  
  http.ListenAndServe(":8000", nil)

If the application workspace contained:

app/
    main.go
    static/
        hello_world.txt

A request to /static/hello_world.txt would serve the desired file.

HTML Templates

By default, Fireball uses the GlobParser to render HTML templates. This object recursively searches a given directory for template files matching the given glob pattern. The default root directory is "views", and the default glob pattern is "*.html" The name of the templates are path/from/root/directory + filename.

For example, if the filesystem contained:

views/
    index.html
    partials/
        login.html

The templates names generated would be "index.html", and "partials/login.html". The Context contains a helper function, HTML, which renders templates as HTML.

Example:

func Index(c *fireball.Context) (fireball.Response, error) {
    data := "Hello, World!"
    return c.HTML(200, "index.html", data)
}

Decorators

Decorators can be used to wrap additional logic around Handlers. Fireball has some built-in decorators:

In addition to Decorators, the Before and After functions on the Application object can be used to perform logic when the request is received and after the response has been sent.

Examples & Extras

License

This work is published under the MIT license.

Please see the LICENSE file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].