Code Monkey home page Code Monkey logo

fux's Introduction

Fux

An interface for works with Gorilla mux

Installation

Install with

go get -u github.com/GoPersian/fux

Examples

func main() {
    f := fux.New()
    f.Get("/", HomeHandler)
    f.Post("/products", ProductsHandler)
    f.Get("/articles", ArticlesHandler)
    f.Run(":8080")
}

Paths can have variables. They are defined using the format {name} or {name:pattern}. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:

f := fux.New()
f.Get("/products/{key}", ProductHandler)
f.Get("/articles/{category}/", ArticlesCategoryHandler)
f.Get("/articles/{category}/{id:[0-9]+}", ArticleHandler)
f.Run(":8080")

The names are used to create a map of route variables which can be retrieved calling mux.Vars():

func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
    vars := fux.Vars(r)
    // w.WriteHeader(http.StatusOK)
    fux.Response(w).Status(http.StatusOK)
    fmt.Fprintf(w, "Category: %v\n", vars["category"])
}

And this is all you need to know about the basic usage. More advanced options are explained below.

It is possible to combine several matchers in a single route:

f.Post("/products", ProductsHandler).
  Host("www.example.com").
  Schemes("http")

Define a route with multi-methods:

f.HandleFunc("/multi", MultiHandler).Methods(http.MethodPost, http.MethodGet)

Handler

Set content-type and status:

func UsersHandler(w http.ResponseWriter, request *http.Request)  {
    fux.Response(w).ContentTypeJson().Status(http.StatusOK)
    // OR
    fux.Response(w).ContextTypeHtml().Status(http.StatusOK) 
    // ...
}

Set or add custom headers:

func UsersHandler(w http.ResponseWriter, request *http.Request)  {
    fux.Response(w).Header().Add("key", "value").ContentTypeJson().Status(http.StatusOK)
    // OR
    fux.Response(w).Header().Set("key", "value").ContextTypeHtml().Status(http.StatusOK) 
    // ...
}

File Server

Example:

f.Handle("/", http.FileServer(http.Dir("static")))
// OR
f.FileServer("/", http.Dir("static"))

fux's People

Contributors

kamandlou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.