Help us understand the problem. What is going on with this article?

mod_wsgiでhugを使い、APIを作る

More than 3 years have passed since last update.

hugはPythonでAPIを簡単に作るモジュールです。
mod_wsgiはPythonコードをApacheで動かすためのパッケージです。
これらを組み合わせて、Apacheで動くAPIを簡単に作ります。

hugアプリケーションをmod_wsgiで読み込む

PythonコードにWSGIに対応したapplicationを用意する必要があります。
hugにはapplicationが用意されており、以下ように読み込めます。

  • application = __hug_wsgi__
  • from hoge_hoge import __hug__wsgi__ as application

サンプルコード

# -*- coding:utf-8 -*-

import hug

@hug.get("/", examples="text=Hello World!")
def keyword_extraction_api(text: hug.types.text):
    return {'text': text}

application = __hug_wsgi__

これで、http://hoge-server.com/text=Hello%20World!とリクエストを送れば、{"text": "Hello World!"}が返ってきます。

ちょっとハマったところ

なかなかコードが実行できずハマった。
原因は__hug_wsgi__を最初に書いていたことである。
(おそらく、デコレータを書くと、メソッドが__hug_wsgi__に追加される?ため、最後に__hug_wsgi__を書くと良さそう。)

kanjirz50
日本語処理、ベトナム語処理。
https://sites.google.com/view/takahashi-kanji/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした