Пример #1
0
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)
	}

}
Пример #2
0
// Package main ...
package main

import (
	"os"

	"github.com/microplatform-io/platform"
)

var (
	rabbitUser     = os.Getenv("RABBITMQ_USER")
	rabbitPass     = os.Getenv("RABBITMQ_PASS")
	rabbitAddr     = os.Getenv("RABBITMQ_PORT_5672_TCP_ADDR")
	rabbitPort     = os.Getenv("RABBITMQ_PORT_5672_TCP_PORT")
	serverProtocol = platform.Getenv("SERVER_PROTOCOL", "https")
	serverPort     = platform.Getenv("PORT", "443")

	publisher  platform.Publisher
	subscriber platform.Subscriber

	SSL_CERT_FILE = "/tmp/server.cert"
	SSL_KEY_FILE  = "/tmp/server.key"
)
Пример #3
0
package main

import (
	"log"

	"github.com/microplatform-io/platform"
	"strconv"
	"time"
)

var delay = platform.Getenv("DELAY", "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),