imgix

package module
v0.0.0-...-fb0be94 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2016 License: MIT Imports: 9 Imported by: 1

README

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.

Documentation

Index

Constants

View Source
const (
	ShardStrategyCRC   = ShardStrategy(":crc")
	ShardStrategyCycle = ShardStrategy(":cycle")
)

Variables

View Source
var RegexUrlCharactersToEscape = regexp.MustCompile("([^ a-zA-Z0-9_.-])")

Regexp for all characters we should escape in a URI passed in.

View Source
var RegexpHTTPAndS = regexp.MustCompile("https?://")

Matches http:// and https://

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

The Client is used to build URLs.

func NewClient

func NewClient(hosts ...string) Client

Create a new Client with the given hosts, with HTTPS enabled.

func NewClientWithToken

func NewClientWithToken(host string, token string) Client

Create a new Client with the given host and token. HTTPS enabled.

func (*Client) Host

func (c *Client) Host(path string) string

Returns the host for the given path.

func (*Client) Hosts

func (c *Client) Hosts(index int) string

Returns a host at the given index. Panics if there are no hosts.

func (*Client) Path

func (c *Client) Path(imgPath string) string

Builds the full URL to the image (including the host) with no params.

func (*Client) PathWithParams

func (c *Client) PathWithParams(imgPath string, params url.Values) string

`PathWithParams` will manually build a URL from a given path string and parameters passed in. Because of the differences in how net/url escapes path components, we need to manually build a URL as best we can.

The behavior of this function is highly dependent upon its test suite.

func (*Client) Scheme

func (c *Client) Scheme() string

Returns the URL scheme to use. One of 'http' or 'https'.

func (*Client) Secure

func (c *Client) Secure() bool

Returns whether HTTPS should be used.

func (*Client) ShardStrategy

func (c *Client) ShardStrategy() ShardStrategy

The sharding strategy used by this client. Panics if the sharding strategy is not supported by this library.

func (*Client) SignatureForPath

func (c *Client) SignatureForPath(path string) string

Creates and returns the URL signature in the form of "s=SIGNATURE" with no values.

func (*Client) SignatureForPathAndParams

func (c *Client) SignatureForPathAndParams(path string, params url.Values) string

Creates and returns the URL signature in the form of "s=SIGNATURE" for the given parameters. Requires that the client have a token.

type ShardStrategy

type ShardStrategy string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL