heroku

tech2023-12-02  37

heroku

Following hot on the heels of its recent announcement that it would add support for Java to the support already given to apps written in Ruby, Node.js and Clojure, Platform-as-a-service provider Heroku this morning announced a breakthrough partnership with Facebook that effectively allows anyone with a Heroku account to become an adept, cloud-based Facebook app developer.

在最近宣布将在对以Ruby,Node.js和Clojure编写的应用程序的支持中增加对Java的支持之后,平台即服务提供商Heroku今天早上宣布与Facebook建立突破性合作伙伴关系有效地使拥有Heroku帐户的任何人都可以成为熟练的基于云的Facebook应用程序开发人员。

To give SitePoint readers a head start, we’ve obtained permission to publish the following tutorial, drawing on functionality in Facebook that is only available from today.

为了让SitePoint读者有一个良好的开端,我们已经获得了发布以下教程的许可,并利用了Facebook的功能(仅从今天开始可用)。

Let me hand you over to Adam Wiggins of Heroku.

让我交给Heroku的 Adam Wiggins。

Heroku上的Facebook应用程序入门 (Getting Started with Your Facebook App on Heroku)

This guide is for Facebook developers who are creating apps on Heroku via the Facebook Cloud Services integration. It assumes no previous knowledge of Heroku. Heroku is a cloud application platform, not an old-fashioned web host, so some things may be unfamiliar to you.

本指南适用于通过Facebook Cloud Services集成在Heroku上创建应用程序的Facebook开发人员。 它假定没有Heroku的先前知识。 Heroku是一个云应用程序平台,而不是老式的Web主机,因此某些事情您可能不熟悉。

This article will walk through every part of the process: creating an app and a Heroku account, setting up local development tools, and deploying changes to your Facebook app.

本文将逐步介绍该过程的每个部分:创建一个应用程序和一个Heroku帐户,设置本地开发工具以及将更改部署到您的Facebook应用程序。

创建一个应用 (Create an App)

(If you’ve already created a Heroku app via Facebook, you can skip to the next section.)

(如果您已经通过Facebook创建了Heroku应用,则可以跳到下一部分。)

Start by going to Facebook Developers and clicking Create New App in the upper-right corner:

首先转到Facebook Developers ,然后单击右上角的Create New App :

Enter anything you wish for the app name, check the terms box, then click Continue:

输入您想要的名称作为应用名称,选中条款框,然后点击继续 :

Fill out the captcha in the subsequent dialog and click Submit. After a brief delay you’ll be taken to your (currently blank) Facebook app settings page. On this page, under Cloud Services, click Get Started:

在随后的对话框中填写验证码,然后单击提交 。 短暂延迟后,您将转到您的Facebook应用程序设置页面(当前为空白)。 在此页面上的“ Cloud Services”下 ,单击“入门” :

Get Started” />

开始使用” />

Heroku is the only option in this dialog, so click Next:

Heroku是此对话框中的唯一选项,因此请单击“ 下一步” :

If you already have a Heroku account under your selected email address, the new app will be associated with your existing account.

如果您在所选电子邮件地址下已经有一个Heroku帐户,则新应用将与您现有的帐户相关联。

Choose your favorite programming language from the dropdown (PHP, Ruby, and Python are available). Enter the email address you wish to use for your Heroku user account, then click Create:

从下拉列表中选择您喜欢的编程语言(可使用PHP,Ruby和Python)。 输入您希望用于Heroku用户帐户的电子邮件地址,然后点击创建 :

You’ll be taken to a success dialog showing the new URL of your Facebook app running on Heroku:

您将进入一个成功对话框,显示在Heroku上运行的Facebook应用程序的新URL:

A template app, written in the language you chose, has been copied and deployed just for you! Click the URL or Go To Page to visit your new app.

以您选择的语言编写的模板应用程序已为您复制并部署! 单击URL或转到页面以访问您的新应用。

Other users visiting your app will need to grant permission via a similar dialog. Users only need to do this the first time they visit the app.

访问您应用的其他用户将需要通过类似的对话框授予权限。 用户仅在首次访问该应用程序时才需要这样做。

Because you’re visiting your app as a user, you need to grant permission for the app to access your Facebook profile by clicking Allow:

由于您是以用户身份访问应用程序的,因此您需要通过单击允许授予该应用程序访问您的Facebook个人资料的权限:

By default, your new app is configured as a website that integrates with Facebook. This means it will display as a standalone page. If you’d like your app to run as a Canvas Page inside the Facebook chrome, follow this guide

默认情况下,您的新应用程序被配置为与Facebook集成的网站。 这意味着它将显示为独立页面。 如果您希望您的应用在Facebook chrome中作为“ 画布页面”运行,请遵循此指南

Congratulations! You now have your very own Facebook app, running on Heroku:

恭喜你! 您现在拥有了自己的Facebook应用程序,可以在Heroku上运行:

The app shows examples of accessing the Facebook API to list friends, photos, interests, and more. Once you start editing the code, you can use these capabilities to make your app do more interesting things.

该应用程序显示了访问Facebook API列出朋友,照片,兴趣等的示例。 一旦开始编辑代码,就可以使用这些功能使您的应用执行更多有趣的事情。

So now we want to get you set up for editing your app. We’ll get to that shortly, but first, a brief aside.

因此,现在我们想让您进行编辑应用的设置。 我们将在短期内解决这个问题,但首先,请简短说明。

什么是Heroku? (What is Heroku?)

(If you’re already familiar with Heroku, you can skip ahead.)

(如果您已经熟悉Heroku,则可以跳过。)

Your Facebook app needs to run somewhere. This can be traditional hosting such as your own hardware or a VPS; however, these options are time-consuming and costly to set up. Apps on Heroku can be created instantly, and they cost you nothing unless the app grows to a large amount of traffic or users.

您的Facebook应用程序需要在某个地方运行。 这可以是传统的托管,例如您自己的硬件或VPS。 但是,这些选项设置起来既耗时又昂贵。 Heroku上的应用程序可以立即创建,除非应用程序增长到大量的流量或用户,否则它们不会花费您任何费用。

Heroku is a cloud application platform. With Heroku, you don’t need to think about servers at all. You can write apps in the programming language of your choice, back it with add-on resources such as SQL and NoSQL databases, Memcached, and many others. You manage your app using the Heroku command-line tool and you deploy code using the Git revision control system. Take a tour of the Heroku architecture.

Heroku是一个云应用程序平台。 使用Heroku,您根本不需要考虑服务器。 您可以使用自己选择的编程语言来编写应用程序,并使用诸如SQL和NoSQL数据库, Memcached等许多附加资源来支持它。 您可以使用Heroku命令行工具管理应用,并使用Git版本控制系统部署代码。 参观Heroku架构。

Any kind of web app can be deployed to Heroku, and you can read all about the platform’s capabilities in the Heroku Dev Center. For the rest of this guide, we’ll focus on editing the Facebook app you’ve already created in the previous section.

可以将任何类型的Web应用程序部署到Heroku,您可以在Heroku Dev Center中阅读有关该平台功能的所有信息。 在本指南的其余部分中,我们将重点介绍编辑上一节中已经创建的Facebook应用程序。

Heroku帐户和工具设置 (Heroku Account and Tools Setup)

(If you already have a Heroku account and the local development tools set up, you can skip to the next section.)

(如果您已经有一个Heroku帐户并设置了本地开发工具,则可以跳到下一部分 。)

Although an account as already been created for you, and the app has already been deployed, you still need to create a password and set up local tools to begin editing it.

尽管已经为您创建了一个帐户,并且已经部署了该应用程序,但是您仍然需要创建密码并设置本地工具才能开始对其进行编辑。

1)选择一个密码 (1) Choose a Password)

A Heroku user account was created for you when you created an app through Facebook. Check your email under the email address you used to the welcome message from Heroku:

当您通过Facebook创建应用程序时,为您创建了一个Heroku用户帐户。 在用于Heroku欢迎消息的电子邮件地址下检查您的电子邮件:

Follow the first link in that email to the page where you can choose a password for your Heroku account:

按照该电子邮件中的第一个链接,进入页面,您可以在其中为Heroku帐户选择密码:

When finished, close that browser window and proceed to the next step.

完成后,关闭该浏览器窗口,然后继续下一步。

2)安装Heroku工具带 (2) Install the Heroku Toolbelt)

To manage and edit your app, you’ll need the Heroku command-line tool and the Git revision control system, both of which will be installed by the Heroku toolbelt. Download and install the toolbelt for your OS from the list below:

要管理和编辑您的应用程序,您将需要使用Heroku命令行工具和Git版本控制系统,这两者都将由Heroku工具带安装。 从下面的列表中下载并安装适用于您的操作系统的工具带:

If you have…Install with…Mac OS XDownload OS X packageWindowsDownload Windows .exe installerUbuntu Linuxapt-get repositoryOtherTarball (add contents to your $PATH) 如果你有… 用...安装 Mac OS X 下载OS X软件包 视窗 下载Windows .exe安装程序 Ubuntu Linux apt-get存储库 其他 Tarball (将内容添加到$PATH )

3)从命令行登录 (3) Log In from the Command Line)

Once installed, you’ll have access to the heroku command from your terminal. Log in using the email address and password for your Heroku account:

安装完成后,您将可以从终端上访问heroku命令。 使用您的Heroku帐户的电子邮件地址和密码登录:

:::term $ heroku login Enter your Heroku credentials. Email: adam@example.com Password: Could not find an existing public key. Would you like to generate one? [Yn] Generating new SSH public key. Uploading ssh public key /Users/adam/.ssh/id_rsa.pub

Press enter at the prompt to upload your existing ssh key or create a new one, used for pushing code later on.

在提示符下按Enter键,以上传您现有的ssh密钥或创建一个新密钥,用于以后推送代码。

编辑您的应用 (Editing Your App)

With your Heroku account and local tools set up, you can start making changes to your Facebook app.

通过设置Heroku帐户和本地工具,您可以开始对Facebook应用进行更改。

1)获取您的应用程序的源代码 (1) Fetch Your App’s Sourcecode)

We’ll start by grabbing a copy of your app’s sourcecode using Git. You’ll need to know the name of your app for this: Heroku gave it a randomly-generated haiku name to start with, so look in the URL to find your app name. For example, if your URL is http://furious-robot-218.herokuapp.com, then your app name is furious-robot-218. Paste this into a git clone command like so:

我们将从使用Git抓取您的应用程序源代码的副本开始。 为此,您需要知道应用程序的名称:Heroku以随机生成的句名称开头,因此请在URL中查找您的应用程序名称。 例如,如果您的URL为http://furious-robot-218.herokuapp.com ,则您的应用程序名称为furious-robot-218 。 将其粘贴到git clone命令中,如下所示:

:::term $ git clone git@heroku.com:furious-robot-218.git -o heroku Initialized empty Git repository in /Users/adam/facebook-template-php/.git/ remote: Counting objects: 273, done. remote: Compressing objects: 100% (183/183), done. remote: Total 273 (delta 2), reused 261 (delta 0) Receiving objects: 100% (273/273), 25.55 KiB, done. Resolving deltas: 100% (2/2), done. $ cd furious-robot-218 $ ls AppInfo.php FBUtils.php close.php images index.php stylesheets utils.php

The directory output shown above is from the PHP sample app. Other languages will have different files.

上面显示的目录输出来自PHP示例应用程序。 其他语言将具有不同的文件。

2)做出改变 (2) Make a Change)

Let’s tweak something small in the app and push it back up to Heroku, illustrating the deploy process. For example, find the line of HTML which shows the welcome banner (in the PHP app, this is in index.php line 157), which should look like this:

让我们在应用程序中进行一些细微调整,然后将其推送回Heroku,以说明部署过程。 例如,找到显示欢迎标语HTML行(在PHP应用程序中,这在index.php第157行中),其外观应如下所示:

<p>Welcome to your Facebook app, running on <span>heroku</span>!</p>

Use your favorite text editor to change this line to:

使用您喜欢的文本编辑器将此行更改为:

<p>This is my app, I can edit it all I want.</p>

Save the file, then use your terminal to commit the change to Git:

保存文件,然后使用终端将更改提交到Git:

:::term $ git commit -am "changed greeting" [master 0ff313a] changed greeting 1 files changed, 1 insertions(+), 1 deletions(-)

3)部署到Heroku (3) Deploy to Heroku)

Now, the fun part: pushing our modified app up to Heroku, so that anyone who visits the app on Facebook sees the new version. Push it to Heroku with Git:

现在,有趣的部分是:将经过修改的应用程序推向Heroku,以便在Facebook上访问该应用程序的任何人都可以看到新版本。 使用Git将其推送到Heroku:

:::term $ git push heroku Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 347 bytes, done. Total 3 (delta 1), reused 0 (delta 0) -----> Heroku receiving push -----> PHP app detected -----> Bundling Apache... done, v2.2.19 -----> Bundling PHP... done, v5.3.6 -----> Discovering process types Procfile declares types -> (none) Default types for PHP -> web -----> Compiled slug size is 20.9MB -----> Launching... done, v2 http://furious-robot-218.herokuapp.com deployed to Heroku To git@heroku.com:furious-robot-218.git 396ec84..994290d master -> master

Now view the app in your browser. You should see the changed welcome banner:

现在,在浏览器中查看该应用程序。 您应该看到更改后的欢迎标语:

Congratulations, you’re now a Facebook app developer!

恭喜,您现在是Facebook应用程序开发人员!

在本地工作 (Working Locally)

In the previous section, we pushed changes to the app to the live production environment without testing it in a local environment. A better workflow for working on your app is to run the it locally, test there, and commit changes as you make improvements that you’ve tested as working. When you’re ready to deploy, use git push heroku to push up changes to the live production app.

在上一部分中,我们将对应用程序的更改推送到了实时生产环境,而无需在本地环境中对其进行测试。 处理您的应用程序时,更好的工作流程是在本地运行该应用程序,在其中进行测试并在进行测试时进行改进,然后提交所做的更改。 准备部署时,请使用git push heroku将更改推送到实时生产应用程序。

Methods for running your app on your local computer will vary by programming language and your own choice in local development environment. For example, PHP developers on Mac OS X can use the version of Apache and PHP that comes with the operating system by putting a checkout of the app’s code into the Sites subdirectory of their home folder, and configuring a VirtualHost in /etc/apache2/users/USERNAME.conf. Ruby developers, on the other hand, will probably run their app with Foreman and visit it at http://localhost:5000/.

在本地计算机上运行应用程序的方法因编程语言和您在本地开发环境中的选择而异。 例如,Mac OS X上PHP开发人员可以通过将应用程序代码的签出放入其主文件夹的Sites子目录中,并在/etc/apache2/users/USERNAME.conf配置VirtualHost ,来使用操作系统随附的Apache和PHP版本。 /etc/apache2/users/USERNAME.conf 。 另一方面,Ruby开发人员可能会使用Foreman运行他们的应用程序,并访问http://localhost:5000/ 。

Regardless of how you run your app locally, there are two techniques you’ll need to know about that are specific to running a local development version of a Facebook app.

无论您如何在本地运行应用程序,都需要了解两种技术,这些技术特定于运行Facebook应用程序的本地开发版本。

1)创建一个开发Facebook应用 (1) Creating a Development Facebook App)

The Facebook app you previously created points at the URL of your Heroku app (which will look like https://furious-robot-218.herokuapp.com). This is the production app.

您先前创建的Facebook应用程序指向Heroku应用程序的URL(看起来像https://furious-robot-218.herokuapp.com )。 这是生产应用程序。

For development, you’ll need to register another app with Facebook. Unlike the first app, this one will not run on Heroku, but instead will run on your local workstation and have a URL like http://localhost:5000/ (or whatever URL you’ve configured for your local machine).

为了进行开发,您需要在Facebook注册另一个应用程序。 与第一个应用程序不同,该应用程序将不会在Heroku上运行,而是将在您的本地工作站上运行,并具有类似http://localhost:5000/ URL(或为本地计算机配置的任何URL)。

Go to Facebook Developers and again click Create New App. Choose a name to indicate that this is the development version of your existing app. For example, if your other app is named “My Cool App”, you might call this one “My Cool App – Dev”.

转到Facebook Developers,然后再次单击Create New App 。 选择一个名称,以表明这是您现有应用程序的开发版本。 例如,如果您的另一个应用程序名为“我的酷应用程序”,则可以将其称为“我的酷应用程序-开发”。

Once created, click the Website checkbox and enter your local URL:

创建完成后,单击“ 网站”复选框,然后输入您的本地URL:

Click Save Changes.

点击保存更改 。

2)设置Facebook App Env Vars (2) Setting Facebook App Env Vars)

On the same settings page for your app used to set the website URL, you’ll also find the App ID and App Secret:

在用于设置网站网址的应用程序的同一设置页面上,您还将找到应用程序ID和应用程序密钥:

These were automatically added to your Heroku app as config vars, but in your local environment you’ll need to set them as environment variables. How you’ll set env vars depends on your OS and method of running your app. Here are two examples:

这些已作为config vars自动添加到您的Heroku应用中,但是在您的本地环境中,您需要将它们设置为环境变量。 设置环境变量的方式取决于您的操作系统和运行应用程序的方法。 这是两个示例:

福尔曼.env (Forman .env)

If you run your app with Foreman, it will automatically source a file named .env in the root of your app’s code checkout. Cut-and-paste your App ID and App Secret into .env:

如果使用Foreman运行应用程序,它将在应用程序代码签出的根目录中自动获取名为.env的文件。 将您的App ID和App Secret粘贴并粘贴到.env :

FACEBOOK_APP_ID='964173273189' FACEBOOK_SECRET='dcd5d23d003d53cb2b68e01'

You should also exclude .env from source control by appending this line to your .gitignore`:

您还应该通过.env添加到.gitignore来将.env从源代码管理中排除:

.env

Apache SetEnv (Apache SetEnv)

If you’re running your app with Apache, you can set the env vars for your local app’s VirtualHost using the SetEnv directive. For example:

如果您正在使用Apache运行应用程序,则可以使用SetEnv指令为本地应用程序的VirtualHost设置环境变量。 例如:

<VirtualHost *:80> DocumentRoot /Users/adam/Sites/mycoolapp-dev ServerName mycoolapp-dev.localhost SetEnv FACEBOOK_APP_ID 964173273189 SetEnv FACEBOOK_SECRET dcd5d23d003d53cb2b68e01 </VirtualHost>

结论 (Conclusion)

With these two changes in place, you should be able to visit your app locally and access all Facebook functionality. When you’re ready to share your changes in the world, git commit them and then git push heroku, then visit your production app to confirm that your changes work correctly on the live app.

通过这两个更改,您应该能够本地访问您的应用程序并访问所有Facebook功能。 当您准备好与世界分享您的更改时,请git commit更改,然后git push heroku ,然后访问您的生产应用程序以确认您的更改在实时应用程序上可以正常工作。

进一步阅读 (Further Reading)

The Heroku Dev Center

Heroku 开发中心

Facebook Graph API docs

Facebook Graph API文档

翻译自: https://www.sitepoint.com/heroku-breaks-through-with-facebook-cloud-integration-2-2/

heroku

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)