| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 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 |
// https://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 |
// This application processes orders for converting images to PNG format. It |
| 16 |
// consists of two components: a frontend, which serves web pages that people |
| 17 |
// can use to place and view orders; and a processor, which performs the |
| 18 |
// conversions. This binary can run both together in one process (the default), |
| 19 |
// or it can run either on its own. Either way, the two components: |
| 20 |
// - communicate over a topic using the gocloud.dev/pubsub API; |
| 21 |
// - write orders to a database using the gocloud.dev/docstore API; |
| 22 |
// - and save image files to cloud storage using the gocloud.dev/blob API. |
| 23 |
// |
| 24 |
// This application assumes at-least-once processing. Make sure the pubsub |
| 25 |
// implementation you provide to it has that behavior. |
| 26 |
package main |
| 27 |
|
| 28 |
import ( |
| 29 |
"context" |
| 30 |
"flag" |
| 31 |
"log" |
| 32 |
"os" |
| 33 |
"path/filepath" |
| 34 |
|
| 35 |
"gocloud.dev/blob" |
| 36 |
"gocloud.dev/docstore" |
| 37 |
"gocloud.dev/pubsub" |
| 38 |
) |
| 39 |
|
| 40 |
var ( |
| 41 |
requestTopicURL = flag.String("request-topic", "mem://requests", "gocloud.dev/pubsub URL for request topic") |
| 42 |
requestSubURL = flag.String("request-sub", "mem://requests", "gocloud.dev/pubsub URL for request subscription") |
| 43 |
bucketURL = flag.String("bucket", "", "gocloud.dev/blob URL for image bucket") |
| 44 |
collectionURL = flag.String("collection", "mem://orders/ID", "gocloud.dev/docstore URL for order collection") |
| 45 |
|
| 46 |
port = flag.Int("port", 10538, "HTTP port for frontend") |
| 47 |
runFrontend = flag.Bool("frontend", true, "run the frontend") |
| 48 |
runProcessor = flag.Bool("processor", true, "run the image processor") |
| 49 |
) |
| 50 |
|
| 51 |
func main() { |
| 52 |
flag.Parse() |
| 53 |
conf := config{ |
| 54 |
requestTopicURL: *requestTopicURL, |
| 55 |
requestSubURL: *requestSubURL, |
| 56 |
bucketURL: *bucketURL, |
| 57 |
collectionURL: *collectionURL, |
| 58 |
} |
| 59 |
frontend, processor, cleanup, err := setup(conf) |
| 60 |
if err != nil { |
| 61 |
log.Fatal(err) |
| 62 |
} |
| 63 |
defer cleanup() |
| 64 |
|
| 65 |
// Run the frontend, or the processor, or both. |
| 66 |
// When we want to run both, one of them has to run in a goroutine. |
| 67 |
// So it's simpler to run both in goroutines, even if we only need |
| 68 |
// to run one. |
| 69 |
errc := make(chan error, 2) |
| 70 |
if *runFrontend { |
| 71 |
go func() { errc <- frontend.run(context.Background(), *port) }() |
| 72 |
log.Printf("listening on port %d", *port) |
| 73 |
} else { |
| 74 |
errc <- nil |
| 75 |
} |
| 76 |
if *runProcessor { |
| 77 |
go func() { errc <- processor.run(context.Background()) }() |
| 78 |
log.Println("processing") |
| 79 |
} else { |
| 80 |
errc <- nil |
| 81 |
} |
| 82 |
// Each of the goroutines will send once to errc, so receive two values. |
| 83 |
for i := 0; i < 2; i++ { |
| 84 |
if err := <-errc; err != nil { |
| 85 |
log.Fatal(err) |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// config describes the URLs for the resources used by the order application. |
| 91 |
type config struct { |
| 92 |
requestTopicURL string |
| 93 |
requestSubURL string |
| 94 |
bucketURL string |
| 95 |
collectionURL string |
| 96 |
} |
| 97 |
|
| 98 |
// setup opens all the necessary resources for the application. |
| 99 |
func setup(conf config) (_ *frontend, _ *processor, cleanup func(), err error) { |
| 100 |
addCleanup := func(f func()) { |
| 101 |
old := cleanup |
| 102 |
cleanup = func() { old(); f() } |
| 103 |
} |
| 104 |
|
| 105 |
defer func() { |
| 106 |
if err != nil { |
| 107 |
cleanup() |
| 108 |
cleanup = nil |
| 109 |
} |
| 110 |
}() |
| 111 |
|
| 112 |
ctx := context.Background() |
| 113 |
cleanup = func() {} |
| 114 |
|
| 115 |
reqTopic, err := pubsub.OpenTopic(ctx, conf.requestTopicURL) |
| 116 |
if err != nil { |
| 117 |
return nil, nil, cleanup, err |
| 118 |
} |
| 119 |
addCleanup(func() { reqTopic.Shutdown(ctx) }) |
| 120 |
|
| 121 |
reqSub, err := pubsub.OpenSubscription(ctx, conf.requestSubURL) |
| 122 |
if err != nil { |
| 123 |
return nil, nil, cleanup, err |
| 124 |
} |
| 125 |
addCleanup(func() { reqSub.Shutdown(ctx) }) |
| 126 |
|
| 127 |
burl := conf.bucketURL |
| 128 |
if burl == "" { |
| 129 |
dir, err := os.MkdirTemp("", "gocdk-order") |
| 130 |
if err != nil { |
| 131 |
return nil, nil, cleanup, err |
| 132 |
} |
| 133 |
if os.PathSeparator == '\\' { |
| 134 |
burl = "file:///" + filepath.ToSlash(dir) |
| 135 |
} else { |
| 136 |
burl = "file://" + dir |
| 137 |
} |
| 138 |
addCleanup(func() { os.Remove(dir) }) |
| 139 |
} |
| 140 |
bucket, err := blob.OpenBucket(ctx, burl) |
| 141 |
if err != nil { |
| 142 |
return nil, nil, cleanup, err |
| 143 |
} |
| 144 |
addCleanup(func() { bucket.Close() }) |
| 145 |
|
| 146 |
coll, err := docstore.OpenCollection(ctx, conf.collectionURL) |
| 147 |
if err != nil { |
| 148 |
return nil, nil, cleanup, err |
| 149 |
} |
| 150 |
addCleanup(func() { coll.Close() }) |
| 151 |
|
| 152 |
f := &frontend{ |
| 153 |
requestTopic: reqTopic, |
| 154 |
bucket: bucket, |
| 155 |
coll: coll, |
| 156 |
} |
| 157 |
p := &processor{ |
| 158 |
requestSub: reqSub, |
| 159 |
bucket: bucket, |
| 160 |
coll: coll, |
| 161 |
} |
| 162 |
return f, p, cleanup, nil |
| 163 |
} |