Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package main | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "log" | |
| 10 | |
| 11 "golang.org/x/mobile/app" | |
| 12 | |
| 13 "mojo/public/go/application" | |
| 14 "mojo/public/go/bindings" | |
| 15 "mojo/public/go/system" | |
| 16 | |
| 17 "mojo/services/http_server/public/interfaces/http_request" | |
| 18 "mojo/services/http_server/public/interfaces/http_response" | |
| 19 "mojo/services/http_server/public/interfaces/http_server" | |
| 20 "mojo/services/http_server/public/interfaces/http_server_factory" | |
| 21 "mojo/services/network/public/interfaces/net_address" | |
| 22 ) | |
| 23 | |
| 24 //#include "mojo/public/c/system/types.h" | |
|
jamesr
2015/04/22 19:21:55
do we need this just for the main? might be nice t
rogulenko
2015/04/22 20:49:14
Looks like that for this we need to have two .go f
| |
| 25 import "C" | |
| 26 | |
| 27 type HttpHandler struct{} | |
| 28 | |
| 29 func (h *HttpHandler) HandleRequest(request http_request.HttpRequest) (http_resp onse.HttpResponse, error) { | |
| 30 resp := "Hello, Go http handler!" | |
| 31 r, producer, consumer := system.GetCore().CreateDataPipe(&system.DataPip eOptions{ | |
| 32 system.MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | |
| 33 1, | |
| 34 uint32(len(resp)), | |
| 35 }) | |
| 36 if r != system.MOJO_RESULT_OK { | |
| 37 panic(fmt.Sprintf("can't create data pipe: %v", r)) | |
| 38 } | |
| 39 | |
| 40 producer.WriteData([]byte(resp), system.MOJO_WRITE_DATA_FLAG_ALL_OR_NONE ) | |
| 41 producer.Close() | |
| 42 return http_response.HttpResponse{ | |
| 43 200, | |
| 44 &consumer, | |
| 45 int64(len(resp)), | |
| 46 "text/html; charset=utf-8", | |
| 47 nil, | |
| 48 }, nil | |
| 49 } | |
| 50 | |
| 51 type HttpHandlerDelegate struct { | |
| 52 server *http_server.HttpServerProxy | |
| 53 handlerStub *bindings.Stub | |
| 54 } | |
| 55 | |
| 56 func (d *HttpHandlerDelegate) Initialize(ctx application.Context) { | |
| 57 request, pointer := http_server_factory.CreateMessagePipeForHttpServerFa ctory() | |
| 58 ctx.ConnectToApplication("mojo:http_server").ConnectToService(&request) | |
| 59 factory := http_server_factory.NewHttpServerFactoryProxy(pointer, bindin gs.GetAsyncWaiter()) | |
| 60 | |
| 61 addr := &net_address.NetAddress{ | |
| 62 net_address.NetAddressFamily_IpV4, | |
| 63 &net_address.NetAddressIPv4{ | |
| 64 8080, | |
| 65 [4]uint8{127, 0, 0, 1}, | |
| 66 }, | |
| 67 nil, | |
| 68 } | |
| 69 serverRequest, serverPointer := http_server.CreateMessagePipeForHttpServ er() | |
| 70 factory.CreateHttpServer(serverRequest, addr) | |
| 71 d.server = http_server.NewHttpServerProxy(serverPointer, bindings.GetAsy ncWaiter()) | |
| 72 handlerRequest, handlerPointer := http_server.CreateMessagePipeForHttpHa ndler() | |
| 73 ok, err := d.server.SetHandler("/go", handlerPointer) | |
| 74 if !ok { | |
| 75 log.Println("can't set handler:", err) | |
| 76 return | |
| 77 } | |
| 78 | |
| 79 d.handlerStub = http_server.NewHttpHandlerStub(handlerRequest, &HttpHand ler{}, bindings.GetAsyncWaiter()) | |
| 80 go func() { | |
| 81 for { | |
| 82 if err := d.handlerStub.ServeRequest(); err != nil { | |
| 83 log.Println("can't handle http request:", err) | |
| 84 return | |
| 85 } | |
| 86 } | |
| 87 }() | |
| 88 factory.Close_proxy() | |
| 89 } | |
| 90 | |
| 91 func (d *HttpHandlerDelegate) AcceptConnection(c *application.Connection) { | |
| 92 c.Close() | |
| 93 } | |
| 94 | |
| 95 func (d *HttpHandlerDelegate) Quit() { | |
| 96 d.server.Close_proxy() | |
| 97 d.handlerStub.Close() | |
| 98 } | |
| 99 | |
| 100 //export MojoMain | |
| 101 func MojoMain(handle C.MojoHandle) C.MojoResult { | |
| 102 application.Run(&HttpHandlerDelegate{}, system.MojoHandle(handle)) | |
| 103 return C.MOJO_RESULT_OK | |
| 104 } | |
| 105 | |
| 106 func main() { | |
| 107 app.Run(app.Callbacks{}) | |
| 108 } | |
| OLD | NEW |