คู่มือการติดตั้งและโฮสต์ Falcon ด้วยตนเองสำหรับ Frappe Learning

คู่มือนี้จะพาคุณไปดูขั้นตอนการตั้งค่าและติดตั้งระบบ Falcon ของคุณเอง ซึ่งเป็นเอนจินประมวลผลโค้ดแบบเรียลไทม์ (Live code execution engine) สำหรับ Frappe Learning ทั้งนี้หากคุณต้องการผสานระบบการเรียนรู้เข้ากับระบบบริหารจัดการองค์กร สามารถศึกษาข้อมูลเพิ่มเติมได้จากบริการ Nextio ERPNext

Prerequisites

  1. คลาวด์เซิร์ฟเวอร์ (เช่น DigitalOcean droplet หรือ VPS ใดๆ)
  2. Python 3.11 ขึ้นไป
  3. Docker และ Docker Compose
  4. Git
  5. Nginx
  6. โดเมนสำหรับใช้งาน

Clone the Repository

git clone https://github.com/fossunited/falcon
cd falcon

หรือใช้ fork ของคุณเองหากจำเป็น

Setup Python Environment

python3 -m venv env
source env/bin/activate
pip install --upgrade pip
pip install -r dev-requirements.txt

ตรวจสอบให้แน่ใจว่าได้ติดตั้ง uvicorn[standard] เพื่อรองรับการทำงานของ WebSocket:

pip install 'uvicorn[standard]'

Pull Docker Images

Falcon ใช้ Docker containers สำหรับการประมวลผลโค้ดในสภาพแวดล้อมที่ปลอดภัย (sandboxed execution) ให้ดึง image พื้นฐานของ runtime ต่างๆ:

docker pull fossunited/falcon-python:3.9
docker pull fossunited/falcon-golang
docker pull fossunited/falcon-rust
docker pull frappedevs/falcon-javascript

Create systemd Service

สร้างไฟล์ service ที่ /etc/systemd/system/falcon.service:

[Unit]
Description=Falcon LiveCode Service
After=network.target
[Service]
User=root
WorkingDirectory=/root/falcon
ExecStart=/root/falcon/env/bin/uvicorn livecode_server.server:app --host 0.0.0.0 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target

จากนั้น reload และสั่งเริ่มการทำงานของ service:

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable falcon
sudo systemctl start falcon

ตรวจสอบ logs การทำงาน:

journalctl -u falcon -f

Configure Nginx

ติดตั้ง Nginx หากยังไม่ได้ติดตั้งในเครื่อง:

sudo apt update
sudo apt install nginx

สร้างการตั้งค่าไซต์ใหม่ภายใต้ /etc/nginx/sites-available/falcon:

server {
    listen 80;
    server_name falcon.example.com;
    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

เปิดใช้งานไซต์:

sudo ln -s /etc/nginx/sites-available/falcon /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

(ทางเลือกเพิ่มเติม) ตั้งค่า HTTPS ด้วย Certbot:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx

Test Your Setup

ทดสอบสร้าง session บนเบราว์เซอร์หรือ frontend app ของคุณ:

ขั้นแรกให้เพิ่มสคริปต์ลงในหน้าเว็บ

const script = document.createElement('script')
script.src = `https://falcon.frappe.io/static/livecode.js`
document.head.appendChild(script)

จากนั้นรัน session

const session = new LiveCodeSession({
  base_url: "https://falcon.example.com",
  runtime: "python",
  code: "print('Hello')",
  onMessage: console.log
});

คุณควรจะเห็นข้อความตอบกลับ:

{ msgtype: "welcome", message: "welcome to livecode" }

Troubleshooting

  1. เกิดข้อผิดพลาด 403 หรือ 502: ให้ตรวจสอบด้วยคำสั่ง journalctl -u falcon -f และ nginx -t
  2. WebSocket ไม่ทำงาน: ตรวจสอบให้แน่ใจว่าติดตั้ง uvicorn[standard] แล้ว และ Nginx รองรับ WebSockets
  3. ข้อผิดพลาดเกี่ยวกับ Docker ในการทดสอบ: ตรวจสอบว่าได้ทำการดึง runtime images ลงมาครบถ้วนและถูกต้อง

แปลจากเอกสารของ Frappe: https://docs.frappe.io/learning/falcon-self-hosting-guide