Iris
Getting Started
  • What is Iris
  • 📌Getting started
    • Installation
    • Quick start
  • 🔌Routing
    • Middleware
    • API Versioning
  • 🗜️Compression
    • Index
  • ✈️Redirect
    • Redirect from Context
    • Rewrite Middleware
    • Multi Application Instances
  • 🖼️ View/Templates
    • Documentation
    • Benchmarks
    • ➲ Examples
  • 📁File Server
    • Introduction
    • Listing
    • In-memory Cache
    • HTTP/2 Push + Embedded + Cache and Compression
    • The PrefixDir function
    • Serve files from Context
    • ➲ Examples
  • 🌎Localization
    • Documentation
    • Sitemap
    • ➲ Examples
  • 🛡️Security
    • Basic Authentication
    • CORS
    • Sessions & Cookies
    • CSRF
    • JSON Web Tokens
    • Access Control
    • Anti-bot CAPTCHA
    • ➲ Examples
  • 🚀Responses
    • Text
    • HTML
    • Markdown
    • XML
    • YAML
    • Binary
    • JSON
    • JSONP
    • Problem
    • Protocol Buffers
    • MessagePack
    • Gzip
    • Content Negotiation
    • Stream
    • Server-Sent Events
    • HTTP/2 Push
    • Recorder
    • Outroduction
    • ➲ Examples
  • 📥Requests
    • URL Query
    • Headers
    • URL Path Parameters
    • Form
    • Text
    • XML
    • YAML
    • Binary
    • JSON
    • Validation
    • Protocol Buffers
    • MessagePack
    • Gzip
    • ➲ Examples
  • 💉Dependency Injection
    • Documentation
    • Register Dependency from Context
    • Inputs
    • Outputs
    • ➲ Examples
  • 🦏MVC
    • Quick start
    • Documentation
    • Handle Errors
    • Sessions
    • Websockets
    • gRPC
    • ➲ Examples
  • 🤓Resources
    • Examples
    • Starter Kits
    • Publications
    • Benchmarks
    • Support
  • 📘Contents
    • Host
      • Automatic Public Domain with TLS
    • Configuration
    • Routing
      • Path Parameter Types
      • Reverse Lookups
      • Handle HTTP errors
      • Subdomains
      • Wrap the Router
      • Context Methods
    • HTTP Method Override
    • HTTP Referrer
    • URL Query Parameters
    • Forms
    • Model Validation
    • Cache
    • Cookies
    • Sessions
      • Database
      • Flash Messages
    • Websockets
    • Sitemap
    • Localization
    • Testing
Powered by GitBook
On this page
  • Introduction
  • Example

Was this helpful?

  1. 📁File Server

HTTP/2 Push + Embedded + Cache and Compression

PreviousIn-memory CacheNextThe PrefixDir function

Last updated 1 year ago

Was this helpful?

Welcome to the File Server section that can boost your web application's performance to the next level. Enable HTTP/2 Push, serve embedded files with their compressed contents pre-cached.

This is a unique feature that you won't find in any other backend web framework as of 2023.

Please read the previous File Server's sections first and read the by Google LLC to learn about the benefits of using HTTP/2.

Introduction

The DirOptions contains the PushTargets and PushTargetsRegexp fields to enable and customize automatic push assets (css, javascript, images) of Index pages.

The PushTargets field is a map of filenames to be served without additional client's requests (HTTP/2 Push) when a specific request relative path hits an Index.

Example, to serve a specific favicon, javascript and css assets when the client hits the root path (and /index.html):

var opts = iris.DirOptions{
    IndexName: "index.html",
    PushTargets: map[string][]string{
        "/": {
            "favicon.ico",
            "js/main.js",
            "css/main.css",
        },
    },
}

app.HandleDir("/", iris.Dir("./assets"), opts)

Alternatively the PushTargetsRegexp field can be used instead (recommended), to automatically serve common assets based on a regular expression:

var opts = iris.DirOptions{
    IndexName: "index.html",
    PushTargetsRegexp: map[string]*regexp.Regexp{
        "/": iris.MatchCommonAssets,
    },
}

app.HandleDir("/", iris.Dir("./assets"), opts)

The iris.MatchCommonAssets is just a regular expression which matches javascript, css, ico, png, ttf, svg, webp and gif file types.

Example

We've learn about Embedded files, Memory Cache (with pre-compressed contents) and Push Targets. Now it is time to combine all prior knowedge we got from this File Server chapter to create and run a simple web server which serves index files and their assets pushed through HTTP/2, both index and assets will be served from memory cache and pre-compressed contents will be written to the clients based on their accepted content encoding header.

1. Create a directory to host the Iris web server.

3. Install go bindata, open and terminal and execute the following command:

$ go get -u github.com/go-bindata/go-bindata/...

4. Use go-bindata to generate a compatible http.FileServer to pass into Party.HandleDir:

$ go-bindata -nomemcopy -fs -prefix "assets" ./assets/...

5. Create a main.go file and copy-paste the following code:

package main

import (
	"regexp"

	"github.com/kataras/iris/v12"
)

var opts = iris.DirOptions{
	IndexName: "index.html",
	PushTargetsRegexp: map[string]*regexp.Regexp{
		"/":              iris.MatchCommonAssets,
		"/app2/app2app3": iris.MatchCommonAssets,
	},
	ShowList: true,
	Cache: iris.DirCacheOptions{
		Enable:         true,
		CompressIgnore: iris.MatchImagesAssets,
		Encodings:       []string{"gzip", "deflate", "br", "snappy"},
        // Compress files equal or larger than 50 bytes.
        CompressMinSize: 50,
		Verbose:         1,
	},
}

func main() {
	app := iris.New()
	app.HandleDir("/public", AssetFile(), opts)
	app.Run(iris.TLS(":443", "mycert.crt", "mykey.key"))
}

6. Open a terminal and execute go build or go run .

7. Open a browser or use any http client and navigate through:

  • https://127.0.0.1/public

  • https://127.0.0.1/public/app2

  • https://127.0.0.1/public/app2/app2app3

  • https://127.0.0.1/public/app2/app2app3/dirs

That's all.

We will use both go-bindata and Iris Memory Cache to serve pre-compressed data from embedded files of our executable Program. The assets directory of this exercise can be found . In the end, we'll need to deploy just the executable file(and the generated tls keys, although you can pass there as well), assets phyisical folder is not required after forth step.

1. This web server SHOULD be served under TLS in order for HTTP/2 Push to work. Generate keys for your local web server. For the sake of simplicity, in this example we will use the and files. Place them inside the project's folder.

2. Download the example and place it to the project's folder.

Introduction to HTTP/2
here
public and private key Go generated values
mycert.crt
mykey.key
assets folder