مثال Lambda لجلب جميع منشورات "النشرة الإخبارية" من Pocket.
راجع رمز لامدا على ./src/lambda/newsletter.js.
قم بتشغيله على https://pocket-newsletter-lambda.netlify.com/.
جدول المحتويات:
الجزء الأكبر من المنطق الخاص بالجيب هو وظيفة fetchBookmarks
، وهي تقوم بما يلي:
state: 'all'
للحصول على المشاركات المؤرشفة وغير المؤرشفةtag: 'newsletter'
لجلب المشاركات الموسومة بالرسالة newsletter
detailType: 'complete'
يعني أن واجهة برمجة التطبيقات (API) تقوم بإرجاع بيانات أكثر اكتمالاً{ title, url, excerpts, authors }
(كل هذه الحقول عبارة عن سلاسل)انظر الكود
async function fetchBookmarks ( consumerKey , accessToken ) {
const res = await axios . post ( 'https://getpocket.com/v3/get' , {
consumer_key : consumerKey ,
access_token : accessToken ,
tag : 'newsletter' ,
state : 'all' ,
detailType : 'complete'
} ) ;
const { list } = res . data ;
// List is a key-value timestamp->entry map
const entries = Object . values ( list ) ;
return entries . map (
( {
given_title ,
given_url ,
resolved_url ,
resolved_title ,
excerpt ,
authors ,
rest
} ) => ( {
... rest ,
title : given_title || resolved_title ,
url : given_url || resolved_url ,
excerpt ,
authors : authors
? Object . values ( authors )
. map ( ( { name } ) => name )
. filter ( Boolean )
. join ( ',' )
: ''
} )
) ;
}
لامدا تدعم فقط POST مع الجسم، وبالتالي:
if ( event . httpMethod !== 'POST' ) {
return {
statusCode : 404 ,
body : 'Not Found'
} ;
}
if ( ! event . body ) {
return {
statusCode : 400 ,
body : 'Bad Request'
} ;
}
نحن ندعم كلا من طلبات POST للنموذج المشفر بعنوان URL (يتم ذلك على سبيل المثال عند تعطيل JS في الصفحة التجريبية) وطلبات JSON.
يصل النص إما بتشفير base64 (في حالة استخدام طلب نص نموذج مشفر بعنوان URL) أم لا. تتم الإشارة إلى ذلك بواسطة علامة isBase64Encoded
في event
.
يتم تحليل سلسلة مشفرة base64 في العقدة باستخدام Buffer.from(event.body, 'base64').toString('utf-8)
.
لتحويل النص من النموذج المشفر بعنوان URL إلى كائن، يتم استخدام الوظيفة التالية، والتي تعمل مع POSTs ذات الحقول البسيطة.
function parseUrlEncoded ( urlEncodedString ) {
const keyValuePairs = urlEncodedString . split ( '&' ) ;
return keyValuePairs . reduce ( ( acc , kvPairString ) => {
const [ k , v ] = kvPairString . split ( '=' ) ;
acc [ k ] = v ;
return acc ;
} , { } ) ;
}
إليك الوظيفة في لامدا:
const {
pocket_consumer_key : pocketConsumerKey ,
pocket_access_token : pocketAccessToken
} = event . isBase64Encoded
? parseUrlEncoded ( Buffer . from ( event . body , 'base64' ) . toString ( 'utf-8' ) )
: JSON . parse ( event . body ) ;
إذا كان مفتاح المستهلك أو رمز الوصول مفقودًا، فسنرسل 400:
if ( ! pocketConsumerKey || ! pocketAccessToken ) {
return {
statusCode : 400 ,
body : 'Bad Request'
} ;
}
أخيرًا نحاول جلب fetchBookmarks
(تم تفصيل وظيفة هذه الوظيفة أعلاه).
إذا فشل ذلك عند حدوث خطأ في الطلب (عندما يفشل axios، فإنه يحتوي على خاصية response
للخطأ)، نريد إعادة إرسال معلومات تلك الاستجابة إلى العميل، وإلا فإننا سنرسل 500 فقط عند الخطأ أو 200 عند النجاح:
try {
const bookmarks = await fetchBookmarks ( pocketConsumerKey , pocketAccessToken ) ;
return {
statusCode : 200 ,
body : JSON . stringify ( bookmarks )
} ;
} catch ( e ) {
if ( e . response ) {
return {
statusCode : e . response . statusCode ,
body : `Error while connecting to Pocket API: ${ e . response . statusText } `
}
}
return {
statusCode : 500 ,
body : e . message
}
}
[
{
"title" : "TechnicalDebt" ,
"url" : "https://martinfowler.com/bliki/TechnicalDebt.html" ,
"excerpt" : "Software systems are prone to the build up of cruft - deficiencies in internal quality that make it harder than it would ideally be to modify and extend the system further." ,
"authors" : ""
} ,
{
"title" : "CannotMeasureProductivity" ,
"url" : "https://martinfowler.com/bliki/CannotMeasureProductivity.html" ,
"excerpt" : "We see so much emotional discussion about software process, design practices and the like. Many of these arguments are impossible to resolve because the software industry lacks the ability to measure some of the basic elements of the effectiveness of software development." ,
"authors" : ""
} ,
{
"title" : "How SQLite Is Tested" ,
"url" : "https://www.sqlite.org/testing.html" ,
"excerpt" : "The reliability and robustness of SQLite is achieved in part by thorough and careful testing. As of version 3.23.0 (2018-04-02), the SQLite library consists of approximately 128.9 KSLOC of C code." ,
"authors" : ""
} ,
{
"title" : "How FriendFeed uses MySQL to store schema-less data" ,
"url" : "https://backchannel.org/blog/friendfeed-schemaless-mysql" ,
"excerpt" : "We use MySQL for storing all of the data in FriendFeed. Our database has grown a lot as our user base has grown. We now store over 250 million entries and a bunch of other data, from comments and "likes" to friend lists." ,
"authors" : ""
} ,
{
"title" : "jlevy/the-art-of-command-line: Master the command line, in one page" ,
"url" : "https://github.com/jlevy/the-art-of-command-line" ,
"excerpt" : "Note: I'm looking for a new (and potentially paid) lead author to help expand this to a more comprehensive Guide. While it's very popoular, it could be both deeper and more helpful." ,
"authors" : ""
} ,
{
"title" : "Project Mezzanine: The Great Migration | Uber Engineering Blog" ,
"url" : "https://eng.uber.com/mezzanine-migration/" ,
"excerpt" : "What happens when you have to migrate hundreds of millions of rows of data and more than 100 services over several weeks while simultaneously keeping Uber running for millions of riders? This is the story of how dozens of engineers helped Uber move to Mezzanine in 2014." ,
"authors" : "Rene Schmidt"
} ,
{
"title" : "8 Protips to Start Killing It When Dockerizing Node.js - NodeSource" ,
"url" : "https://nodesource.com/blog/8-protips-to-start-killing-it-when-dockerizing-node-js/" ,
"excerpt" : "Containers are the best way to deploy Node.js applications to production. Containers provide a wide variety of benefits, from having the same environment in production and development to streamlining deploys for speed and size. Dockerizing your Node." ,
"authors" : "Tierney Cyren"
} ,
{
"title" : "How and Why We Switched from Erlang to Python – Mixpanel Engineering" ,
"url" : "https://engineering.mixpanel.com/2011/08/05/how-and-why-we-switched-from-erlang-to-python/" ,
"excerpt" : "A core component of Mixpanel is the server that sits at http://api.mixpanel.com. This server is the entry point for all data that comes into the system – it’s hit every time an event is sent from a browser, phone, or backend server." ,
"authors" : "mxpnl"
} ,
{
"title" : "Some Were Meant for C - kell17some-preprint.pdf" ,
"url" : "https://www.cs.kent.ac.uk/people/staff/srk21//research/papers/kell17some-preprint.pdf" ,
"excerpt" : "" ,
"authors" : ""
} ,
{
"title" : "API Gateways Are Going Through an Identity Crisis" ,
"url" : "http://blog.christianposta.com/microservices/api-gateways-are-going-through-an-identity-crisis/" ,
"excerpt" : "API Gateways are going through a bit of an identity crisis these days. Are they centralized, shared resources that facilitate the exposure and governance of APIs to external entities?" ,
"authors" : ""
} ,
{
"title" : "Understanding Database Sharding" ,
"url" : "https://www.digitalocean.com/community/tutorials/understanding-database-sharding" ,
"excerpt" : "Any application or website that sees significant growth will eventually need to scale in order to accommodate increases in traffic. For data-driven applications and websites, it's critical that scaling is done in a way that ensures the security and integrity of their data." ,
"authors" : "Justin Ellingwood"
} ,
{
"title" : "Moving from Ruby to Rust" ,
"url" : "http://deliveroo.engineering/2019/02/14/moving-from-ruby-to-rust.html" ,
"excerpt" : "In the Logistics Algorithms team, we have a service, called Dispatcher, the main purpose of which is to offer an order to the rider, optimally." ,
"authors" : "Andrii Dmytrenko"
} ,
{
"title" : "Getting to Know Python 3.7: Data Classes, async/await and More! | Heroku" ,
"url" : "https://blog.heroku.com/python37-dataclasses-async-await" ,
"excerpt" : "If you're like me, or like many other Python developers, you've probably lived (and maybe migrated) through a few version releases. Python 3.7(." ,
"authors" : "Casey Faist"
} ,
{
"title" : "? What does Unsplash cost in 2019?" ,
"url" : "https://medium.com/p/f499620a14d0" ,
"excerpt" : "Since then, Unsplash has continued to grow tremendously, now powering more image use than the major image media incumbents, Shutterstock, Getty, and Adobe, combined." ,
"authors" : "Luke Chesser"
} ,
{
"title" : "PHP in 2019 - stitcher.io" ,
"url" : "https://stitcher.io/blog/php-in-2019" ,
"excerpt" : "Do you remember the popular "PHP: a fractal of bad design" blog post? The first time I read it, I was working in a crappy place with lots of legacy PHP projects. This article got me wondering whether I should just quit and go do something entirely different than programming." ,
"authors" : ""
}
]
على موقعي، لا يقرأ لامدا رمز الوصول ومفتاح العميل من الطلب.
بدلاً من ذلك، فهو عبارة عن GET بسيط يقرأ الرمز المميز والمفتاح من متغيرات البيئة.
يجب عليك تشغيل yarn
قبل البدء.
البرامج النصية التالية متوفرة:
yarn start
: ابدأ تشغيل Lambda (s) وخدمة الدليل الثابت باستخدام Netlify Dev. هام: yarn build:tw
قبل start
.yarn build:tw
: أنشئ المجموعة الكاملة من أدوات Tailwind CSS المساعدة (المفيدة للتطوير)، وتأكد من التحقق من الشكل الذي سيبدو عليه موقعك مباشرة باستخدام yarn build:css
yarn build
: تشغيل بناء netlify-lambda + إنشاء إنتاج Tailwind CSS (إزالة الفئات غير المستخدمة باستخدام PurgeCSS)yarn build:css
: إنشاء إنتاج Tailwind CSS (يزيل الفئات غير المستخدمة باستخدام PurgeCSS)yarn lint
yarn format
: تشغيل XO، "Linter JavaScript مع الإعدادات الافتراضية الرائعة" (انظر github.com/xojs/xo) مع أو بدون علامة --fix
على التوالي