diy js游戏框架_DIY —用152行代码构建自己的无服务器框架。

这篇博客介绍了如何从头开始使用152行JavaScript代码构建自己的无服务器游戏框架。内容包括翻译自Medium的文章,探讨了在游戏开发中运用无服务器架构的可能性。
摘要由CSDN通过智能技术生成

diy js游戏框架

Have you ever asked yourself how a framework has been built to do a magic job for you? In this article, we will be together in 3 minutes to learn HOWTO to build a framework. This framework will deploy your function code into AWS Lambda or GCloud Functions service as you choose.

等皆你可曾问过自己如何的框架已建成,为你做一个神奇的工作吗? 在本文中,我们将在3分钟内一起学习如何构建框架。 该框架会将您选择的功能代码部署到AWS Lambda或GCloud Functions服务中。

According to “Framework” definition, a Serverless Framework will be a supporting structure around which a Serverless can be built.

根据“框架”的定义, 无服务器框架将是可构建 无服务器框架 的支持结构

In software development, this is about how to construct a project using infrastructure or service that you don’t have to manage any Server to run your whole project code (Monolithic) or a set of functions (FaaS).

在软件开发中,这是关于如何使用无需管理任何服务器即可运行整个项目代码(Monolithic)或一组功能(FaaS)的基础结构或服务来构建项目的方法。

All that is, something will help you construct your projects, manage its deployment and tests will be called as a Serverless Framework.

就是说,这将帮助您构建项目,管理其部署和测试,这被称为无服务器框架

Basic features of a Serverless Framework

无服务器框架的基本功能

There are THREE essential features and SIX following steps you must provide for any serverless tool or framework:

对于任何无服务器工具或框架,必须提供三个基本功能和六个步骤:

  1. Init project information with Cloud Service Provider authentication method that allows your framework upload files, manage essential services such as S3, Lambda, IAM Role and Policy.

    使用Cloud Service Provider身份验证方法初始化项目信息,该方法允许您的框架上载文件,管理基本服务,例如S3,Lambda,IAM角色和策略。

  2. Upload your source code as a zip file onto a specifically designated location such as S3.

    将您的源代码以zip文件的形式上载到S3之类的指定位置。
  3. Request to create a serverless function such as Lambda that will use your zip file.

    请求创建将使用您的zip文件的无服务器功能(例如Lambda)。
  4. Notify developers when the function is created and ready to use.

    创建功能并准备使用时通知开发人员。
  5. Change your code, then deploy it again, again and again…

    更改您的代码,然后一次又一次地部署它…

  6. Destroy everything you have created to cost you nothing.

    销毁您创建的所有内容,不花一分钱。

Most of the frameworks having problems in Step 5. When the code logic is changed, your infrastructure design will change accordingly to its resource accesses. Your deployment probably will fail.

大多数框架在步骤5中都有问题。 当更改代码逻辑时,您的基础结构设计将相应地对其资源访问进行更改。 您的部署可能会失败。

Step 5 requires a good framework to be able to perform:

步骤5需要一个良好的框架才能执行:

  • Source code is updated only if it has been modified. Otherwise, just keeps it no change to upload.

    仅在修改源代码后才对其进行更新。 否则,请保持上传不变。
  • A configuration is always up-to-date accordingly for every deployment. This design will always ensure the configuration consistent with the current code.

    每个部署的配置始终是最新的。 这种设计将始终确保配置与当前代码一致。
  • Your function metadata must be collected and saved locally or in a centralized place. You always need this metadata to orchestrate your infrastructure as code.

    您的功能元数据必须收集并保存在本地或集中的位置。 您始终需要此元数据来将基础结构编排为代码。

Why don’t we upload everything when performing a deployment? A configuration data is very small in bytes or kilobytes but your code is not usually smaller than megabytes.

为什么在执行部署时我们不上传所有内容? 配置数据以字节或千字节为单位很小,但是您的代码通常不小于兆字节。

In order to avoid consuming too many bandwidths, the best way is using a hashing algorithm. AWS Lambda has its own CodeHash field but it is hashed with a timestamp along with its code. It means whenever you hash your code, it will be different from the last time. You need to overcome this point when building your toolkit to avoid uploading the same thing every time.

为了避免消耗太多带宽,最好的方法是使用哈希算法。 AWS Lambda有其自己的CodeHash字段,但使用时间戳及其代码对其进行哈希处理。 这意味着每当您对代码进行哈希处理时,它将与上次有所不同。 在构建工具箱时,您需要克服这一点,以避免每次都上传相同的内容。

A resilience, graceful operation for deploying and destroying a function should be put in mind when designing every software. Resilience operation means it fails when it needs to be failed.

设计每个软件时,应牢记用于部署销毁功能的弹性,优雅的操作。 弹性操作意味着它在需要失败时会失败。

When you deploy your code, if there is something wrong with user input parameters, it must be failed and wait for user correction. If there is an error from a reason likes “You cannot create your function because it exists.” It must be resilient by continuing to the next operation.

部署代码时,如果用户输入参数有问题,则必须失败并等待用户更正。 如果由于诸如“ 您无法创建函数,因为它存在 ”之类的原因而出现错误。 通过继续下一个操作,它必须具有弹性。

Important: a good framework must support to manage multiple deployment environments without impacting to the others when switching between them.

重要提示: 一个好的框架必须支持管理多个部署环境,而在它们之间切换时又不影响其他部署环境。

A deployment environment should cover the following properties:

部署环境应包含以下属性:

  • A name as a shortcode (eg: demo, staging, prod)

    名称作为简码(例如:demo,staging,prod)

  • A service account (eg: 01234566622 or gc-project-id)

    服务帐户 (例如:01234566622或gc-project-id)

  • A location or region (eg: us-east-1, eu-central-1)

    位置或区域 (例如:us-east-1,eu-central-1)

  • A project code name (eg: starwars or bookstore)

    项目代码名称(例如:starwars或书店)

In software development, a “so-called” project has several stages in its lifecycle. Best practice tell you to separate development, testing and production environment into different spaces for security.

在软件开发中,“所谓的”项目在其生命周期中具有多个阶段。 最佳实践告诉您将开发,测试和生产环境分隔到不同的空间以确保安全。

Sharing these environments also are needed for everyone in your team. You should make it easily with git pull and run deploy command.

团队中的每个人都需要共享这些环境。 您应该使用git pull并运行deploy命令使其变得容易。

To generalize your project with multiple environments. You must use a template or variable parsing technique. A ${FOO} must be “bar-demo” or “bar-staging” according to the current environment.

要在多个环境中概括您的项目。 您必须使用模板或变量解析技术。 根据当前环境,$ {FOO}必须是“ bar-demo”或“ bar-staging”。

simplify-cli — A pre-built Serverless Framework

simple-cli —预先构建的无服务器框架

Simplify SDK offers you a basic set of operations for building this framework-like. An optimistic, minimalist framework code with 152 lines can be found in https://github.com/simplify-framework/serverless/blob/master/cli.js

Simplify SDK为您提供了一组用于构建类似框架的基本操作。 可以在https://github.com/simplify-framework/serverless/blob/master/cli.js中找到具有152行的乐观,简约的框架代码

Let’s install simplify-cli package then generate a skeleton code to play with:

让我们安装simple-cli软件包,然后生成要使用的框架代码:

  • npm install -g simplify-cli

    npm install -g简化-cli
  • mkdir ./starwars && cd ./starwars

    mkdir ./starwars && cd ./starwars
  • simplify-cli init

    简化CLI初始化

Looking into the .env file was generated. You will see a block to declare a deployment configuration, the application and the information for your function:

查看.env文件已生成。 您将看到一个块,用于声明部署配置,应用程序和功能信息:

Image for post
Figure — 1. Project environment variables.
图— 1.项目环境变量。

All parameters are put in a .env file so that the framework can read easily and combine with the config.json will generate a final environment base setup. To deploy your function above, you can try to write this sample code:

所有参数都放在一个.env文件中,以便该框架可以轻松阅读并与config.json结合使用,将生成最终的基于环境的设置。 要在上面部署函数,您可以尝试编写以下示例代码:

var fw = require(‘simplify-cli’)

var fw = require('simplify-cli')

fw.deployFunction(“config.json”, “.env”, “role.json”, “policy.json”, “src”)

fw.deployFunction(“ config.json”,“。env”,“ role.json”,“ policy.json”,“ src”)

Or just run through the simplify-cli command-line interface:

或者只是通过simple-cli命令行界面运行:

  • simplify-cli deploy

    简化CLI部署

  • simplify-cli destroy

    简化cli销毁

An example application is provided to demonstrate the way to use your function in a web project:

提供了一个示例应用程序来演示在Web项目中使用功能的方式:

  • cd examples

    cd的例子
  • npm i simplify-sdk

    NPM我简化SDK
  • node deployment.js

    节点Deployment.js
  • node destroy.js (for your work)

    节点destroy.js (用于您的工作)

This application will deploy an API Gateway that triggers your Lambda function in .src folder. It also deploys the html folder into an S3 published as a static WebSite.

此应用程序将部署一个API网关,该网关将触发您的.src文件夹中的Lambda函数。 还将html文件夹部署到作为静态WebSite发布的S3中。

Try to visit the Website URL: https://starwars-website.s3.amazonaws.com/index.html to see the CloudWatch Logs printed on your website. You must refresh several times to generate function logs:

尝试访问网站URL: https ://starwars-website.s3.amazonaws.com/index.html,以查看网站上印刷的CloudWatch Logs。 您必须刷新几次才能生成功能日志:

Image for post
Figure — 2. The showlogs() function result.
图— 2. showlogs()函数的结果。

You can tailor from this code to become your own framework or use it with simplify-sdk or @aws/cdk to extend your toolkit.

您可以从此代码改编成您自己的框架,或将其与simple-sdk或@ aws / cdk结合使用以扩展您的工具包。

Author: cuong3ihut@gmail.com

作者:cuong3ihut@gmail.com

Next Article: How to build a scalable software development team from one to hundreds of developers for a Start-Up.

下一篇文章:如何建立一个可扩展的软件开发团队,从一个开发人员到数百个开发人员。

翻译自: https://medium.com/swlh/dyi-build-a-serverless-framework-with-152-lines-of-code-c8b2d1cc09e6

diy js游戏框架

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值