跳至內容
出自 Arch Linux 中文维基

GNU ArtanisScheme 程式語言的第一個產品級現代網絡框架。它的設計和維護都以穩健、快速和易於使用為目標,適用於專業的網絡開發。

GNU Artanis 以 GPLv3+ & LGPLv3+ 發布。它非常輕量級——新手也能輕鬆破解和學習。它具有一個完整的網絡伺服器實現,包括一個錯誤頁面處理程序。它支持的資料庫(通過 guile-dbi)有 MySQLSQLitePostgreSQL

安裝

Guix

如果您已經安裝 Guix 包管理器,可以直接使用以下命令安裝:

guix install artanis

手動安裝

安裝 guilebase-develnss 等基礎軟體包。

安裝 guile-curlAURguile-jsonAURguile-redisAUR

切換至一個新目錄,下載 最新版本原始碼壓縮文件解壓,切換至解壓得到的目錄,運行:

$ ./autogen.sh –no-configure 
$ ./configure 
$ make 
# make install 

AUR

安裝 artanisAURartanis-gitAUR

提示:上述兩個軟體包的維護者均為 LS-Shandong,若打包有問題可以直接用中文交流。

配置

首次運行 Artanis 時需要配置文件。

  • 如果使用最小模式,例如,所有代碼都在腳本文件中,而不在應用程式目錄下。配置文件必須命名為 /etc/artanis/artanis.conf
  • 如果使用應用程式目錄,配置文件 conf/artanis.conf 會自動生成。

使用

在控制台中鍵入 guile,進入 Guile REPL。屏幕上將顯示以下文本:

GNU Guile 3.0.9
Copyright (C) 1995-2023 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)>

本文許多對 Artanis 的操作都會在這裡完成。

運行

簡單 HTTP 伺服器

在控制台中運行這段代碼:

guile -c "(use-modules (artanis artanis))(init-server)(run)"

您會看到這個屏幕:

Anytime you want to quit just try Ctrl+C, thanks!
http://127.0.0.1:3000


運行一個使用 GNU Artanis 的站點

這是最簡單的網站運行方式:

#!/bin/env guile
!#
(use-modules (artanis artanis))
(init-server)
(get "/hello" (lambda () "hello world"))
(run)

運行伺服器

scheme@(guile-user)> (run #:host #f #:port #f #:debug #f #:use-db? #f #:dbd #f #:db-username #f #:db-passwd #f #:db-name #f)

關鍵字的值為 #f,默認情況下將從配置文件中獲取值。

但你也可以定義它們:

  • #:host:主機名。
  • #:port:伺服器的套接字埠。
  • #:debug:設置 #t 如果您想要啟用調試模式,日誌將更加詳細。
  • #:use-db?:設置 #t 如果您想使用資料庫,GNU Artanis 將初始化資料庫連接。
  • #:dbd:選擇一個 dbd。這些是支持的三個:postgresqlmysqlsqlite3
  • #:db-username:你的資料庫伺服器的用戶名。
  • #:db-passwd:上述用戶的資料庫密碼。
  • #:db-name:要使用的資料庫名稱。

與 Nginx 一起工作

您可以使用反向代理嘗試 GNU Artanis+Nginx。

雖然 GNU Artanis 有很好的伺服器核心,但官方還是建議使用 Nginx 作為前端伺服器。除了性能增強外,它還不易受到攻擊。

以下是 /etc/nginx/nginx.conf 的一些示例行:

location / {
proxy_pass http://127.0.0.1:''1234'';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

要使其正常工作,請在編輯文件後重啟 Nginx,並運行 GNU Artanis:

scheme@(guile-user)> (run #:port 1234)

參見