Ocif2016_go_web_freamwork
-
Upload
shuichi-yukimoto -
Category
Internet
-
view
511 -
download
0
Transcript of Ocif2016_go_web_freamwork
2016©MNU co. ltd.
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
n
n
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
APPサーバ リソースサーバクライアント
リクエストリクエスト
レスポンスレスポンス
ワーカーを占有してしまう!
www.usa-mimi.jp
2016©MNU co. ltd.
n
APPサーバクライアント
リクエスト(アップロード)
レスポンス
アップロード完了まで新規リクエスト受付不可!
www.usa-mimi.jp
2016©MNU co. ltd.
n
APPサーバクライアント
リクエスト
通知イベント
レスポンス
リクエスト受けられない
www.usa-mimi.jp
2016©MNU co. ltd.
n
APPサーバクライアント
リクエスト
レスポンス
リクエスト
レスポンス
リクエスト
レスポンス
APPサーバクライアント
リクエスト
レスポンス
リクエスト
レスポンス
リクエスト
レスポンス
これができない
1セット
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
var x int
var x int = 10var x = 10 // 型を省略できるx := 10 // := で varを省略できる
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
var arr [10] inta := [3]int{1, 2, 3}b := [10]int{1, 2, 3}c := [...]int{4, 5, 6}
var fslice []intslice := []byte {'a', 'b', 'c', 'd'}slice = arr[1:3]
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
sum := 0for i := 0; i < 10; i++ {
sum += i}
sum := 1for sum < 1000 {
sum += sum}
for {// 無限ループ
}
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
if x > 10 {fmt.Println("x is greater than 10")
} else {fmt.Println("x is less than 10")
}
func add(x int, y int) int {return x + y
}
www.usa-mimi.jp
2016©MNU co. ltd.
n
go f(x, y, z)
www.usa-mimi.jp
2016©MNU co. ltd.
package main
import ("fmt""runtime"
)
func say(s string) {for i := 0; i < 5; i++ {
runtime.Gosched()fmt.Println(s)
}}
func main() {go say(“world”) //新しいGoroutinesを実行する。say("hello") //現在のGoroutines実行
}
// 実行結果// hello// world// hello// world// hello// world// hello// world// hello
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n
ch <- v // v をチャネル chへ送信するv := <-ch // chから受信した変数を v へ割り当てる
ci := make(chan int)cs := make(chan string)cf := make(chan interface{})
www.usa-mimi.jp
2016©MNU co. ltd.
package main
import "fmt"
func sum(a []int, c chan int) {total := 0for _, v := range a {
total += v}c <- total // send total to c
}
func main() {a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)go sum(a[:len(a)/2], c)go sum(a[len(a)/2:], c)x, y := <-c, <-c // receive from c
fmt.Println(x, y, x + y)}
// 実行結果// -5 17 12// または// 17 -5 12
www.usa-mimi.jp
2016©MNU co. ltd.
package main
import ("fmt""net/http""log"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "Hello!") //ここでwに入るものがクライアントに出力されます。
}
func main() {http.HandleFunc("/", sayhelloName) //アクセスのルーティングを設定します。err := http.ListenAndServe(":9090", nil) //監視するポートを設定します。if err != nil {
log.Fatal("ListenAndServe: ", err)}
}
www.usa-mimi.jp
2016©MNU co. ltd.
nfunc (srv *Server) Serve(l net.Listener) error {
defer l.Close()if fn := testHookServerServe; fn != nil {
fn(srv, l)}var tempDelay time.Duration // how long to sleep on accept failureif err := srv.setupHTTP2_Serve(); err != nil {
return err}baseCtx := context.Background()ctx := context.WithValue(baseCtx, ServerContextKey, srv)ctx = context.WithValue(ctx, LocalAddrContextKey, l.Addr())for {
rw, e := l.Accept()// ...tempDelay = 0c := srv.newConn(rw)c.setState(c.rwc, StateNew) // before Serve can returngo c.serve(ctx)
}}
www.usa-mimi.jp
2016©MNU co. ltd.
package mainimport (
//…"golang.org/x/net/websocket"
)func Echo(ws *websocket.Conn) {
var err errorfor {
var reply stringif err = websocket.Message.Receive(ws, &reply); err != nil {
break}fmt.Println("Received back from client: " + reply)msg := "Received: " + replyif err = websocket.Message.Send(ws, msg); err != nil {
break}
}}func main() {
http.Handle("/", websocket.Handler(Echo))if err := http.ListenAndServe(":1234", nil); err != nil {
log.Fatal("ListenAndServe:", err)}
}
www.usa-mimi.jp
2016©MNU co. ltd.
www.usa-mimi.jp
2016©MNU co. ltd.
www.usa-mimi.jp
2016©MNU co. ltd.
n
APPサーバ
Goサーバ
Nginx
リクエスト
リバースプロキシ
サーバ
認証・ロケーション
www.usa-mimi.jp
2016©MNU co. ltd.
Goサーバ Appサーバクライアント
リクエスト認証等
レスポンス
リソース取得
リソースサーバ
ノンブロック
www.usa-mimi.jp
2016©MNU co. ltd.
n
APPサーバ
Goサーバ
Object Storage
アップロード
必要があればリバースプロキシ
認証・メタデータのDB書込等
アップロード
www.usa-mimi.jp
2016©MNU co. ltd.
n
Goサーバ Appサーバクライアント
アップロード認証等
レスポンス
アップロード
ObjectStorage
メタデータの書込等
ノンブロック
www.usa-mimi.jp
2016©MNU co. ltd.
nfunc UploadOS(user, key, authUrl, container, fileId, contentType string, body io.Reader) (string, error) {
storageUrl, storageToken, err := authenticate(user, key, authUrl)if err != nil {
return "", err}
req := goreq.Request{Uri: storageUrl + "/" + container + "/" + fileId, Method: "PUT", Body: body}req.AddHeader("X-Auth-Token", storageToken)req.AddHeader("Content-Type", contentType)req.AddHeader("Transfer-Encoding", "chunked”)res, err := req.Do()if err != nil {
return "", err}if res.StatusCode != 201 {
log.Info(res.StatusCode)return "", errors.New("upload failed")
}return res.Header.Get("ETag"), nil
}
www.usa-mimi.jp
2016©MNU co. ltd.
n
APPサーバ
Goサーバ監視
通知HTTP, pubsub等
通知
クライアント
www.usa-mimi.jp
2016©MNU co. ltd.
n
Goサーバクライアント
監視
通知
通知
APPサーバ
www.usa-mimi.jp
2016©MNU co. ltd.
n
www.usa-mimi.jp
2016©MNU co. ltd.
www.usa-mimi.jp
2016©MNU co. ltd.
n
n
n