You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert
世の中に沢山の「ミドルウェア」が存在しますが、ここで紹介するミドルウェアは、あるメインロジックを大きく変更することなく、その前後に挟む処理のことを指します。 アプリケーションを作成する場合に、メインロジックのハンドラを mainHandler として、ミドルウェア A, B, C を使用していた場合の挙動は以下の順序の通りになります。 request -> A -> B -> C -> mainHandler -> C -> B -> A -> response これを意識するためには、Perl Monger にお馴染みの図を覚えるといいでしょう。玉ねぎ内部のそれぞれの層はミドルウェアを表現しています。 http://blog.nomadscafe.jp/2012/01/plackmiddlewareaccesslog.html これを踏まえて Go でまずは、アプリケーションのミドルウェ
標準ライブラリのhttpパッケージだけでもmiddlewareは簡単に作れますよ、というお話。 おさらい: http.Handlerまたはhttp.HandlerFuncでやり取りする Goのhttp.Handlerやhttp.HandlerFuncをちゃんと理解する - oinume journalに書いたとおり、 http.ListenAndServe ServeMux.Handle に渡すのは http.Handler なので、何かしらのmiddlewareもこのinterfaceを満たすことを考えればよい。もしくは ServeMux.HandleFunc を使うのであれば http.HandlerFunc を使うでもよし。 middlewareはhttp.Handlerを引数に取り、http.Handlerを返す 例えば、 admin という名前のCookieを持っていないとアクセ
はじめに GoでHTTP Serverを作ろうとすると、標準ライブラリを使う場合以下のようなコードをよく書くと思う。 package main import ( "fmt" "log" "net/http" ) func main() { mux := http.NewServeMux() mux.Handle("/hello", http.HandlerFunc(hello)) log.Fatal(http.ListenAndServe(":8080", mux)) } func hello(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello World") } このコードの登場人物としては以下になるが、それぞれなんだっけ?というのをいっつも忘れてしまうの
UPDATE: You can watch a video of me giving this talk at Gophercon 2019:
When writing an HTTP server or client in Go, timeouts are amongst the easiest and most subtle things to get wrong: there’s many to choose from, and a mistake can have no consequences for a long time, until the network glitches and the process hangs. HTTP is a complex multi-stage protocol, so there's no one-size fits all solution to timeouts. Think about a streaming endpoint versus a JSON API versu
questionable services Technical writings about computing infrastructure, HTTP & security. (by Matt Silverlock) Testing Your (HTTP) Handlers in Go ••• You’re building a web (HTTP) service in Go, and you want to unit test your handler functions. You’ve got a grip on Go’s net/http package, but you’re not sure where to start with testing that your handlers return the correct HTTP status codes, HTTP he
Introduction Covered in this tutorial: Creating a data structure with load and save methods Using the net/http package to build web applications Using the html/template package to process HTML templates Using the regexp package to validate user input Using closures Assumed knowledge: Programming experience Understanding of basic web technologies (HTTP, HTML) Some UNIX/DOS command-line knowledge Ge
Go の標準パッケージである net/http を使えば簡単に HTTP サーバーを立てることができる。とは言うものの、自分はそのへんが実際どうなってるのか全然わかってない。つらい。ということで、Go の勉強も兼ねて net/http の動きを少しだけ追ってみることにした。 まず、net/http を用いたよく見かけるサンプルコードを書いてみる。 package main import ( "fmt" "log" "net/http" ) func poyo(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "poyo!") } func main() { http.HandleFunc("/", poyo) if err := http.ListenAndServe(":9090", nil); err != nil { l
こんにちわ、ワカルのアドベントカレンダー2日目を担当する包です。 最近はGoばかり書いているので、Goネタです。 外部への http アクセスをする時に構造を理解しておくと便利な、http.RoundTripper について書きます。 http.RoundTripper とは Go で、外部にhttpアクセスするときには、 net/http パッケージにある、 http.Client を使います。 また、いろいろなAPIのクライアントライブラリの中でも殆どの場合 http.Client が使われていて、定義は以下のようになっています。(一部コメント省略) type Client struct { // Transport specifies the mechanism by which individual // HTTP requests are made. // If nil, Def
こんにちは。斎藤です。 ここ1〜2年、私は仕事でGolangを書くことが増えています。きっかけは、ITインフラをお預かりする中で、お客様のサーバにツールを置く場合でも1つのバイナリさえ置けば良いという手軽さからだったのですが、最近はScalaと並び手軽に並列処理が書けるプログラミング言語として重宝しています。 さて、今回はGolangで作ったhttpdの接続数をLimitListenerを利用して接続数の制限をしてみようというお話です。以下に紹介するお話は、Githubのリポジトリ "github.com/koemu/go-http-max-connections-demo" にデモプログラムを保存しています。Golangのビルド環境がある方は、実際にビルドしながらお試しいただければと思います。 ※Golang 1.5.1でビルドする前提で説明しています モティベーション 仕事でとあるAP
The structure of a programming language reflects the challenges and solutions the designers decided to address. Each designer coming with his/her own background decides to tackle some specific issues in a novel way and/or often decides to borrow existing paradigms from other languages. We can’t, then, fairly judge a language without understanding what problem the language designer was trying to ad
Goは標準パッケージが充実しているのが特徴の1つだが、net/httpも例外ではなくHTTPクライアントの処理が簡単に書ける GET 例えばGETでHTTPリクエストを行う処理を実装したい場合、3通りの方法がある http.Get 関数を実行する Client 型の Get(url) メソッドを実行する Client 型の Do(request) メソッドを実行する 単純なアクセスなら1で良い 一番単純な http.Get(url)によるGET url.Values でクエリを組み立てて、Get関数で指定したURLの末尾にEncode結果をパラメータとして付加すればOK url.Valuesはクエリパラメータをkey-value形式で保持する型。ペアを追加するAddや上書きするSet等のメソッドが提供されている 組み立てたクエリはEncodeメソッドを呼び出す事で?key1=value1&
I've investegated a way to mock HTTP request and response when writing a code to access to a HTTP API with http.Client. Finally, I found that I can do it by just implementing RoundTrip method of http.RoundTripper. Here is an example of mocking and proxying HTTP request and response. Running with go run main.go -mock mock, you can get mocked result and with go run main.go -mock proxy, you can get p
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く