restocks / imgix-go

Go client for building Imgix URL's

Home Page:https://godoc.org/github.com/parkr/imgix-go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

imgix-go

This is a Go implementation of an imgix url-building library outlined by imgix-blueprint.

Godoc

Build Status

Installation

It's a go package. Do this in your terminal:

go get github.com/parkr/imgix-go

Usage

Something like this:

package main

import (
    "fmt"
    "net/url"
    "github.com/parkr/imgix-go"
)

func main() {
    client := imgix.NewClient("mycompany.imgix.net")

    // Nothing fancy.
    fmt.Println(client.Path("/myImage.jpg"))

    // Throw some params in there!
    fmt.Println(client.PathWithParams("/myImage.jpg", url.Values{
        "w": []string{"400"},
        "h": []string{"400"},
    }))
}

That's it at a basic level. More fun features though!

Sharding Hosts

This client supports sharding hosts, by CRC or just by Cycle.

Cycle is a simple round-robin algorithm. For each request, it picks the host subsequent to the host for the previous request. Like this:

client := Client{
  hosts: []string{"1.imgix.net", "2.imgix.net"},
  shardStrategy: imgix.ShardStrategyCycle,
}
client.Host("/myImage.jpg") // => uses 1.imgix.net
client.Host("/myImage.jpg") // => uses 2.imgix.net
client.Host("/myImage.jpg") // => uses 1.imgix.net... and so on.

CRC uses the CRC32 hashing algorithm paired with the input path to determine the host. This allows you to ensure that an image request will always hit the same host. It looks like this:

client := Client{
  hosts: []string{"1.imgix.net", "2.imgix.net"},
  shardStrategy: imgix.ShardStrategyCRC,
}

// If you have the same path, you'll always get the same host.
client.Host("/myImage.jpg") // => uses 1.imgix.net
client.Host("/myImage.jpg") // => uses 1.imgix.net
client.Host("/myImage.jpg") // => uses 1.imgix.net... and so on.

// Now, a request for another image may find itself with a different host:
client.Host("/1/wedding.jpg") // => uses 2.imgix.net
client.Host("/1/wedding.jpg") // => uses 2.imgix.net
client.Host("/1/wedding.jpg") // => uses 2.imgix.net... and so on.

The default sharding is Cycle.

License

MIT. See LICENSE for details.

About

Go client for building Imgix URL's

https://godoc.org/github.com/parkr/imgix-go

License:MIT License


Languages

Language:Go 99.0%Language:Makefile 1.0%