<commit_before># Slimgfast

Slimgfast is a library that allows you to create a scalable, efficient, dynamic
image origin server.

As an example, if you want an image to be resized to 640x480, you could hit the
url at http://127.0.0.1/my-image.jpg?w=640&h=480 and slimgfast will dynamically
resize your image to the correct dimensions, serve it, and cache it for later.

Slimgfast comes with an executable which supports a baseline default, but most
advanced users will want to use it as a library for maximum configurability.

## Getting Started

The easiest way to get a copy of slimfast is to use "go get":

    go get github.com/ericflo/slimgfast

In your code you can now import the library:

    import github.com/ericflo/slimgfast/src

    // For example:
    fetcher := &slimgfast.ProxyFetcher{ProxyUrlPrefix: "http://i.imgur.com"}

To see what the default executable can do, navigate to the newly-downloaded
slimgfast directory and run:

    go run main.go http://i.imgur.com

Now to load an image we can do:

    open http://localhost:4400/EgLrnVL.jpg

With no arguments it will just proxy the original image which lives at
http://i.imgur.com/EgLrnVL.jpg.  With arguments it will resize it:

    open http://localhost:4400/EgLrnVL.jpg\?w=300\&h=300

## Using Slimgfast as a library

The steps for setting up a slimfast instance are fairly straightforward:

* Create a fetcher that will know how to read images from the upstream source
* Create a list of transformers, or potential operations that can be applied to
  the image (e.g. resize)
* Instantiate an app struct, which collects all the fetchers and transformers
  and handles the actual HTTP requests
* Spin up the groupcache library so it knows who its peers are
* Start the app and the http server

## Creating your own Fetcher

Creating a Fetcher is straightforward, you only have to implement the Fetcher
inteface, which means implementing the following:

    Fetch(req *ImageRequest, dest groupcache.Sink) error
    ParseURL(rawUrl string) error

Since it's really not all that much code, here's the body of the filesystem
fetcher as an example:

    type FilesystemFetcher struct {
        PathPrefix string
        path       string
    }

    func (f *FilesystemFetcher) ParseURL(rawUrl string) error {
        parsedUrl, err := url.ParseRequestURI(rawUrl)
        if err != nil {
            return err
        }
        f.path = path.Clean(f.PathPrefix + parsedUrl.Path)
        log.Println(f.path)
        return nil
    }

    func (f *FilesystemFetcher) Fetch(req *ImageRequest, dest groupcache.Sink) error {
        data, err := ioutil.ReadFile(f.path)
        if err != nil {
            return err
        }<commit_msg>No need for log in the example.<commit_after># Slimgfast

Slimgfast is a library that allows you to create a scalable, efficient, dynamic
image origin server.

As an example, if you want an image to be resized to 640x480, you could hit the
url at http://127.0.0.1/my-image.jpg?w=640&h=480 and slimgfast will dynamically
resize your image to the correct dimensions, serve it, and cache it for later.

Slimgfast comes with an executable which supports a baseline default, but most
advanced users will want to use it as a library for maximum configurability.

## Getting Started

The easiest way to get a copy of slimfast is to use "go get":

    go get github.com/ericflo/slimgfast

In your code you can now import the library:

    import github.com/ericflo/slimgfast/src

    // For example:
    fetcher := &slimgfast.ProxyFetcher{ProxyUrlPrefix: "http://i.imgur.com"}

To see what the default executable can do, navigate to the newly-downloaded
slimgfast directory and run:

    go run main.go http://i.imgur.com

Now to load an image we can do:

    open http://localhost:4400/EgLrnVL.jpg

With no arguments it will just proxy the original image which lives at
http://i.imgur.com/EgLrnVL.jpg.  With arguments it will resize it:

    open http://localhost:4400/EgLrnVL.jpg\?w=300\&h=300

## Using Slimgfast as a library

The steps for setting up a slimfast instance are fairly straightforward:

* Create a fetcher that will know how to read images from the upstream source
* Create a list of transformers, or potential operations that can be applied to
  the image (e.g. resize)
* Instantiate an app struct, which collects all the fetchers and transformers
  and handles the actual HTTP requests
* Spin up the groupcache library so it knows who its peers are
* Start the app and the http server

## Creating your own Fetcher

Creating a Fetcher is straightforward, you only have to implement the Fetcher
inteface, which means implementing the following:

    Fetch(req *ImageRequest, dest groupcache.Sink) error
    ParseURL(rawUrl string) error

Since it's really not all that much code, here's the body of the filesystem
fetcher as an example:

    type FilesystemFetcher struct {
        PathPrefix string
        path       string
    }

    func (f *FilesystemFetcher) ParseURL(rawUrl string) error {
        parsedUrl, err := url.ParseRequestURI(rawUrl)
        if err != nil {
            return err
        }
        f.path = path.Clean(f.PathPrefix + parsedUrl.Path)
        return nil
    }

    func (f *FilesystemFetcher) Fetch(req *ImageRequest, dest groupcache.Sink) error {
        data, err := ioutil.ReadFile(f.path)
        if err != nil {
            return err
        }
{'fraction_non_alphanumeric': 0.06435459940652818, 'fraction_numerical': 0.011869436201780416, 'mean_word_length': 3.283558379666402, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6103755', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1536, 'n_words': 689}