Macaron Documentation
  • Welcome
  • Starter Guide
  • Core Concepts
  • Core Services
  • Custom Services
  • Middlewares
    • Routing
    • Templating
    • Gzip
    • Localization
    • Data Binding and Validation
    • Cache
    • Captcha
    • Session
    • Cross-Site Request Forgery
    • Embed Binary Data
    • Serving Multiple Sites
  • FAQs
  • 简体中文
    • 初学者指南
    • 核心概念
    • 核心服务
    • 自定义服务
    • 中间件和辅助模块
      • 路由模块
      • 模板引擎
      • Gzip 压缩
      • 应用本地化
      • 数据绑定与验证
      • 缓存管理(Cache)
      • 验证码服务
      • 会话管理(Session)
      • 跨域请求攻击(CSRF)
      • 嵌入二进制数据
      • 服务多个站点
    • 常见问题
Powered by GitBook
On this page
  • Installation
  • Usage
  • Options

Was this helpful?

  1. Middlewares

Gzip

PreviousTemplatingNextLocalization

Last updated 5 years ago

Was this helpful?

Middleware gzip provides compress to responses for Macaron . Make sure to register it before other middlewares that write content to response.

Installation

go get github.com/go-macaron/gzip

Usage

package main

import (
    "github.com/go-macaron/gzip"
    "gopkg.in/macaron.v1"
)

func main() {
    m := macaron.Classic()
    m.Use(gzip.Gziper())
    // Register routers.
    m.Run()
}

In this case, the static files will not be compressed by Gzip, to compress them:

package main

import (
    "github.com/go-macaron/gzip"
    "gopkg.in/macaron.v1"
)

func main() {
    m := macaron.New()
    m.Use(macaron.Logger())
    m.Use(macaron.Recovery())
    m.Use(gzip.Gziper())
    m.Use(macaron.Static("public"))
    // Register routers.
    m.Run()
}

Or you can choose to only compress a group of routes' responses:

// ...

func main() {
    m := macaron.Classic()
    m.Group("/gzip", func() {
        // ...
    }, gzip.Gziper())
    // ...
    m.Run()
}

Options

// ...
m.Use(gzip.Gziper(gzip.Options{
    // Compression level. Can be DefaultCompression(-1), ConstantCompression(-2)
    // or any integer value between BestSpeed(1) and BestCompression(9) inclusive.
    // Default is 4.
    CompressionLevel: 4,
}))
// ...

This service comes with a variety of configuration options():

gzip.Options
GitHub
API Reference
Instances