Oracle FN Project กับ go

พอได้อ่านบทความของอาจารย์ Chanwit Kaewkasi เกี่ยวกับเรื่อง FN Project ก็เลยอยากจะทดลองเล่นดูสักหน่อย ส่วนหนึ่งก็เพราะมันถูกเขียนด้วย go นั่นเองใครสนใจบทความของอาจารย์ ตามไปอ่านที่นี่นะครับ

โดย concept ตัวนี้เป็น serverless เพราะฉะนั้นเราก็จะต้อง implement ตามคู่มือ คลิกที่นี่

ใครสนใจจะทดลอง จำเป็นจะต้องมี docker ในเครื่องก่อน เพราะเจ้าตัวนี้จะรันผ่าน docker ในเครื่องเราเลย ส่วนขึ้นตอนก็มีดังนี้

  1. install ด้วยคำสั่ง curl สำหรับ linux และ Windows ส่วน mac มี brew ให้นะ
curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh

2. สั่ง start ตัว FN ขึ้นมาด้วยคำสั่ง

fn start

3. สร้าง project hello สำหรับภาษา go ด้วยคำสั่งง่ายๆ

fn init --runtime go hello

4. cd เข้าไปใน hello

ทีนี้เราก็จะ deploy เจ้า hello เข้าไปใน FN ด้วยคำสั่ง

fn deploy --app myapp --local

เมื่อเสร็จแล้ว เราสามารถทดลองเรียกใช้งานง่ายๆด้วยคำสั่ง curl แบบนี้

curl http://localhost:8080/r/myapp/hello

และจะได้คำตอบแบบนี้

และเรายังสามารถส่ง payload ให้มันตอบชื่อเราออกมาแทนได้ ด้วยการส่ง json เข้าไปด้วยแบบนี้

curl -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/r/myapp/hello

แล้วมันก็จะตอบมาหน้าตาประมาณนี้ครับ

ถึงตรงนี้หลายคนก็อยากจะดูหน้า monitoring ละ ซึ่งมันจะต้องใช้ docker image อีกตัวนึง ก็ตามนี้เลยครับ

docker run --rm -it --link fnserver:api -p 4000:4000 -e "FN_API_URL=http://api:8080" fnproject/ui

แล้วรอสักครู่ให้ docker pull ของมารันให้เรา เมื่อเสร็จแล้วให้เข้า url ตามนี้ครับ

http://localhost:4000/

แล้วเราก็จะเห็นหน้า monitor แบบนี้

โดยรวมแล้วเริ่มต้นเล่นไม่ยากครับ ทำตามคู่มือได้เลย และตัวโปรเจ็คยังมี fdk รองรับหลายภาษาตอนนี้เช่น

  • java
  • ruby
  • rust
  • nodejs
  • python

ตัว framework ก็ดูง่ายๆ simple มากๆ มี yaml file สำหรับกำหนดค่าต่างๆ และยังรันเทสได้อีกด้วย โดย hello ตัวนี้ auto generate test.json มาให้แล้วด้วย โดยเราสามารถสั่งเทสได้ด้วยคำสั่ง

fn test

ก็จะได้เห็น output แบบนี้

ใครสนใจก็ทดลองดูนะครับ เผื่อเป็นตัวเลือกเอาไปใช้งานได้ครับ

odds.team

Odds Team

    Pallat Anchaleechamaikorn

    Written by

    I am just a programmer.

    odds.team

    odds.team

    Odds Team

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade