func main() {
	serverIPAddr, err := platform.GetMyIp()

	if err != nil {
		logger.Fatalf("Could not resolve server IP address: %s", err)
	}

	if err := ioutil.WriteFile(SSL_CERT_FILE, []byte(strings.Replace(os.Getenv("SSL_CERT"), "\\n", "\n", -1)), 0755); err != nil {
		log.Fatalf("> failed to write SSL cert file: %s", err)
	}

	if err := ioutil.WriteFile(SSL_KEY_FILE, []byte(strings.Replace(os.Getenv("SSL_KEY"), "\\n", "\n", -1)), 0755); err != nil {
		log.Fatalf("> failed to write SSL cert file: %s", err)
	}

	server, _ := NewServer(&Options{
		IPAddr:      platform.Getenv("SERVER_IP", serverIPAddr),
		MicroIpAddr: serverIPAddr,
		Port:        serverPort,
		TLSCertFile: SSL_CERT_FILE,
		TLSKeyFile:  SSL_KEY_FILE,
	})

	connectionManager := platform.NewAmqpConnectionManager(rabbitUser, rabbitPass, rabbitAddr+":"+rabbitPort, "")
	publisher = getDefaultPublisher(connectionManager)
	subscriber = getDefaultSubscriber(connectionManager, server.GetRouterURI())

	router := platform.NewStandardRouter(publisher, subscriber)
	server.SetRouter(router)

	manageRouterState(&platform.RouterConfigList{
		RouterConfigs: []*platform.RouterConfig{
			&platform.RouterConfig{
				RouterType:   platform.RouterConfig_ROUTER_TYPE_HTTP.Enum(),
				ProtocolType: platform.RouterConfig_PROTOCOL_TYPE_HTTPS.Enum(),
				Host:         platform.String(serverIPAddr),
				Port:         platform.String(serverPort),
			},
		},
	})

	mux := http.NewServeMux()

	mux.Handle("/", EnforceHeadersMiddleware(http.HandlerFunc(MicroplatformEndpointHandler(server))))
	mux.Handle("/server", EnforceHeadersMiddleware(http.HandlerFunc(ServerDiscoveryHandler(server))))

	if err := server.ListenAndServeTLS(mux); err != nil {
		logger.Fatalf("Failed to listen and serve: %s", err)
	}

}
// ErrorResponse - Used by handlers so we can return back errors
func ErrorResponse(msg string) []byte {
	return []byte(hex.EncodeToString(GetProtoBytes(&platform.Request{
		Routing:   platform.RouteToUri("resource:///router-http/reply/error"),
		Payload:   GetProtoBytes(&platform.Error{Message: platform.String(msg)}),
		Completed: platform.Bool(true),
	})))
}
Esempio n. 3
0
func main() {
	service, err := platform.NewBasicService("micro-echo")
	if err != nil {
		log.Fatalf("failed to create service: %s", err)
	}

	service.AddHandler("/platform/create/echo", platform.HandlerFunc(func(responseSender platform.ResponseSender, request *platform.Request) {
		d, err := strconv.Atoi(delay)
		if err != nil {
			d = 0
		}

		time.Sleep(time.Duration(d) * time.Second)

		responseSender.Send(platform.GenerateResponse(request, &platform.Request{
			Routing:   platform.RouteToUri("resource:///platform/reply/echo"),
			Context:   request.Context,
			Payload:   request.Payload,
			Completed: platform.Bool(true),
		}))
	}))

	service.AddHandler("/platform/get/documentation", platform.HandlerFunc(func(responseSender platform.ResponseSender, request *platform.Request) {
		responseSender.Send(platform.GenerateResponse(request, &platform.Request{
			Routing: platform.RouteToUri("resource:///platform/reply/documentation"),
			Payload: GetProtoBytes(&platform.Documentation{
				ServiceRoutes: []*platform.ServiceRoute{
					&platform.ServiceRoute{
						Description: platform.String("Create Echo "),
						Request:     &platform.Route{Uri: platform.String("microservice:///platform/create/echo")},
						Responses: []*platform.Route{
							&platform.Route{Uri: platform.String("resource:///platform/reply/echo")},
						},
						Version: platform.String("1.0"),
					},
				},
			}),
			Completed: platform.Bool(true),
		}))
	}))

	service.Run()
}