本文整理汇总了Golang中github.com/plimble/ace.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: loadAce
func loadAce(routes []route) http.Handler {
router := ace.New()
for _, route := range routes {
router.Handle(route.method, route.path, []ace.HandlerFunc{aceHandle})
}
return router
}
示例2: main
func main() {
a := ace.New()
a.Use(gzip.Gzip(gzip.DefaultCompression))
a.GET("/ping", func(c *ace.C) {
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
})
// Listen and Server in 0.0.0.0:8080
a.Run(":8080")
}
示例3: newServer
func newServer(useGzip bool) *ace.ace {
r := ace.New()
if useGzip {
r.Use(Gzip(DefaultCompression))
}
r.GET("/", func(c *ace.C) {
c.Writer.Header().Set(headerContentLength, strconv.Itoa(len(testResponse)))
c.String(200, testResponse)
})
return r
}
示例4: TestRequestMethod
func TestRequestMethod(t *testing.T) {
g := ace.New()
g.Use(Cors(Options{
AllowMethods: []string{},
}))
assert := assert.New(t)
r := request(g, requestOptions{
Method: "OPTIONS",
URL: "/",
Headers: map[string]string{
"Origin": "http://maji.moe",
"Access-Control-Request-Method": "PUT",
},
})
assert.Equal("", r.Header().Get("Access-Control-Allow-Methods"))
}
示例5: TestCSRFForm
func TestCSRFForm(t *testing.T) {
assert := assert.New(t)
token := ""
a := ace.New()
a.Session(cookie.NewCookieStore(), nil)
CSRF(nil)
a.GET("/", func(c *ace.C) {
token = Token(c)
c.JSON(200, nil)
})
a.POST("/", Validate, func(c *ace.C) {
c.String(200, "passed")
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
assert.NotEmpty(token)
cookie := w.Header().Get("Set-Cookie")
r, _ = http.NewRequest("POST", "/", nil)
r.Header.Set("Cookie", cookie)
r.ParseForm()
r.PostForm.Set("csrf_token", token)
w = httptest.NewRecorder()
a.ServeHTTP(w, r)
assert.Equal(200, w.Code)
assert.Equal("passed", w.Body.String())
cookie = w.Header().Get("Set-Cookie")
r, _ = http.NewRequest("POST", "/", nil)
r.Header.Set("Cookie", cookie)
r.ParseForm()
r.PostForm.Set("csrf_token", token)
w = httptest.NewRecorder()
a.ServeHTTP(w, r)
assert.Equal(500, w.Code)
assert.Equal("Invalid CSRF Token", w.Body.String())
}
示例6: TestMaxAge
func TestMaxAge(t *testing.T) {
g := ace.New()
g.Use(Cors(Options{
MaxAge: time.Hour,
}))
assert := assert.New(t)
g.OPTIONS("/", func(c *ace.C) {
c.String(http.StatusOK, "")
})
r := request(g, requestOptions{
Method: "OPTIONS",
URL: "/",
Headers: map[string]string{
"Origin": "http://maji.moe",
},
})
assert.Equal("3600", r.Header().Get("Access-Control-Max-Age"))
}
示例7: TestAllowHeaders
func TestAllowHeaders(t *testing.T) {
g := ace.New()
g.Use(Cors(Options{
AllowHeaders: []string{"X-Custom-Header", "X-Auth-Token"},
}))
assert := assert.New(t)
g.OPTIONS("/", func(c *ace.C) {
c.String(http.StatusOK, "")
})
r := request(g, requestOptions{
Method: "OPTIONS",
URL: "/",
Headers: map[string]string{
"Origin": "http://maji.moe",
},
})
assert.Equal("X-Custom-Header,X-Auth-Token", r.Header().Get("Access-Control-Allow-Headers"))
}
示例8: TestAllowCredentials
func TestAllowCredentials(t *testing.T) {
g := ace.New()
g.Use(Cors(Options{
AllowCredentials: true,
}))
assert := assert.New(t)
g.GET("/test", func(c *ace.C) {
c.String(http.StatusOK, "OK")
})
r := request(g, requestOptions{
URL: "/test",
Headers: map[string]string{
"Origin": "http://maji.moe",
},
})
assert.Equal("true", r.Header().Get("Access-Control-Allow-Credentials"))
assert.Equal("OK", r.Body.String())
}
示例9: TestAllowMethods
func TestAllowMethods(t *testing.T) {
g := ace.New()
g.Use(Cors(Options{
AllowMethods: []string{"GET", "POST", "PUT"},
}))
g.OPTIONS("/", func(c *ace.C) {
c.String(http.StatusOK, "")
})
assert := assert.New(t)
r := request(g, requestOptions{
Method: "OPTIONS",
URL: "/",
Headers: map[string]string{
"Origin": "http://maji.moe",
},
})
assert.Equal("GET,POST,PUT", r.Header().Get("Access-Control-Allow-Methods"))
}
示例10: TestExposeHeaders
func TestExposeHeaders(t *testing.T) {
g := ace.New()
g.Use(Cors(Options{
ExposeHeaders: []string{"Foo", "Bar"},
}))
assert := assert.New(t)
g.GET("/test", func(c *ace.C) {
c.String(http.StatusOK, "OK")
})
r := request(g, requestOptions{
URL: "/test",
Headers: map[string]string{
"Origin": "http://maji.moe",
},
})
assert.Equal("Foo,Bar", r.Header().Get("Access-Control-Expose-Headers"))
assert.Equal("OK", r.Body.String())
}
示例11: startAce
func startAce() {
mux := ace.New()
mux.GET("/hello", aceHandler)
mux.Run(":" + strconv.Itoa(port))
}
示例12: newServer
func newServer() *ace.Ace {
g := ace.New()
g.Use(Cors(Options{}))
return g
}
示例13: loadAceSingle
func loadAceSingle(method, path string, handle ace.HandlerFunc) http.Handler {
router := ace.New()
router.Handle(method, path, []ace.HandlerFunc{handle})
return router
}
示例14: main
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
app := cli.NewApp()
app.Name = "Ivy"
app.Usage = "Assets & Image processing on the fly"
app.Author = "Witoo Harianto"
app.Email = "[email protected]"
app.Version = "1.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "url,u",
Value: ":4900",
Usage: "server port",
},
cli.IntFlag{
Name: "httpcache,t",
Value: 0,
Usage: "if cache enable this is http cache in second",
},
cli.StringFlag{
Name: "cache,c",
Value: "",
Usage: "enable cache, specific eg, file",
},
cli.StringFlag{
Name: "source,s",
Value: "file",
Usage: "source of image eg, file, s3",
},
cli.StringFlag{
Name: "s3key",
Value: "",
Usage: "if source is s3, AWS S3 access key",
EnvVar: "AWS_ACCESS_KEY",
},
cli.StringFlag{
Name: "s3secret",
Value: "",
Usage: "if source is s3, AWS S3 secret key",
EnvVar: "AWS_SECRET_KEY",
},
cli.StringFlag{
Name: "sourceroot",
Value: "",
Usage: "if source is file, specific root path of image",
},
cli.StringFlag{
Name: "cacheroot",
Value: "",
Usage: "if cache is file, specific root path of cache",
},
}
app.Action = func(c *cli.Context) {
var source ivy.Source
switch c.String("source") {
case "s3":
source = ivy.NewS3Source(c.String("s3key"), c.String("s3secret"))
default:
source = ivy.NewFileSystemSource(c.String("sourceroot"))
}
var cache ivy.Cache
switch c.String("cache") {
case "file":
cache = ivy.NewFileSystemCache("cacheroot")
default:
cache = nil
}
iv := ivy.New(source, cache, ivy.NewGMProcessor(), &ivy.Config{
HTTPCache: int64(c.Int("httpcache")),
IsDevelopment: false,
})
a := ace.New()
a.GET("/:bucket/:params/*path", func(c *ace.C) {
start := stopwatch.Start()
params, _ := url.QueryUnescape(c.Params.ByName("params"))
iv.Get(c.Params.ByName("bucket"), params, c.Params.ByName("path"), c.Writer, c.Request)
watch := stopwatch.Stop(start)
log.Printf("[Ivy] %d %s %vms", c.Writer.Status(), c.Request.URL.String(), watch.Milliseconds())
})
url := c.String("url")
log.Println("[Ivy] Start server on " + url)
a.Run(url)
}
app.Run(os.Args)
}