func loadAce(routes []route) http.Handler { router := ace.New() for _, route := range routes { router.Handle(route.method, route.path, []ace.HandlerFunc{aceHandle}) } return router }
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") }
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 }
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")) }
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()) }
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")) }
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")) }
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()) }
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")) }
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()) }
func startAce() { mux := ace.New() mux.GET("/hello", aceHandler) mux.Run(":" + strconv.Itoa(port)) }
func newServer() *ace.Ace { g := ace.New() g.Use(Cors(Options{})) return g }
func loadAceSingle(method, path string, handle ace.HandlerFunc) http.Handler { router := ace.New() router.Handle(method, path, []ace.HandlerFunc{handle}) return router }
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 = "*****@*****.**" 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) }