Skip to content

Commit f96da1c

Browse files
committed
fix: cleanup workflow example and workflow runtime
Signed-off-by: mikeee <[email protected]>
1 parent 2fe6d7a commit f96da1c

File tree

7 files changed

+526
-9
lines changed

7 files changed

+526
-9
lines changed

examples/workflow/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ expected_stdout_lines:
1919
- '== APP == workflow started with id: a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9'
2020
- '== APP == workflow paused'
2121
- '== APP == workflow resumed'
22+
- '== APP == stage: 1'
2223
- '== APP == workflow event raised'
2324
- '== APP == stage: 2'
2425
- '== APP == workflow status: COMPLETED'
@@ -55,6 +56,7 @@ dapr run --app-id workflow-sequential \
5556
- '== APP == workflow started with id: a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9'
5657
- '== APP == workflow paused'
5758
- '== APP == workflow resumed'
59+
- '== APP == stage: 1'
5860
- '== APP == workflow event raised'
5961
- '== APP == stage: 2'
6062
- '== APP == workflow status: COMPLETED'

examples/workflow/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
)
2020

2121
func main() {
22-
wr, err := workflow.NewRuntime("localhost", "50001")
22+
wr, err := workflow.NewRuntime(":50001")
2323
if err != nil {
2424
log.Fatal(err)
2525
}
@@ -116,7 +116,9 @@ func main() {
116116
log.Fatalf("workflow not running")
117117
}
118118

119-
fmt.Printf("workflow resumed\n")
119+
fmt.Println("workflow resumed")
120+
121+
fmt.Printf("stage: %d", stage)
120122

121123
// Raise Event Test
122124

workflow/internal/parse.go

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
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+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package internal
15+
16+
import (
17+
"errors"
18+
"fmt"
19+
"net"
20+
"net/url"
21+
"strings"
22+
)
23+
24+
// Parsed represents a parsed gRPC endpoint.
25+
type Parsed struct {
26+
Target string
27+
TLS bool
28+
}
29+
30+
//nolint:revive
31+
func ParseGRPCEndpoint(endpoint string) (Parsed, error) {
32+
target := endpoint
33+
if len(target) == 0 {
34+
return Parsed{}, errors.New("target is required")
35+
}
36+
37+
var dnsAuthority string
38+
var hostname string
39+
var tls bool
40+
41+
urlSplit := strings.Split(target, ":")
42+
if len(urlSplit) == 3 && !strings.Contains(target, "://") {
43+
target = strings.Replace(target, ":", "://", 1)
44+
} else if len(urlSplit) >= 2 && !strings.Contains(target, "://") && schemeKnown(urlSplit[0]) {
45+
target = strings.Replace(target, ":", "://", 1)
46+
} else {
47+
urlSplit = strings.Split(target, "://")
48+
if len(urlSplit) == 1 {
49+
target = "dns://" + target
50+
} else {
51+
scheme := urlSplit[0]
52+
if !schemeKnown(scheme) {
53+
return Parsed{}, fmt.Errorf("unknown scheme: %q", scheme)
54+
}
55+
56+
if scheme == "dns" {
57+
urlSplit = strings.Split(target, "/")
58+
if len(urlSplit) < 4 {
59+
return Parsed{}, fmt.Errorf("invalid dns scheme: %q", target)
60+
}
61+
dnsAuthority = urlSplit[2]
62+
target = "dns://" + urlSplit[3]
63+
}
64+
}
65+
}
66+
67+
ptarget, err := url.Parse(target)
68+
if err != nil {
69+
return Parsed{}, err
70+
}
71+
72+
var errs []string
73+
for k := range ptarget.Query() {
74+
if k != "tls" {
75+
errs = append(errs, fmt.Sprintf("unrecognized query parameter: %q", k))
76+
}
77+
}
78+
if len(errs) > 0 {
79+
return Parsed{}, fmt.Errorf("failed to parse target %q: %s", target, strings.Join(errs, "; "))
80+
}
81+
82+
if ptarget.Query().Has("tls") {
83+
if ptarget.Scheme == "http" || ptarget.Scheme == "https" {
84+
return Parsed{}, errors.New("cannot use tls query parameter with http(s) scheme")
85+
}
86+
87+
qtls := ptarget.Query().Get("tls")
88+
if qtls != "true" && qtls != "false" {
89+
return Parsed{}, fmt.Errorf("invalid value for tls query parameter: %q", qtls)
90+
}
91+
92+
tls = qtls == "true"
93+
}
94+
95+
scheme := ptarget.Scheme
96+
if scheme == "https" {
97+
tls = true
98+
}
99+
if scheme == "http" || scheme == "https" {
100+
scheme = "dns"
101+
}
102+
103+
hostname = ptarget.Host
104+
105+
host, port, err := net.SplitHostPort(hostname)
106+
aerr, ok := err.(*net.AddrError)
107+
if ok && aerr.Err == "missing port in address" {
108+
port = "443"
109+
} else if err != nil {
110+
return Parsed{}, err
111+
} else {
112+
hostname = host
113+
}
114+
115+
if len(hostname) == 0 {
116+
if scheme == "dns" {
117+
hostname = "localhost"
118+
} else {
119+
hostname = ptarget.Path
120+
}
121+
}
122+
123+
switch scheme {
124+
case "unix":
125+
separator := ":"
126+
if strings.HasPrefix(endpoint, "unix://") {
127+
separator = "://"
128+
}
129+
target = scheme + separator + hostname
130+
131+
case "vsock":
132+
target = scheme + ":" + hostname + ":" + port
133+
134+
case "unix-abstract":
135+
target = scheme + ":" + hostname
136+
137+
case "dns":
138+
if len(ptarget.Path) > 0 {
139+
return Parsed{}, fmt.Errorf("path is not allowed: %q", ptarget.Path)
140+
}
141+
142+
if strings.Count(hostname, ":") == 7 && !strings.HasPrefix(hostname, "[") && !strings.HasSuffix(hostname, "]") {
143+
hostname = "[" + hostname + "]"
144+
}
145+
if len(dnsAuthority) > 0 {
146+
dnsAuthority = "//" + dnsAuthority + "/"
147+
}
148+
target = scheme + ":" + dnsAuthority + hostname + ":" + port
149+
150+
default:
151+
return Parsed{}, fmt.Errorf("unsupported scheme: %q", scheme)
152+
}
153+
154+
return Parsed{
155+
Target: target,
156+
TLS: tls,
157+
}, nil
158+
}
159+
160+
func schemeKnown(scheme string) bool {
161+
for _, s := range []string{
162+
"dns",
163+
"unix",
164+
"unix-abstract",
165+
"vsock",
166+
"http",
167+
"https",
168+
} {
169+
if scheme == s {
170+
return true
171+
}
172+
}
173+
174+
return false
175+
}

0 commit comments

Comments
 (0)