Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1381)

Unified Diff: examples/go/http_handler.go

Issue 1105553002: examples/go: http handler & http server (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: examples/go/http_handler.go
diff --git a/examples/go/http_handler.go b/examples/go/http_handler.go
new file mode 100644
index 0000000000000000000000000000000000000000..3794c9f454ff61791d0b1702bf88f87974fb3262
--- /dev/null
+++ b/examples/go/http_handler.go
@@ -0,0 +1,108 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package main
+
+import (
+ "fmt"
+ "log"
+
+ "golang.org/x/mobile/app"
+
+ "mojo/public/go/application"
+ "mojo/public/go/bindings"
+ "mojo/public/go/system"
+
+ "mojo/services/http_server/public/interfaces/http_request"
+ "mojo/services/http_server/public/interfaces/http_response"
+ "mojo/services/http_server/public/interfaces/http_server"
+ "mojo/services/http_server/public/interfaces/http_server_factory"
+ "mojo/services/network/public/interfaces/net_address"
+)
+
+//#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
+import "C"
+
+type HttpHandler struct{}
+
+func (h *HttpHandler) HandleRequest(request http_request.HttpRequest) (http_response.HttpResponse, error) {
+ resp := "Hello, Go http handler!"
+ r, producer, consumer := system.GetCore().CreateDataPipe(&system.DataPipeOptions{
+ system.MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
+ 1,
+ uint32(len(resp)),
+ })
+ if r != system.MOJO_RESULT_OK {
+ panic(fmt.Sprintf("can't create data pipe: %v", r))
+ }
+
+ producer.WriteData([]byte(resp), system.MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)
+ producer.Close()
+ return http_response.HttpResponse{
+ 200,
+ &consumer,
+ int64(len(resp)),
+ "text/html; charset=utf-8",
+ nil,
+ }, nil
+}
+
+type HttpHandlerDelegate struct {
+ server *http_server.HttpServerProxy
+ handlerStub *bindings.Stub
+}
+
+func (d *HttpHandlerDelegate) Initialize(ctx application.Context) {
+ request, pointer := http_server_factory.CreateMessagePipeForHttpServerFactory()
+ ctx.ConnectToApplication("mojo:http_server").ConnectToService(&request)
+ factory := http_server_factory.NewHttpServerFactoryProxy(pointer, bindings.GetAsyncWaiter())
+
+ addr := &net_address.NetAddress{
+ net_address.NetAddressFamily_IpV4,
+ &net_address.NetAddressIPv4{
+ 8080,
+ [4]uint8{127, 0, 0, 1},
+ },
+ nil,
+ }
+ serverRequest, serverPointer := http_server.CreateMessagePipeForHttpServer()
+ factory.CreateHttpServer(serverRequest, addr)
+ d.server = http_server.NewHttpServerProxy(serverPointer, bindings.GetAsyncWaiter())
+ handlerRequest, handlerPointer := http_server.CreateMessagePipeForHttpHandler()
+ ok, err := d.server.SetHandler("/go", handlerPointer)
+ if !ok {
+ log.Println("can't set handler:", err)
+ return
+ }
+
+ d.handlerStub = http_server.NewHttpHandlerStub(handlerRequest, &HttpHandler{}, bindings.GetAsyncWaiter())
+ go func() {
+ for {
+ if err := d.handlerStub.ServeRequest(); err != nil {
+ log.Println("can't handle http request:", err)
+ return
+ }
+ }
+ }()
+ factory.Close_proxy()
+}
+
+func (d *HttpHandlerDelegate) AcceptConnection(c *application.Connection) {
+ c.Close()
+}
+
+func (d *HttpHandlerDelegate) Quit() {
+ d.server.Close_proxy()
+ d.handlerStub.Close()
+}
+
+//export MojoMain
+func MojoMain(handle C.MojoHandle) C.MojoResult {
+ application.Run(&HttpHandlerDelegate{}, system.MojoHandle(handle))
+ return C.MOJO_RESULT_OK
+}
+
+func main() {
+ app.Run(app.Callbacks{})
+}
« examples/go/BUILD.gn ('K') | « examples/go/README.txt ('k') | examples/go/http_server.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698