LoginSignup
1
1

More than 5 years have passed since last update.

Redstone.Dartをやってみた的なもの

Posted at

4日目です。日付変わってから書き始めるという準備不足もいいとろころですがそれは置いといてとりあえずよろしくお願いします。

はじめに

裏側との疎通確認するのがめんどくさかったんで簡易的なものを作ろうという感じではじめました。選定理由もシンプル・情報がそこそこありそうという2点だけです。

簡単な認証処理とかで使うからそれをネタにして書こうって考えたんですが素晴らしいサンプルがすでにあるじゃないか・・・。
使ってみてハマるところもなかったので要点だけに絞ることにします。

ルーティング・フィルタ

import 'package:redstone/server.dart' as app;

// @route

// 通常
@app.Route("/")
helloWorld() => 'Hello, World!';

// :値の部分を取得
@app.Route("/info/:company/:id")
userInfo(String company, int b) => getUser(company, id);

// 単純なGETならこっち
@app.Route("/search")
search(@app.QueryParam() keyword) => keyword;

// POST
@app.Route("/message/send", methods: const [app.POST])
sendMessage(@app.Body(app.JSON) Map msg) {
  return {
    received: true,
    response: msg['message'] == 'hello' ? 'hi ' + msg['username'] : 'hello'
  };
}

// フィルタ
// 重ねがけも可
@app.Interceptor(r'/user/private/.+')
authenticationFilter() {
  if (app.request.session['username'] == null) {
    app.chain.interrupt(statusCode: HttpStatus.UNAUTHORIZED, responseValue: {"error": "NOT_AUTHENTICATED"});
  } else {
    app.chain.next();
  }
}

GETとPOSTで同じようにとれればもっとよかった、そんなに問題はないけど。

何もしなければreturnする型によってコンテンツタイプが決定される。
簡単なことしかやってないからこういうのは楽できてありがたい。

Returned Value Response type
String text/plain
Map or List application/json
File (MimeType of the file)

はまったところ

webを変更したあとにページみても自動更新されないことがあるから焦る。結局常にタブを2個開いている状態になった・・・。

クライアントとサーバで別ディレクトリ(あるいは別パッケージ)で作りたいけどどうすればいいかわからない。pubspec.yamlの管理が大変。

感想

学習コストがほぼなく、すぐに使い始められたので満足。
リファレンスの説明はもうちょっと欲しい・・・。
仕事で使うことはまだまだなさそう・・・。

明日は再度laco0416さんです。

1
1
1

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
1
1