Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud development Three small cases


May 27, 2021 A minimalist getting started manual for cloud development serverless


Table of contents


Case 1: Article Reading Service

We set up our own blogs, or personal websites, often requiring statistical services. S tatistical services are divided into two kinds, one is similar to Baidu, AIA statistics, according to the data to see PV/UV or crowd portraits. B ut sometimes it's also necessary to show the number of readings per article to the reader. Reading statistics services can serve neither the latter nor themselves.
You can refer to the serverless practice: Build your own reading count component in this article.

Count the reading count for a single article/page

The core code is as follows:

  1. 'use strict';
  2. const tcb = require('tcb-admin-node')
  3. const app = tcb.init({
  4. env: "你的环境 ID"
  5. })
  6. const db = app.database()
  7. const _ = db.command
  8. exports.main = async (event, context) => {
  9. let coll = "read_count"
  10. let path = decodeURIComponent(event.queryStringParameters.path || '')
  11. let host = decodeURIComponent(event.queryStringParameters.host || '')
  12. if(!path && !host){
  13. return {
  14. status: 0,
  15. info: '必须传入 host 和 path 参数'
  16. }
  17. }
  18. let data = await db.collection(coll).where({
  19. host: host,
  20. path: path
  21. }).limit(1).get()
  22. //更新
  23. if(data.data.length){
  24. let doc_id = data.data[0]._id
  25. await db.collection(coll).doc(doc_id).update({
  26. count: _.inc(1)
  27. })
  28. let obj = await db.collection(coll).doc(doc_id).get()
  29. return {
  30. status: 1,
  31. count: obj.data[0].count
  32. }
  33. }
  34. //增加
  35. else{
  36. let o = await db.collection(coll).add({
  37. host: host,
  38. path: path,
  39. count: 1
  40. })
  41. return {
  42. status: 1,
  43. count: 1
  44. }
  45. }
  46. };

If you don't want to develop and build, but want to use out-of-the-https://github.com/serverless80/tongji, you can refer to reading https://github.com/serverless80/tongji, which is already deployed as a stand-alone service.

Case 2: Top of the University Program

In 2018, a small program called "Top of the University" was developed that contains data from 2,600 plus colleges and universities; You can sweep the code experience.
Cloud development Three small cases

The effect of the small program is as follows:
Cloud development Three small cases
Cloud development Three small cases

Of course, the data for this little program is open source, https://github.com/vczero/serverless-colleage. W ith data, it's easy to use cloud development to build a small program.

Case 3: Personal Album Program

There are a lot of good memories, there are a lot of important pictures, picture management is very difficult. M obile phones change one a year, picture transmission effort is often lost. O ften prompt that there is not enough space, delete it over and over again. S o idly come up with a small program: a small favorite to manage the private pictures you need to collect. Y ou can scan the experience:

Cloud development Three small cases

The effect of the small program is as follows:
Cloud development Three small cases

The code is already open source, please refer to: https://github.com/vczero/CloudPhoto.

Conclusion

Embrace Serverless, embrace cloud development, use the productivity of the times, do something interesting...