本文整理匯總了Golang中github.com/codegangsta/martini.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: BenchmarkCodegangstaMartini_Composite
func BenchmarkCodegangstaMartini_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)
}
示例2: startGUI
func startGUI(addr string, m *model.Model) {
router := martini.NewRouter()
router.Get("/", getRoot)
router.Get("/rest/version", restGetVersion)
router.Get("/rest/model", restGetModel)
router.Get("/rest/connections", restGetConnections)
router.Get("/rest/config", restGetConfig)
router.Get("/rest/need", restGetNeed)
router.Get("/rest/system", restGetSystem)
fs, err := embed.Unpack(Resources)
if err != nil {
panic(err)
}
go func() {
mr := martini.New()
mr.Use(embeddedStatic(fs))
mr.Use(martini.Recovery())
mr.Action(router.Handle)
mr.Map(m)
err := http.ListenAndServe(addr, mr)
if err != nil {
warnln("GUI not possible:", err)
}
}()
}
示例3: 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")
}
}
}
示例4: 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())
}
}
示例5: TestMartiniSelectiveRouter
func TestMartiniSelectiveRouter(t *testing.T) {
for _, test := range tests {
w := httptest.NewRecorder()
r := martini.NewRouter()
done := make(chan bool)
selectRoute(r, test.ExpectedMethod, func(rq *http.Request) {
done <- true
})
req, err := http.NewRequest(test.Method, "/", nil)
if err != nil {
t.Fatal(err)
}
OverrideRequestMethod(req, test.OverrideMethod)
m := martini.New()
m.Use(Override())
m.Action(r.Handle)
go m.ServeHTTP(w, req)
select {
case <-done:
case <-time.After(30 * time.Millisecond):
t.Errorf("Expected router to route to %s, got something else (%v).", test.ExpectedMethod, test)
}
}
}
示例6: main
func main() {
// Load DB
options := &bolt.Options{Timeout: 2 * time.Second}
dbPath := path.Join(os.Getenv("BOLT_PATH"), "reminder.db")
db, err := bolt.Open(dbPath, 0600, options)
if err != nil {
log.Fatalf("Error opening bolt DB: %v", err)
}
defer db.Close()
// Schedule all (non-cancelled) Reminders
rems, err := remind.GetAllReminders(db)
if err != nil {
log.Fatalf("Error getting reminders: %v\n", err)
}
runningReminders.Schedule(db, rems)
//
// Router, etc
//
r := martini.NewRouter()
m := martini.New()
m.Use(martini.Logger())
m.Use(martini.Recovery())
m.Action(r.Handle)
m.Map(db)
r.Post("/sms", incomingSMS)
m.Run()
}
示例7: main
func main() {
m := martini.New()
m.Use(martini.Recovery())
m.Use(martini.Logger())
m.Use(nrscstatic.NrscStatic("public", true))
http.ListenAndServe(":8080", m)
}
示例8: startGUI
func startGUI(cfg GUIConfiguration, m *Model) {
router := martini.NewRouter()
router.Get("/", getRoot)
router.Get("/rest/version", restGetVersion)
router.Get("/rest/model", restGetModel)
router.Get("/rest/connections", restGetConnections)
router.Get("/rest/config", restGetConfig)
router.Get("/rest/config/sync", restGetConfigInSync)
router.Get("/rest/system", restGetSystem)
router.Get("/rest/errors", restGetErrors)
router.Post("/rest/config", restPostConfig)
router.Post("/rest/restart", restPostRestart)
router.Post("/rest/reset", restPostReset)
router.Post("/rest/error", restPostError)
router.Post("/rest/error/clear", restClearErrors)
go func() {
mr := martini.New()
if len(cfg.User) > 0 && len(cfg.Password) > 0 {
mr.Use(basic(cfg.User, cfg.Password))
}
mr.Use(embeddedStatic())
mr.Use(martini.Recovery())
mr.Use(restMiddleware)
mr.Action(router.Handle)
mr.Map(m)
err := http.ListenAndServe(cfg.Address, mr)
if err != nil {
warnln("GUI not possible:", err)
}
}()
}
示例9: martiniInit
func martiniInit() {
m := martini.New()
r := martini.NewRouter()
m.Map(db)
//m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
//m.Use(oauth2.Github(&oauth2.Options{
// ClientId: "64a641523f31dd5bfe4b",
// ClientSecret: "4fe3fbbca262835c424ca6a80aec6c6cb4228037",
// RedirectURL: "http://localhost:2714/github_callback",
// Scopes: []string{"user:email"},
//}))
//r.Get("/", index)
r.Get("/comments/:forum/:page", GetAllCommentsResource)
r.Post("/comment", PostCommentHandler)
//r.Get("/github_redirect", RedirectUrl)
//r.Get("/github_callback", func(request *http.Request) string {
// request.ParseForm()
// return fmt.Sprintf("%+v", request)
//})
m.Use(martini.Static("ui"))
m.Action(r.Handle)
fmt.Println("Running on port " + viper.GetString("port"))
sio.Handle("/", m)
//http.Handle("/", m)
//http.ListenAndServe(":"+viper.GetString("port"), m)
}
示例10: startGUI
func startGUI(addr string, m *Model) {
router := martini.NewRouter()
router.Get("/", getRoot)
router.Get("/rest/version", restGetVersion)
router.Get("/rest/model", restGetModel)
router.Get("/rest/connections", restGetConnections)
router.Get("/rest/config", restGetConfig)
router.Get("/rest/config/sync", restGetConfigInSync)
router.Get("/rest/need", restGetNeed)
router.Get("/rest/system", restGetSystem)
router.Get("/rest/errors", restGetErrors)
router.Post("/rest/config", restPostConfig)
router.Post("/rest/restart", restPostRestart)
router.Post("/rest/error", restPostError)
go func() {
mr := martini.New()
mr.Use(embeddedStatic())
mr.Use(martini.Recovery())
mr.Action(router.Handle)
mr.Map(m)
err := http.ListenAndServe(addr, mr)
if err != nil {
warnln("GUI not possible:", err)
}
}()
}
示例11: Test_Preflight
func Test_Preflight(t *testing.T) {
recorder := httptest.NewRecorder()
m := martini.New()
m.Use(Allow(&Options{
AllowAllOrigins: true,
AllowMethods: []string{"PUT", "PATCH"},
AllowHeaders: []string{"Origin", "X-whatever"},
}))
r, _ := http.NewRequest("OPTIONS", "foo", nil)
r.Header.Add(headerRequestMethod, "PUT")
r.Header.Add(headerRequestHeaders, "X-whatever")
m.ServeHTTP(recorder, r)
methodsVal := recorder.HeaderMap.Get(headerAllowMethods)
headersVal := recorder.HeaderMap.Get(headerAllowHeaders)
if methodsVal != "PUT,PATCH" {
t.Errorf("Allow-Methods is expected to be PUT,PATCH, found %v", methodsVal)
}
if headersVal != "X-whatever" {
t.Errorf("Allow-Headers is expected to be X-whatever, found %v", headersVal)
}
}
示例12: NewServer
// NewServer creates an instance of a martini server. The
// adapterInst parameter is the adapter type the server will
// use when dispatching requests.
func NewServer(adapterInst PanamaxAdapter) AdapterServer {
s := martini.New()
// Setup middleware
s.Use(martini.Recovery())
s.Use(martini.Logger())
s.Use(mapEncoder)
s.Use(func(c martini.Context, w http.ResponseWriter, r *http.Request) {
c.Map(adapterInst)
})
// Setup routes
router := martini.NewRouter()
router.Group(fmt.Sprintf("/%s", APIVersion), func(r martini.Router) {
r.Get(`/services`, getServices)
r.Get(`/services/:id`, getService)
r.Post(`/services`, createServices)
r.Put(`/services/:id`, updateService)
r.Delete(`/services/:id`, deleteService)
r.Get(`/metadata`, getMetadata)
})
// Add the router action
s.Action(router.Handle)
server := martiniServer{svr: s}
return &server
}
示例13: main
func main() {
Db = dbSetup()
m := martini.New()
//m.Use(martini.Logger())
m.Use(martini.Recovery())
m.Action(routes().Handle)
m.Run()
}
示例14: init
func init() {
m = martini.New()
r := martini.NewRouter()
r.Get("/v2/catalog", GetCatalog)
r.Put("/v2/service_instances/:id", CreateService)
r.Delete("/v2/service_instances/:id", DeleteService)
r.Put("/v2/service_instances/:instance_id/service_bindings/:id", BindService)
r.Delete("/v2/service_instances/:instance_id/service_bindings/:id", UnbindService)
m.Action(r.Handle)
}
示例15: Benchmark_WithoutCORS
func Benchmark_WithoutCORS(b *testing.B) {
recorder := httptest.NewRecorder()
m := martini.New()
b.ResetTimer()
for i := 0; i < 100; i++ {
r, _ := http.NewRequest("PUT", "foo", nil)
m.ServeHTTP(recorder, r)
}
}