Fighter Laf in cloud development lets you write code like a blog

Dear cloud native movers and PPT architects, have you ever thought about writing code as easily as writing articles?

How can I write code like writing articles? The ideal requirement should be to be able to write and debug functions online, without restarting the service, view the running log of the function on the web anytime, anywhere, without connecting to the server, without tossing the database, object storage, Nginx, etc., and the application can be launched anytime , anywhere. Come, swipe, and post!

At this time, the king of understanding is online, my dear, this is serverless, the vision of serverless is that users only need to focus on development intentions, write function codes, and upload them to the code repository, and do not need to care about other things, the serverless platform will automatically allocate computing resources, and run functions on demand.

You know a lot, but unfortunately, only Serverless can't meet the above requirements.

To write code like writing articles, in addition to serverless to provide function computing services, the following prerequisites are also required:

  • Cloud database: Provide out-of-the-box database services for application development;
  • Cloud storage: Provide professional file object storage services for application development, compatible with S3 and other storage service interfaces;
  • WebIDE: Write code online, complete type prompts, code auto-completion, and release online at will!

Knowing Wang is online again. I also understand this. Tencent Cloud Development, Google Firebase and UniCloud provide such functions.

Would you be ecstatic if I told you that there is now a platform that has all the above conditions and is completely open source , and that every development team can have its own cloud development platform at any time?

Yes, that platform is Laf.

应用列表

Life is short, I use Laf for the front end, Laf for the back end, Laf for cloud development users, Laf for independent developers, Laf for start-up teams, and I use Laf too . Link up 🔗:


It is worth mentioning that the number of Stars in Laf increased by nearly 300 in one day last Friday, and the number of Stars in the following consecutive days increased vertically, which is simply simple.

👀 What is Laf?

Laf is essentially a one-stop development platform that integrates "function computing", "database", and "object storage", which can quickly develop and publish applications. It includes the following functions:

cloud function

Cloud functions are JavaScript codes that run in the cloud, and developers familiar with Node.js can get started directly.

Cloud functions can also be written in Typescript, without the need to manage the server. You can write, debug online, and save the backend code online in the development console to run the backend code.

In your application, most data can be obtained by directly operating the database on the client side, but "non-database operations" are usually used in business, such as registration, login, file operations, transactions, third-party interfaces, etc., which can be directly Implemented using cloud functions.

For details, please refer to the documentation of Cloud Functions .

云函数

cloud database

The front-end can use laf-client-sdk to "directly connect" to the cloud database, without the need to interface with the server.

By configuring the corresponding access policy in the development console, the client can also operate the database securely.

For details, please refer to the cloud database documentation .

云数据库

cloud storage

laf cloud storage is an object storage service provided by the basic MinIO. laf provides an object storage service that is fully compatible with MinIO (aws s3-api). Before the laf oss documentation goes online, developers can refer to the MinIO documentation to use laf oss.

云存储

static hosting

laf supports the hosting of static websites, and can quickly launch static websites without tossing Nginx.

👨‍💻 Who is suitable for using Laf ?

  1. Front-end developer+ laf = full-stack developer, front-end becomes full-stack in seconds, becoming a real big front-end.

    • laf provides laf-client-sdk for front-end, suitable for any js running environment;
    • laf The cloud function is developed using js/ts, the front-end and back-end codes are not separated, and there is no threshold to get started quickly;
    • laf Provides static website hosting, which can directly deploy the front-end web pages, without the need to configure servers, Nginx, domain names, etc.;
    • laf the future, a variety of client SDKs (Flutter/Android/iOS, etc.) will be provided to provide back-end development services and consistent development experience for all client developers.
  2. Back-end developers can be freed from trivial matters, focus on the business itself, and improve development efficiency.

    • laf can save server operation and maintenance, multi-environment deployment and management effort;
    • laf can let you say goodbye to configuring and debugging Nginx;
    • laf can let you say goodbye to "manually deploying databases, security concerns and other repetitive tasks for each project";
    • laf can let you say goodbye to the repetitive and tedious iterative experience of "modify once and publish for half a day";
    • laf It allows you to view the running log of the function on the Web anytime, anywhere, without having to connect to the server and search for it;
    • laf It allows you to "write a function like a blog".
  3. Cloud development users, if you are a WeChat cloud development user, you can not only get a more powerful and fast development experience, but also not be locked by the WeChat cloud development platform.

    • You can provide customers with source code delivery, and privately deploy a set of laf + your cloud development application, but using closed-source cloud development services cannot deliver independently runable source code;
    • You can deploy your product to your own server at any time according to your future needs, laf is open source and free;
    • You can even modify and customize your own cloud development platform, laf which is open source and highly extensible.
  4. Node.js developer, laf is developed with Node.js, you can use laf as a more convenient Node.js development platform or framework.

    • You can write and debug functions online without restarting the service, and you can use it with one-click release;
    • You can view and retrieve function call logs online;
    • You can get your application online anytime, anywhere without having to mess with databases, object storage, and Nginx;
    • You can easily upload a piece of Node.js code to the cloud, such as a piece of crawler, a piece of monitoring code, and write Node like a blog!
  5. Independent developers, start-up entrepreneurial teams, save costs, start quickly, and focus on business.

    • Reduce the process of starting project development, start quickly, and shorten the product verification cycle;
    • Greatly improve the iteration speed, respond to changes at any time, and release at any time;
    • Focus on the product business itself, quickly launch the minimum available product (MVP), and quickly conduct product and market verification;
    • One person + laf = team.

💥 What can Laf be used for?

laf is the back-end development platform of the application, which can theoretically do any application!
  1. Use laf to quickly develop WeChat Mini Programs/Official Accounts: e-commerce, social, tools, education, finance, games, short videos, communities, businesses and other applications!

    • WeChat applet strongly requires https access, you can directly use [lafyun.com]; ( http://www.lafyun.com "lafyun.com") to create an application to provide https interface services for the applet;
    • The application's h5 page and management terminal (admin) can be directly deployed to the static hosting by laf ;
    • Host h5 directly to laf , and configure the assigned dedicated domain name to the official account to access online;
    • Use cloud functions to implement WeChat authorization, payment and other services;
    • Use cloud storage to store user data such as videos and avatars.
  2. Develop Android or iOS applications.

    • Use cloud functions, cloud databases, and cloud storage for business processing;
    • The back-end management (admin) of the application can be directly deployed to static hosting by laf ;
    • You can use cloud functions to implement WeChat authorization, payment, hot update and other services.
  3. Deploy personal blogs and corporate official websites.

    • Deploy statically generated blogs such as vuepress / hexo / hugo to laf static hosting, see laf-cli
    • Cloud functions can be used to process user messages, comments, access statistics and other services
    • You can use cloud functions to extend other capabilities of the blog, such as courses, polls, questions, etc.
    • Can use cloud storage to store videos, pictures
    • Cloud functions can be used for crawler, push and other functions
  4. Enterprise informatization construction: a set of enterprise private deployment laf cloud development platform

    • Quickly develop the internal information system of the enterprise, which can be quickly launched, modified, and iterated to reduce costs
    • Support multiple applications, multiple accounts, different departments and different systems, which can be isolated or connected
    • With the help of laf community ecology, you can directly use the existing laf application, out-of-the-box, and reduce costs
    • laf Open source is free, there is no technology lock-in concerns, can be customized and used freely
  5. The "Cloud at Hand" for Individual Developers

    • laf Let developers write a piece of code, and instantly have the ability to use the cloud
    • Just like typing a text in a memo on your phone, it is automatically synchronized to the cloud, and can be accessed and executed by the entire network
    • laf is every developer's "bad pen", writing a function like a note
    • laf is the "personal assistant" of each developer, for example, you can write a function to regularly send SMS and email notifications at any time
  6. other

    • Some users use laf cloud storage as a network disk
    • Some users regard the laf application as a log server, collect client log data, and use cloud functions for analysis and statistics
    • Some users use laf to run crawlers and grab third-party news and information.
    • Some users use laf cloud function as webhook, monitor Git repository submission messages, and push to DingTalk and enterprise WeChat groups
    • Some users use laf cloud function to do dial test and regularly check the health status of online services
    • ...
In the future, lafyun.com will launch an application market to collect these applications, so that laf users can use massive applications with one click!

🚀 How to get started quickly?

Next, we will quickly experience laf cloud development by developing a simple "user login/registration" function on lafyun.com.

Preparation

  1. You need to register an account on lafyun.com ;
  2. Log in to the lafyun.com console, click the 新建 button in the upper left corner to create an empty application;
  3. After the application is successfully launched, click the "Development" button on the right to enter the "Development Console" of the application. Next, we will conduct the first laf application function development in the "Development Console".

Writing cloud functions

This example will write two cloud functions: register to handle registration requests, login to handle login requests.

User registration cloud function

On the "Cloud Function" management page, click "New Function" to create a registered cloud function register . Then click the "Develop" button on the right side of the register function to enter WebIDE and write the following code:

 import cloud from '@/cloud-sdk'
import { createHash } from 'crypto'

exports.main = async function (ctx: FunctionContext) {
  const username = ctx.body?.username || ''
  const password = ctx.body?.password || ''

  // check param
  if (!/[a-zA-Z0-9]{3,16}/.test(username)) return { error: 'invalid username' }
  if (!/[a-zA-Z0-9]{3,16}/.test(password))  return { error: 'invalid password' }

  // check username existed
  const db = cloud.database()
  const exists = await db.collection('users')
    .where({ username: username })
    .count()

  if(exists.total > 0) return { error: 'username already existed'}

  // add user
  const { id } = await db.collection('users')
    .add({
      username: username,
      password: createHash('sha256').update(password).digest('hex'),
      created_at: new Date()
    })

  console.log('user registered: ', id)
  return { data: id }
}

Click "Show Debug Panel" (Ctrl/Cmd + B) in the upper right corner to debug and run.

Click the "Save" & "Publish" function to publish and go live!

User login cloud function

Same as above, create login cloud function, write the following code:

 import cloud from '@/cloud-sdk'
import { createHash } from 'crypto'

exports.main = async function (ctx: FunctionContext) {
  const username = ctx.body?.username || ''
  const password = ctx.body?.password || ''

  // check user login
  const db = cloud.database()
  const res = await db.collection('users')
    .where({
      username: username,
      password: createHash('sha256').update(password).digest('hex')
    })
    .getOne()

  if (!res.data)
    return { error: 'invalid username or password' }
  
  // generate jwt token
  const user_id = res.data._id
  const payload = {
    uid: user_id,
    exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7
  }

  const access_token = cloud.getToken(payload)

  return {
    uid: res.data._id,
    access_token: access_token
  }
}

Click "Show Debug Panel" (Ctrl/Cmd + B) in the upper right corner to debug and run.

Click the "Save" & "Publish" function to publish and go live!

Use curl to call cloud functions

Finally, you can view & copy the calling address of the cloud function through the cloud function list page, or replace APPID in the following curl command with your APPID and execute:

 # 注册用户
$ curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "admin"}' https://APPID.lafyun.com/register

# 用户登陆
$ curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "admin"}' https://APPID.lafyun.com/login

APPID can be found on the welcome page of lafyun.com.

Using Cloud Functions in Frontend Projects

Install the laf client sdk in your frontend project:

 $ npm install laf-client-sdk

Finally, you can call these two cloud functions in your Vue/React/Angular/Applet page to complete the specific login and registration functions! E.g:

 // user.ts

import { Cloud } from 'laf-client-sdk'

const cloud = new Cloud({ 
  baseUrl: "https://APPID.lafyun.com",
  getAccessToken: () => localStorage.getItem('access_token')
})

// regiser function
export async function register(username: string, password: string) {
  const res = await cloud.invoke('register', {
    username: username,
    password: password
  })

  return res
}

// login function
export async function login(username: string, password: string) {
  const res = await cloud.invoke('login', {
    username: username,
    password: password
  })

  if(res.access_token) {
    // save token
    localStorage.setItem('access_token', res.access_token)
  }

  return res
}

Precautions

  • We can view cloud function call logs, online debugging, etc. in the development console;
  • If the call returns 404, check if the function name is misspelled, or if the cloud function has already been published.

📝 Summary

Operation and maintenance students and back-end students, please note that because Laf is too powerful, you no longer need to toss the infrastructure and back-end services, and you may face the risk of being fired due to insufficient workload! Pay attention to control risks!


米开朗基杨
172 声望27 粉丝