Giter Club home page Giter Club logo

rocket's Introduction

rocket

Build Status Build status Go Report Card codecov GoDoc

Rocket is a web framework inspired by rocket-rs.

Document: https://dannypsnl.github.io/rocket

Install

go get github.com/dannypsnl/rocket

Usage

Import

package example

import (
	"github.com/dannypsnl/rocket"
)

Create Handler

package example

import (
	"fmt"

	"github.com/dannypsnl/rocket"
)

type User struct {
	Name string `route:"name"`
	Age int `route:"age"`
}

var hello = rocket.Get("/name/:name/age/:age", func(u *User) string {
	return fmt.Sprintf(
		"Hello, %s.\nYour age is %d.",
		u.Name, u.Age)
})
  • First argument of handler creator is a route string can have variant part.
  • Second argument is handler function.
    • handler function can have a argument, that is a type you define to be request context Tag in your type will load request value into it!
      • route tag is route:"name", if route contains /:name, then value is request URL at this place e.g. /Danny will let value of name is string Danny
      • form tag is form:"key", it get form value from form request
      • json tag is json:"key", it get POST/PUT body that is JSON
    • return type of handler function is meaningful
      • response.Html: returns text as HTML(set Content-Type to text/html)
      • response.Json: returns text as JSON(set Content-Type to application/json)
      • string: returns text as plain text(set Content-Type to text/plain)
  • handler creator name is match to HTTP Method

Mount and Start

rocket.Ignite(":8080"). // Setting port
	Mount("/", index, static).
	Mount("/hello", hello).
	Launch() // Start Serve
  • func Ignite get a string to describe port.
  • Launch start the server.
  • Mount receive a base route and a handler that exactly handle route. You can emit 1 to N handlers in one Mount Note: Base route can't put parameters part. That is illegal route.

Fairing(experimental release)

  • OnResponse and OnRequest An easy approach:
     import "net/http"
     import "github.com/dannypsnl/rocket/response"
     import "github.com/dannypsnl/rocket/fairing"
    
     type Logger struct {
     	fairing.Fairing
     }
     func (l *Logger) OnRequest(r *http.Request) *http.Request {
     	log.Printf("request: %#v\n", r)
     	return r
     }
     func (l *Logger) OnResponse(r *response.Response) *response.Response {
     	log.Printf("response: %#v\n", r)
     	return r
     }
     rocket.Ignite(":6060").
     	// Use Attach to emit, you can call Attach multiple time, but carefully at modify data, that might cause problem
     	Attach(&Logger{}).
     	// Mount(...)
     	Launch()

rocket's People

Contributors

dannypsnl avatar shana0440 avatar

Watchers

 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.