GithubHelp home page GithubHelp logo

chatroom-server's Introduction

ChatRoom Server

C言語製チャットルームアプリ用自作サーバ

使い方

パスに対してハンドラを登録し,サーバを起動する.

SERVER server = server_create();

server_add_path(&server, "/", root_handler);
server_add_path(&server, "/games", root_handler);
server_add_path(&server, "/*", room_handler);

server_start(&server, 8080);

ビルド

make

仕様

ライブラリ

  • server_create() でサーバのインスタンスを作成.
  • server_add_path() でパスに対してハンドラを登録.
  • パスが存在する場合はそのハンドラを呼び出す.
  • 直接的なパスが存在しない場合は,ワイルドカード /* に対するハンドラを呼び出す.
  • /404 に対するハンドラが存在しない場合は,デフォルトのハンドラを呼び出す.
  • server_start() でサーバを起動.(リクエスト毎に fork)
  • server_start_thread() でサーバを起動.(リクエスト毎にスレッドを生成)

HTTP

  • GET / : チャットルーム一覧の JSON を返す

    [ "room1", "room2", "room3", ... ]
  • POST / : 新規チャットルームの作成

    { "room": "doly lab" }
  • GET /{room} : チャットルームのメッセージ一覧の JSON を返す

    {
        "room": "room name",
        "messages": [
            {
                "id": "tom",
                "timestamp": "2020-12-24 12:00:00",
                "message": "Yo"
            },
            {
                "id": "jerry",
                "timestamp": "2020-12-24 12:00:00",
                "message": "Hi!"
            },
    
            ...
        ]
    }
  • POST /{room} : チャットルームにメッセージを送信

    {
        "id": "tom",
        "message": "Yo"
    }

使用したライブラリ

  • sqlite3: データベース
  • sarson: JSON パーサ

工夫した点

  • パスによって実行する関数を決めるようにした.
  • 簡潔なライブラリの使い方.
  • ワイルドカード /* による動的にルーティング.

課題

  • Makefile の理解が浅くハンドラが追加しにくい.
    • ハンドラを追加したときに Makefile を書き換えないようにしたい.
  • yaml などの設定ファイルを読み込んで,ハンドラを登録するようにしたい.
  • C言語でテストするのは大変そうなのでぶっつけ本番で作ってしまった.
  • パスをネストさせる記述を工夫したい.
    • 構造体を再帰的なものにする.
    • ルート以外のワイルドカードに対応させる.
  • clang-format で気に入らないフォーマットが残っている.
  • HTTP の実装が最低限.
    • ヘッダを全く使っていない.
  • データベース系の処理をハンドラと同じファイルに書けばよかった.
  • sqlite3 のタイムスタンプの時刻合わせ.

chatroom-server's People

Contributors

jme-rs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.