本文整理匯總了Golang中github.com/go-martini/martini.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Start
func Start() {
r := martini.NewRouter()
m := martini.New()
m.Use(martini.Logger())
m.Use(martini.Recovery())
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
// Gitchain API
r.Post("/rpc", jsonRpcService().ServeHTTP)
r.Get("/info", info)
// Git Server
r.Post("^(?P<path>.*)/git-upload-pack$", func(params martini.Params, req *http.Request) string {
body, _ := ioutil.ReadAll(req.Body)
fmt.Println(req, body)
return params["path"]
})
r.Post("^(?P<path>.*)/git-receive-pack$", func(params martini.Params, req *http.Request) string {
fmt.Println(req)
return params["path"]
})
r.Get("^(?P<path>.*)/info/refs$", func(params martini.Params, req *http.Request) (int, string) {
body, _ := ioutil.ReadAll(req.Body)
fmt.Println(req, body)
return 404, params["path"]
})
log.Fatal(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", env.Port), m))
}
示例2: init
func init() {
m = martini.New()
//setup middleware
m.Use(martini.Recovery())
m.Use(martini.Logger())
m.Use(martini.Static("public"))
}
示例3: main
func main() {
conf.Env().Flag()
r := martini.NewRouter()
m := martini.New()
if conf.UBool("debug") {
m.Use(martini.Logger())
}
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
session, err := mgo.Dial(conf.UString("mongo.uri"))
if err != nil {
panic(err)
}
session.SetMode(mgo.Monotonic, true)
db := session.DB(conf.UString("mongodb.database"))
logger := log.New(os.Stdout, "\x1B[36m[cdn] >>\x1B[39m ", 0)
m.Map(logger)
m.Map(db)
r.Group("", cdn.Cdn(cdn.Config{
MaxSize: conf.UInt("maxSize"),
ShowInfo: conf.UBool("showInfo"),
TailOnly: conf.UBool("tailOnly"),
}))
logger.Println("Server started at :" + conf.UString("port", "5000"))
_err := http.ListenAndServe(":"+conf.UString("port", "5000"), m)
if _err != nil {
logger.Printf("\x1B[31mServer exit with error: %s\x1B[39m\n", _err)
os.Exit(1)
}
}
示例4: Test_GitHubAuth
func Test_GitHubAuth(t *testing.T) {
recorder := httptest.NewRecorder()
secret := "secret"
signature := "sha1=5d61605c3feea9799210ddcb71307d4ba264225f"
body := "{}"
m := martini.New()
m.Use(GitHub(secret))
m.Use(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hello"))
})
r, _ := http.NewRequest("GET", "foo", bytes.NewReader([]byte(body)))
r.Header.Set("X-Hub-Signature", signature)
m.ServeHTTP(recorder, r)
if recorder.Code == 401 {
t.Error("Response is 401")
}
if recorder.Body.String() != "hello" {
t.Error("Auth failed, got: ", recorder.Body.String())
}
}
示例5: TestRestErrorResult
func TestRestErrorResult(t *testing.T) {
m := martini.New()
recorder := httptest.NewRecorder()
m.Use(rest.RestPostHandler())
m.Use(func(c martini.Context, res http.ResponseWriter, req *http.Request) {
err := &rest.RestError{-1000, "12"}
c.MapTo(err, (*error)(nil))
})
m.ServeHTTP(recorder, (*http.Request)(nil))
if recorder.Code != http.StatusOK {
t.Error("failed status")
return
}
if recorder.Header().Get("Content-Type") != "application/json; charset=utf-8" {
t.Error("failed content type")
return
}
var returnObj rest.RestReturnObj
if err := json.Unmarshal(recorder.Body.Bytes(), &returnObj); err != nil {
t.Error("json decode failed")
return
}
if returnObj.ErrorCode != -1000 {
t.Error("error code failed")
return
}
}
示例6: HandlerFromMartini
func HandlerFromMartini(handler martini.Handler) http.Handler {
m := martini.New()
m.Use(handler)
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
m.ServeHTTP(rw, req)
})
}
示例7: New
// create new helper, this object will be used for globar service for martini middleware
func New() *MartiniHelper {
this := &MartiniHelper{inject.New()}
retHandler := martini.New().Get(reflect.TypeOf(martini.ReturnHandler(nil))).Interface()
// retHandler := martini.defaultReturnHandler()
this.Map(retHandler)
return this
}
示例8: BenchmarkCodegangstaMartini_Middleware
func BenchmarkCodegangstaMartini_Middleware(b *testing.B) {
martiniMiddleware := func(rw http.ResponseWriter, r *http.Request, c martini.Context) {
c.Next()
}
r := martini.NewRouter()
m := martini.New()
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Action(r.Handle)
r.Get("/action", helloHandler)
rw, req := testRequest("GET", "/action")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.ServeHTTP(rw, req)
if rw.Code != 200 {
panic("no good")
}
}
}
示例9: BenchmarkCodegangstaNegroni_Composite
func BenchmarkCodegangstaNegroni_Composite(b *testing.B) {
namespaces, resources, requests := resourceSetup(10)
martiniMiddleware := func(rw http.ResponseWriter, r *http.Request, c martini.Context) {
c.Next()
}
handler := func(rw http.ResponseWriter, r *http.Request, c *martiniContext) {
fmt.Fprintf(rw, c.MyField)
}
r := martini.NewRouter()
m := martini.New()
m.Use(func(rw http.ResponseWriter, r *http.Request, c martini.Context) {
c.Map(&martiniContext{MyField: r.URL.Path})
c.Next()
})
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Use(martiniMiddleware)
m.Action(r.Handle)
for _, ns := range namespaces {
for _, res := range resources {
r.Get("/"+ns+"/"+res, handler)
r.Post("/"+ns+"/"+res, handler)
r.Get("/"+ns+"/"+res+"/:id", handler)
r.Put("/"+ns+"/"+res+"/:id", handler)
r.Delete("/"+ns+"/"+res+"/:id", handler)
}
}
benchmarkRoutes(b, m, requests)
}
示例10: Test_BasicAuth
func Test_BasicAuth(t *testing.T) {
recorder := httptest.NewRecorder()
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte("foo:bar"))
m := martini.New()
m.Use(Basic("foo", "bar"))
m.Use(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hello"))
})
r, _ := http.NewRequest("GET", "foo", nil)
m.ServeHTTP(recorder, r)
if recorder.Code != 401 {
t.Error("Response not 401")
}
if recorder.Body.String() == "hello" {
t.Error("Auth block failed")
}
recorder = httptest.NewRecorder()
r.Header.Set("Authorization", auth)
m.ServeHTTP(recorder, r)
if recorder.Code == 401 {
t.Error("Response is 401")
}
if recorder.Body.String() != "hello" {
t.Error("Auth failed, got: ", recorder.Body.String())
}
}
示例11: init
func init() {
m = martini.New()
// I could probably just use martini.Classic() instead of configure these manually
// Static files
m.Use(martini.Static(`public`))
// Setup middleware
m.Use(martini.Recovery())
m.Use(martini.Logger())
// m.Use(auth.Basic(AuthToken, ""))
m.Use(MapEncoder)
// Setup routes
r := martini.NewRouter()
r.Get(`/albums`, server.GetAlbums)
r.Get(`/albums/:id`, server.GetAlbum)
r.Post(`/albums`, server.AddAlbum)
r.Put(`/albums/:id`, server.UpdateAlbum)
r.Delete(`/albums/:id`, server.DeleteAlbum)
// Inject database
m.MapTo(server.DBInstance, (*server.DB)(nil))
// Add the router action
m.Action(r.Handle)
}
示例12: main
// How to use Martini properly with Defer Panic client library
func main() {
dps := deferstats.NewClient("z57z3xsEfpqxpr0dSte0auTBItWBYa1c")
go dps.CaptureStats()
m := martini.New()
r := martini.NewRouter()
r.Get("/panic", func() string {
panic("There is no need for panic")
return "Hello world!"
})
r.Get("/slow", func() string {
time.Sleep(5 * time.Second)
return "Hello world!"
})
r.Get("/fast", func() string {
return "Hello world!"
})
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
m.Use(middleware.Wrapper(dps))
m.RunOnAddr(":3000")
}
示例13: init
func init() {
m = martini.New()
//set up middleware
m.Use(martini.Recovery())
m.Use(martini.Logger())
m.Use(auth.Basic(AuthToken, ""))
m.Use(MapEncoder)
// set up routes
r := martini.NewRouter()
r.Get(`/albums`, GetAlbums)
r.Get(`/albums/:id`, GetAlbum)
r.Post(`/albums`, AddAlbum)
r.Put(`/albums/:id`, UpdateAlbum)
r.Delete(`/albums/:id`, DeleteAlbum)
// inject database
// maps db package variable to the DB interface defined in data.go
// The syntax for the second parameter may seem weird,
// it is just converting nil to the pointer-to-DB-interface type,
// because all the injector needs is the type to map the first parameter to.
m.MapTo(db, (*DB)(nil))
// add route action
// adds the router’s configuration to the list of handlers that Martini will call.
m.Action(r.Handle)
}
示例14: Test_ResponseWriter_Hijack
func Test_ResponseWriter_Hijack(t *testing.T) {
hijackable := newHijackableResponse()
m := martini.New()
m.Use(All())
m.Use(func(rw http.ResponseWriter) {
if hj, ok := rw.(http.Hijacker); !ok {
t.Error("Unable to hijack")
} else {
hj.Hijack()
}
})
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error(err)
}
r.Header.Set(HeaderAcceptEncoding, "gzip")
m.ServeHTTP(hijackable, r)
if !hijackable.Hijacked {
t.Error("Hijack was not called")
}
}
示例15: startMartini
func startMartini() {
mux := martini.NewRouter()
mux.Get("/hello", martiniHandlerWrite)
martini := martini.New()
martini.Action(mux.Handle)
http.ListenAndServe(":"+strconv.Itoa(port), martini)
}