Git Product home page Git Product logo

go-meta-image-generator's Introduction

Build Status

Meta Image Generator

An open graph meta image generator for social previews.

  • Pass the title of your page via URL and get an optimized .jpg returned.
  • Customize templates via Golang templates.
  • Images are cached and only generated once.
  • URLs can be signed via a query param to prevent image bombs.

To run locally:

  • Confirm Go is installed: go version
  • Clone this repo: git clone [email protected]:robbymilo/go-meta-image-generator.git && cd go-meta-image-generator
  • Run locally: make start
  • Visit localhost:3000/Hello world.jpg in your browser.

Then, in your website template you can use:

<head>
  <meta property="og:image" content="http://localhost:3000/Title of my Page.jpg" />
</head>

Run locally with docker:

docker run -p 3000:3000 --name=meta --rm robbymilo/go-meta-image-generator:latest

Updating the template

Follow the instructions above to run locally, then navigate to localhost:3000/render/Hello world.jpg. Here you can inspect the template in your browser with dev tools.

Signed URLs

When running in production, you can sign the URLs to prevent a scripter from crashing your server by generating countless images.

Set the environmental variable SIGNATURE to a secret key, ex:

docker run -p 3000:3000 -e SIGNATURE=milo --name=meta --rm robbymilo/go-meta-image-generator

then visit http://localhost:3000/Hello%20world.jpg?signature=Yjg3Nzc1N2FmZjIxNGU2M2MxMjJkNGM0YmU4ZGM5NTE0NDFjZmJhNmExNzgwOTBjOWZlOTcxOGU5ZTEyYmNhZA==

In your HTML template, you can generate the hash of the URL with your secret key, base64 encode it, then pass it to the signature query param.

With Hugo:

{{ $title := "Hello world" }}
{{ $hashed := base64Encode (sha256 (print $title "milo")) }}
{{ return (print "http://localhost:3000/" $title ".jpg?signature=" $hashed) }}

Images will not be returned without the correct signature passed to the query param when the SIGNATURE env var is set.

Why not:

Todo:

  • Add tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.