github.com/cs3org/reva/v2@v2.27.7/pkg/events/example/example.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package main 20 21 import ( 22 "log" 23 "time" 24 25 "github.com/cs3org/reva/v2/pkg/events" 26 "github.com/cs3org/reva/v2/pkg/events/example/consumer" 27 "github.com/cs3org/reva/v2/pkg/events/example/publisher" 28 "github.com/cs3org/reva/v2/pkg/events/server" 29 "github.com/cs3org/reva/v2/pkg/events/stream" 30 ) 31 32 // Simple example of an event workflow 33 func main() { 34 // start a server 35 go Server() 36 37 time.Sleep(5 * time.Second) 38 39 // obtain a client 40 c := Client() 41 42 // register a consumer 43 go consumer.Example(c) 44 45 // NOTE: consumer must be registered to get events 46 time.Sleep(time.Millisecond) 47 48 // Publish an event 49 publisher.Example(c) 50 51 // wait for consumer go-routine to print 52 time.Sleep(500 * time.Millisecond) 53 54 } 55 56 // Server generates a nats server 57 func Server() { 58 err := server.RunNatsServer( 59 server.ClusterID("test-cluster"), 60 server.Host("127.0.0.1"), 61 server.Port(9233), 62 ) 63 if err != nil { 64 log.Fatal(err) 65 } 66 } 67 68 // Client builds a nats client 69 func Client() events.Stream { 70 c, err := stream.NatsFromConfig("name of stream", false, stream.NatsConfig{ 71 Endpoint: "127.0.0.1:9233", 72 Cluster: "test-cluster", 73 }) 74 if err != nil { 75 log.Fatal(err) 76 } 77 return c 78 79 }