3

I have a mysql schema full of information that I'd like to expose with a RESTful API, preferably in Ruby.

What I know so far

  • "RESTful" refers to following a certain standard for your API, and is not associated with any particular implementation language.
  • I can use the mysql gem in Ruby to pull the data that I need from my database
  • The Sinatra project appears to be an easy way to set up an API using Ruby

An Example

Let's say I have a server (12.34.56.78) and my ruby code on that server is located at

/opt/example_code/main.rb. 

I'd like the user to be able to query something like

12.34.56.77/customers/1234?show_ssn=true

in order to retrieve a record for customer 1234, opting to also show the SSN

What I don't understand

  1. What basic server-side software do I need running to make this happen? I assume I need some sort of web server like Apache or nginx that's always running? I know you can run a simple HTTP server in python (python -m SimpleHTTPServer) which runs as long as my terminal window is open (or I cancel it). Are Apache and nginx similar to that but they run constantly as a process?

  2. When a user hits 12.34.56.77, how can I let the server know it should be looking in /opt/example_code for my script? Is there some sort of configuration I can set?

    I have a vague idea of how HTTP ports work; should I set certain ports to "point to" certain directories? For example, only if they specify port 5678 will the server know to correctly look in my /opt/example_code directory

    12.34.56.77:5678/customers/1234?show_ssn=true
    
  3. When the HTTP server finally deciphers the request and redirects it to /opt/example_code, what happens next? How do I instruct it to run my main.rb file in that directory? Also, how is the URI ("/customers...true") passed to my ruby app to be deciphered by Sinatra?

I think I have a grasp of the individual pieces that make this process, but I'm struggling on how they all integrate together.

Thanks for the help!

4

1 回答 1

1
  1. 您需要运行 Web 服务器(Apache 或 Nginx)。每个请求都通过 Web 服务器。如果是对静态页面、某些资产等的请求,Web 服务器会处理它。如果请求需要处理,它将被分派到应用服务器(thin、puma、unicorn...)。现在,Apache 或 Nginx 将被妖魔化,这意味着该进程将在后台运行,而不是在终端中运行。这同样适用于您的应用程序服务器。你可以在网上找到很多教程。还要确保检查文档。

  2. 那么这真的取决于你的服务器。Apache 和 Nginx 都有自己的方法。例如,通过为 Nginx 编辑启用站点的文件。但是再次选择一个并检查它的文档,您会在那里找到所有内容。

    关于端口:不,你不应该。由于它的配置,服务器将知道在哪里寻找您的应用程序。端口将允许您运行更多的服务器或进程。想象一下,您的应用程序服务器在端口 3000 上运行。没有其他东西可以使用该端口了。这意味着,如果你想在同一台机器上运行例如 MySQL 服务器,它需要使用不同的端口。

  3. 您将配置您的 Web 服务器,以便它知道在哪里查找引导文件。/customers 之类的路由将在您的 sinatra 应用程序中声明。看看这个http://recipes.sinatrarb.com/p/deployment/nginx_proxied_to_unicorn

请注意,在没有深入了解服务器管理的情况下在生产环境中运行应用程序可能非常危险。很多事情都可能出错。也许一个好的解决方案是使用一些基于云的服务(Heroku、RackSpace ...)?

我做了一些非常相似的事情。我用 Grape ( https://github.com/intridea/grape ) 编写了我的 API,它与 sinatra 非常相似,但它是专门为构建 API 而设计的。我使用 Nginx 作为我的网络服务器,使用 Puma ( https://github.com/puma/puma ) 作为我的应用程序服务器。

祝你好运!

Nginx 配置示例(同一服务器上的两个服务)

/path/to/nginx/conf/sites-enabled/site1.com(将 nginx 与乘客一起使用):

server {
  listen 80;
  server_name site1.com;
}

server {
  listen 443;
  server_name site1.com;
  root /path/to/application/public-folder;
  passenger_enabled on;
}

/path/to/nginx/conf/sites-enabled/site2.com(使用 nginx 和 unicorn 和一些高级配置):

server {
  listen 80;
  server_name site2.com;
}

server {
  listen 443;
  server_name site2.com;
  root /path/to/application/public-folder;

  client_max_body_size       10m;
  client_body_buffer_size    128k;

  proxy_connect_timeout      90;
  proxy_send_timeout         90;
  proxy_read_timeout         90;

  proxy_buffer_size          4k;
  proxy_buffers              4 32k;
  proxy_busy_buffers_size    64k;
  proxy_temp_file_write_size 64k;


  location / {
    proxy_set_header   Host $http_host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_redirect off;

    proxy_pass http://unicorn_cluster;
  }
}
于 2013-10-26T01:43:46.427 回答