0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LuaとOpenRestyの環境構築メモ

Last updated at Posted at 2025-02-11

概要

Luaを学ぼうと思い、APIを作りたくなったので環境構築したメモです。

実装

Dockerfile

FROM openresty/openresty:alpine

COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
COPY lua/script.lua /usr/local/openresty/nginx/lua/script.lua

EXPOSE 8080

CMD ["openresty", "-g", "daemon off;"]

docker-compose

docker-compose.yml
version: '3.8'

services:
  openresty:
    build: .
    ports:
      - "8082:8080"
    volumes:
      - ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
      - ./lua/script.lua:/usr/local/openresty/nginx/lua/script.lua

nginx.conf

nginx.conf
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    server {
        listen 8080;
        server_name localhost;

        location /api {
            # キャッシュを無効にすることで、毎回リクエストを処理する(実質ホットロード)
            lua_code_cache off;
            content_by_lua_file /usr/local/openresty/nginx/lua/script.lua;
        }
    }
}

script.lua

lua/script.lua
local function evaluate_hand(hand)
    -- ポーカーの役判定ロジックをここに実装
    return "役の名前"
end

local hand = {"AS", "KS", "QS", "JS", "10S"} -- 例としてロイヤルフラッシュ
ngx.say(evaluate_hand(hand))

実行

docker-compose up -d
curl localhost:8082/api

テスト(未完)

bustedを入れて単体テストもしようと思ったのですが、インストールに詰まってしまったためできたら追記します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?