ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Firebase] Firebase Functions CLI ์‚ฌ์šฉํ•˜๊ธฐ, ํ•จ์ˆ˜ ๋ฐฐํฌ, ๋ฆฌ์ „ ๋ณ€๊ฒฝ
    PROGRAMMING/๊ธฐํƒ€ 2022. 1. 20. 13:26

     

    Firebase ๋Š” Google ์˜ ๋ชจ๋ฐ”์ผ ๋ฐ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ํ”Œ๋ ›ํผ์ž…๋‹ˆ๋‹ค.

    ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ๋™๊ธฐํ™” ๋“ฑ์˜ ์žฅ์ ์ด ์žˆ์–ด ๋ฐฑ์—”๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์—ฌ๊ฑด์ด ๋˜์ง€ ์•Š๊ฑฐ๋‚˜ ๋น„์šฉ์ ์ธ ์ธก๋ฉด์„ ๊ณ ๋ คํ•œ๋‹ค๋ฉด ์ถฉ๋ถ„ํžˆ ๋งค๋ ฅ์ ์ธ ์นœ๊ตฌ์ž…๋‹ˆ๋‹ค. (๋น„์šฉ์ ์œผ๋กœ '์ €๋ ด'ํ•œ ๊ฒƒ์€ ์–ด๋””๊นŒ์ง€๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ์ ๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์€ ์‹œ์Šคํ…œ.. ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ฉด Firebase ๋Š” ์‚ฌ์šฉํ•˜๊ณ  ์˜ค๊ฐ€๋Š” ๋ฐ์ดํ„ฐ ์–‘์— ๋”ฐ๋ผ ๊ฐ€๊ฒฉ์„ ์ฑ…์ •ํ•˜๋ฏ€๋กœ ๋งŽ์€ ๋น„์šฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ)

     

    ๋‹ค๋งŒ, full text search ๊ฐ€ ๋˜์ง€ ์•Š๊ณ , ์ข€ ๋Š๋ฆฌ๋‹ค๋Š” ๋“ฑ์˜ ๋‹จ์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

    ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์—์„œ firebase ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ๋Š” ์žˆ์ง€๋งŒ, ์ง์ ‘ ์„œ๋ฒ„๋ฅผ ๊ตฌํ˜„ํ–ˆ๋”๋ผ๋ฉด.. ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ์ตœ๊ทผ๋“ค์–ด ๋“ค๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค. firebase ์˜ ๊ธฐ๋ณธ ๋ผˆ๋Œ€์— ์ด๊ฒƒ์ €๊ฒƒ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์žˆ๋Š” ์ง€๊ธˆ์€ ๋˜ ์“ธ๋งŒํ•˜๋‹ค๋Š” ์ƒ๊ฐ๋„ ๋“ญ๋‹ˆ๋‹ค.

     

    firebase ์—๋Š” ๋‹ค์–‘ํ•œ ์ €์žฅ์†Œ ๋ง๊ณ ๋„ functions ๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋Š”๋ฐ, ์ด๋Š” ๋ฐฑ์—”๋“œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ด๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” Serverless ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. Serverless ๋ž€ ์„œ๋ฒ„๊ฐ€ '์‹ค์ œ๋กœ ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋‹ค' ๋ผ๊ธฐ๋ณด๋‹ค๋Š” ์„œ๋ฒ„์— ์ž‘์„ฑํ•ด๋‘” ๋‚ด์šฉ์„ ํ•„์š”ํ•  ๋•Œ (์š”์ฒญ์ด ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ํŠน์ • ํ–‰์œ„๊ฐ€ ํŠธ๋ฆฌ๊ฑฐ๋  ๋•Œ ๋“ฑ) ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Firebase ๋Š” BaaS (Backend as a Service) ๊ณ„์—ด์ด๋ฉฐ Firebase Functions ๋Š” FaaS (Function as a Service) ์ž…๋‹ˆ๋‹ค.

     

    Firebase ์—์„œ๋Š” ํ…์ŠคํŠธ ๊ฒ€์ƒ‰์ด ๋˜์ง€ ์•Š์•„, ์™ธ๋ถ€ ๊ฒ€์ƒ‰ ์—…์ฒด๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด functions ๋˜ํ•œ ๋„์ž…ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

    (Firebase functions ๋Š” Blaze ์š”๊ธˆ์ œ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค)

    ๊ทธ์— ๋”ฐ๋ผ Firebase Functions CLI ๋ฅผ ์„ค์น˜ํ•˜๊ณ , functions ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋ฐฐํฌํ•ด๋ณด๋Š” ๊ณผ์ •์„ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

     

    1) Firebase Functions CLI ์„ค์น˜ํ•˜๊ธฐ

    Firebase Funcitons CLI ๋Š” npm ์œผ๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    $ npm install -g firebase-tools

     

    2) firebase ์„ค์ •

    ์ฒ˜์Œ firebase ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋ฉด login ์„ ๋จผ์ € ํ•˜๊ณ , ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    $ firebase login
    $ firebase init functions

    ๋กœ๊ทธ์ธ์„ ํ•˜์ง€ ์•Š๊ณ  ์ง„ํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด login ์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ๋‚˜์˜ค๊ณ ,

    login ์„ ํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด URL ์„ ํ†ตํ•ด firebase ์— ๋กœ๊ทธ์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    $ firebase login

     

    ์ €์˜ ๊ฒฝ์šฐ ์ผ๋‹จ functions ๋งŒ ๊ณ ๋ คํ•  ๊ฑฐ๋ผ์„œ, firebase init functions ๋ฅผ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

     

    $ firebase init functions

     

    ํ•„์š”์— ๋”ฐ๋ผ firebase init firestore ๋“ฑ์„ ์ถ”๊ฐ€๋กœ ์„ค์น˜ํ•ด์ฃผ๊ฑฐ๋‚˜ firebase init ์œผ๋กœ ์ง์ ‘ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    Realtime Database, Firestore, Functions, Hosting, Storage, Emulators, Remote Config ์˜ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.

     

    $ firebase init

     

    3) functions ์ž‘์„ฑ

    ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ helloWorld ๊ฐ€ ์ž‘์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. (์ฃผ์„์œผ๋กœ)

    ์ด ๋‚ด์šฉ์„ ๊ฐ€์ง€๊ณ  ์‹ค์Šตํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    const functions = require("firebase-functions");
    
    // // Create and Deploy Your First Cloud Functions
    // // https://firebase.google.com/docs/functions/write-firebase-functions
    //
    exports.helloWorld = functions.https.onRequest((request, response) => {
      functions.logger.info("Hello logs!", {structuredData: true});
      response.send("Hello from Firebase!");
    });

     

    (์ฐธ๊ณ : ESLint ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ํ•œ ์ค„์ด 80์ž๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ์•ˆ๋œ๋‹ค)

     

     

     

    4) functions ๋ฐฐํฌ

    ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ํ•จ์ˆ˜๋ฅผ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    $ firebase deploy --only functions

    ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐํฌ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ๋˜๋ฉด, firebase console ์—์„œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๋ฐฐํฌ ์„ฑ๊ณต!

     

    ๋ฐฐํฌ ์ „/ํ›„

     

    ํ•ด๋‹น URL ์— ๋“ค์–ด๊ฐ€๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด Hello from Firebase! ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

     

    ๊ทธ๋Ÿฐ๋ฐ, ์ง€๊ธˆ functions ์˜ ๋ฆฌ์ „์ด us-central1 ์ธ๋ฐ ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” firestore, storage ๋Š” asia-northeast3 ์— ์žˆ์–ด์„œ, functions ์˜ ๋ฆฌ์ „์„ ๋ฐ”๊ฟ”๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ์—ฌ๋Ÿฌ ๋ฆฌ์ „์—์„œ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ฑ์˜ ์ง€์—ฐ ์‹œ๊ฐ„๊ณผ ๊ฐ€๊ฒฉ์ด ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. by firebase

     

    ๊ทธ๋Ÿฐ๋ฐ, ์–ด๋–ค ๋ฆฌ์ „์ธ์ง€์— ๋”ฐ๋ผ functions ๊ฐ€๊ฒฉ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

    asia-northeast3 ๋Š” Tier 2 (๋“ฑ๊ธ‰ 2) ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋œ๋‹ค๋Š”๋ฐ, asia-northeast2 ๋Š” Tier 1 ์ด๋‹ˆ ์ผ๋‹จ ์ €์ชฝ์œผ๋กœ ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    https://cloud.google.com/functions/pricing?authuser=0

     

    region ์€ ๋””ํดํŠธ๊ฐ€ us-central1 ์ธ ๊ฑฐ๋ผ ํ•จ์ˆ˜ ์ž‘์„ฑ ์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    exports.helloWorld = functions
        .region("asia-northeast2")
        .https.onRequest((request, response) => {
          functions.logger.info("Hello logs!", {structuredData: true});
          response.send("Hello from Firebase!");
        });

     

    ์ด๋Ÿฌ๊ณ  deploy ๋ฅผ ํ•˜๋ฉด ๊ธฐ์กด์— ์ƒ์„ฑํ–ˆ๋˜ ํ•จ์ˆ˜ (helloWorld) ๋ž‘ ์ด๋ฆ„์ด ๋™์ผํ•ด์„œ ๋ฎ์–ด์“ฐ๊ธฐ๋ฅผ ํ•  ๊ฑฐ๋ƒ๊ณ  ๋ฌผ์–ด๋ด…๋‹ˆ๋‹ค.

     

     

    ์ƒ์„ฑ์ด ๋˜๊ณ  ๋‚˜๋ฉด ๊ทธ๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž˜ ๋œ ๊ฒƒ์„ console ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

     

     

    ์ด๋ ‡๊ฒŒ ๋ฐฐํฌํ•œ ํ•จ์ˆ˜๋Š” ์•ฑ์—์„œ URL ๋กœ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๋Œ“๊ธ€

Designed by Tistory.