rocket
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 (
rk "github.com/dannypsnl/rocket"
)Create Handler
package example
import (
"fmt"
rk "github.com/dannypsnl/rocket"
)
type User struct {
Name string `route:"name"`
Age int `route:"age"`
}
var hello = rk.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./Dannywill let value ofnameis stringDanny - 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
- route tag is
- return type of handler function is meaningful
- Html: returns text as HTML(set Content-Type to
text/html) - Json: returns text as JSON(set Content-Type to
application/json) - string: returns text as plain text(set Content-Type to
text/plain)
- Html: returns text as HTML(set Content-Type to
- 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!
- 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 at one
Mount
Note
- Base route can't put parameters part. That is illegal route.