百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

guillotine

> 编程语言
开源

URL缩短爱好套件

484 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

URL缩短爱好套件

Guillotine

Simple URL Shortener hobby kit. Currently used to shorten URLs at GitHub.com, and also available as a an installable Heroku app.

USAGE

The easiest way to use it is with the built-in memory adapter.

ruby
# app.rb
require 'guillotine'
module MyApp
  class App  Guillotine::Service.new(adapter)

    get '/' do
      redirect 'https://homepage.com'
    end
  end
end
ruby
# config.ru
require "rubygems"
require File.expand_path("../app.rb", __FILE__)
run MyApp::App

Once it's running, add URLs with a simple POST.

bash
curl http://localhost:4567 -i \
  -F "url=http://techno-weenie.net"

You can specify your own code too:

bash
curl http://localhost:4567 -i \
  -F "url=http://techno-weenie.net" \
  -F "code=abc"

Sequel

The memory adapter sucks though. You probably want to use a DB. Check out the Sequel gem for more examples. It'll support SQLite, MySQL, PostgreSQL, and a bunch of other databases.

ruby
require 'guillotine'
require 'sequel'
module MyApp
  class App  Guillotine::Service.new(adapter)
  end
end

You'll need to initialize the DB schema with something like this (depending on which DB you use):

CREATE TABLE IF NOT EXISTS `urls` (
  `url` varchar(255) DEFAULT NULL,
  `code` varchar(255) DEFAULT NULL,
  UNIQUE KEY `url` (`url`),
  UNIQUE KEY `code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Redis

Redis works well, too. The sample below is adapted from Katana, a hosted wrapper around Guillotine designed for Heroku.

ruby
require 'guillotine'
require 'redis'

module MyApp
  class App  uri.host, :port => uri.port, :password => uri.password)
    adapter = Guillotine::Adapters::RedisAdapter.new(redis)
    set :service => Guillotine::Service.new(adapter)
  end
end

Riak

If you need to scale out your url shortening services across the cloud, you can use Riak!

ruby
require 'guillotine'
require 'riak/client'
module MyApp
  class App  'pbc', :pb_port => 8087
    bucket = client['guillotine']
    adapter = Guillotine::Adapters::RiakAdapter.new(bucket)
    set :service => Guillotine::Service.new(adapter)
  end
end

Cassandra

you can use Cassandra!

ruby
require 'guillotine'
require 'cassandra'

module MyApp
  class App  Guillotine::Service.new(adapter)
  end
end

You need to create keyspace and column families as below

sql
CREATE KEYSPACE url_shortener;
USE url_shortener;

CREATE COLUMN FAMILY urls
WITH comparator = UTF8Type
AND key_validation_class=UTF8Type
AND column_metadata = [{column_name: code, validation_class: UTF8Type}];

CREATE COLUMN FAMILY codes
WITH comparator = UTF8Type
AND key_validation_class=UTF8Type
AND column_metadata = [{column_name: url, validation_class: UTF8Type}];

Domain Restriction

You can restrict what domains that Guillotine will shorten.

ruby
require 'guillotine'
module MyApp
  class App  Guillotine::Service.new(adapter,
      :required_host => 'github.com')

    # or, any *.github.com domain
    set :service => Guillotine::Service.new(adapter,
      :required_host => /(^|\.)github\.com$/)

    # or set a simple wildcard
    set :service => Guillotine::Servicew.new(adapter,
      :required_host => '*.github.com')
  end
end

Not TODO

  • Statistics
  • Authentication

Issues· 6 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言