Compare commits
1629 Commits
v1.15.0
...
v3.0.0-del
| Author | SHA1 | Date | |
|---|---|---|---|
| 6dc7e792c8 | |||
| 81649d51e1 | |||
| 23f5d10ccb | |||
| e3f235acc1 | |||
| fa9ef1c816 | |||
| 77dab8ee15 | |||
| 51fbff3e4a | |||
| bd6493327f | |||
|
|
2141e9631c | ||
| be8d09c663 | |||
| 72bbbe3817 | |||
| c92add984c | |||
| 3542d6c824 | |||
| dc63d96e0b | |||
| 7c9a7e84c7 | |||
| 31180758b4 | |||
| ce25a41fe1 | |||
| 8fa8afdfa4 | |||
| e127547799 | |||
| 1fbf8b2e20 | |||
| e41bb5ebc5 | |||
| 7c311aea19 | |||
| 8a2b122015 | |||
| 40b0870cf8 | |||
| e6ab6d50eb | |||
| a9eff06976 | |||
| 416fe5e4c8 | |||
| ddb53bf8e4 | |||
| 0e6efda528 | |||
| f2413a7789 | |||
| 9553f46cf4 | |||
| 14c97d59c1 | |||
| a59aae760f | |||
| 0a5b34a07b | |||
| 62502ad720 | |||
| 6e43ae7190 | |||
| 0e1f744fcc | |||
| 2fc47782cf | |||
| 34d93306d6 | |||
| 336868ed0d | |||
| 2682f15b8e | |||
| 4c12e38c01 | |||
| 62bfe9c06e | |||
| 24be220f91 | |||
| cacd33e84f | |||
| 9475003059 | |||
| 8532ccebba | |||
| 9c55b1d06a | |||
| efd9075d9b | |||
| 4c4fa00a5d | |||
| 21d5ca1cdd | |||
| ec3c1a02fc | |||
| dc5dc6ab5b | |||
| 1cbd1d2bad | |||
| aa667728a1 | |||
| 9b11ea527a | |||
| 5787a1afb8 | |||
| 74c10f1139 | |||
| 7e3fac8937 | |||
| 6021edc855 | |||
| 8817c110d0 | |||
| d59db9df16 | |||
| 2d1e6db9fd | |||
| 5bfca99627 | |||
| 9ea3149b60 | |||
| 8f03480ed2 | |||
| caec730248 | |||
| f1fde75567 | |||
| 5fe3a46732 | |||
| e7d418183b | |||
| c576749b57 | |||
| c062aab1a9 | |||
| 0252addf05 | |||
| b7338c12d7 | |||
| 7c115823a7 | |||
| 47d007c0b6 | |||
| c9b283be60 | |||
| 112f21006c | |||
| 53654185ba | |||
| 2382446e10 | |||
| 24c20b6907 | |||
| 622490fbf3 | |||
| dd8894e673 | |||
| eb1b14da8a | |||
| 0005f23585 | |||
| fb233374a0 | |||
| 9c695ac343 | |||
| 2c136b005e | |||
| 7cf42589b3 | |||
| 6aa857dfa1 | |||
| 0f19355621 | |||
| aa99378adc | |||
| b4ccde2228 | |||
| 36c53b4917 | |||
| ef773d8d49 | |||
| fc3794f548 | |||
| 67ab44593b | |||
| 8076e410a9 | |||
| dd78ae8658 | |||
| 0f4b1435d9 | |||
| c4a303190a | |||
| 199ff66bd4 | |||
| 98ba3b2788 | |||
| 5ea2590891 | |||
| 0a42845a40 | |||
|
|
e6daa9a838 | ||
|
|
72621e0da4 | ||
|
|
d5da9c0728 | ||
|
|
eee5b98d78 | ||
|
|
cf084b410f | ||
| cf9bdd0f99 | |||
| 06136312bb | |||
| 7a407d5792 | |||
|
|
66df1bb361 | ||
|
|
7eaec450a1 | ||
|
|
5d6b7b3d7d | ||
|
|
2eac8ed64f | ||
|
|
2b2dc2f811 | ||
|
|
21cca297c0 | ||
|
|
19ef225b2f | ||
| 2a23224d91 | |||
|
|
dd2dc7a2b9 | ||
|
|
4413372a3f | ||
|
|
a2a808f2d6 | ||
|
|
7a6669d199 | ||
|
|
09fdd3c121 | ||
|
|
da4159513e | ||
|
|
e1248f90f4 | ||
|
|
3011bad518 | ||
|
|
bb7fe21c46 | ||
|
|
4fd4a116f2 | ||
|
|
50ec6c748f | ||
|
|
7135787b78 | ||
|
|
870a1ebc07 | ||
|
|
61899398b3 | ||
|
|
55d62fc1a5 | ||
|
|
5238a8a85f | ||
|
|
9fffd0419e | ||
|
|
58794df27c | ||
|
|
5a88ea7247 | ||
|
|
374aae1490 | ||
|
|
ccf2f4efd6 | ||
|
|
9380b365de | ||
|
|
f0142febcf | ||
|
|
1fa3ac5599 | ||
|
|
375b67ee16 | ||
|
|
69a53e8070 | ||
|
|
b6e1c7ac99 | ||
|
|
e83a808b05 | ||
|
|
012ec6a998 | ||
|
|
fae4151027 | ||
|
|
e162e6d505 | ||
|
|
c51ef6fc29 | ||
|
|
28d6340f04 | ||
|
|
4fc193f95d | ||
|
|
d072eb6ff2 | ||
|
|
1263806a39 | ||
|
|
959407bad9 | ||
|
|
61d12d3a39 | ||
|
|
4db8ea8f6a | ||
|
|
13f495587e | ||
|
|
593b543230 | ||
|
|
fdce953c15 | ||
|
|
96836f2e43 | ||
|
|
65e6ee8566 | ||
|
|
a7c70c66b1 | ||
|
|
b2582c0992 | ||
|
|
6373cc91b7 | ||
|
|
ed704640aa | ||
|
|
cd9e5a1e9e | ||
|
|
dcf040ec9f | ||
|
|
f838c33008 | ||
|
|
e8ea0f85e9 | ||
|
|
51f8b4ae3d | ||
|
|
69a2032dd7 | ||
|
|
64feb6dff2 | ||
|
|
4c95c65d81 | ||
|
|
4469a41ae7 | ||
|
|
fc67593ee4 | ||
|
|
e7cc3c2210 | ||
|
|
712fe39a62 | ||
|
|
9b14eb8aec | ||
|
|
124b1bd7b7 | ||
|
|
ac1aace214 | ||
|
|
324c4e6886 | ||
|
|
d69a4a30cd | ||
|
|
a6d7b1d710 | ||
|
|
8ee31b94a1 | ||
|
|
37cc7fda92 | ||
|
|
d61cbd29db | ||
|
|
b6ab124d83 | ||
|
|
835343d6a5 | ||
|
|
74907987d1 | ||
|
|
fb8533b74e | ||
|
|
dcf785677f | ||
|
|
991cdba91d | ||
|
|
aefd052dd7 | ||
|
|
2b79910ad9 | ||
|
|
8674dc8e62 | ||
|
|
b93cd0c964 | ||
|
|
39bd6a6ced | ||
|
|
03d47afe47 | ||
|
|
38ec233350 | ||
|
|
eee91ed976 | ||
|
|
07fef9fd33 | ||
|
|
1106f1d996 | ||
|
|
8e126e4fc1 | ||
|
|
1439b101ec | ||
|
|
24e5b2a034 | ||
|
|
971a962894 | ||
|
|
31ed4aa0e8 | ||
|
|
d2cea4b7b7 | ||
|
|
0b73d411ad | ||
|
|
83a64797fb | ||
|
|
e9fc5b1671 | ||
|
|
006bbefaf3 | ||
|
|
3d1ba914fc | ||
|
|
d66803a136 | ||
|
|
9813f98c8b | ||
| c6163bb22f | |||
|
|
cb4a2864da | ||
|
|
f17e4fdb44 | ||
|
|
563768b58a | ||
|
|
9dfeb98111 | ||
|
|
5f4491bb86 | ||
|
|
fbdf1f2c1c | ||
|
|
a3a7434f2c | ||
|
|
592179c0a2 | ||
|
|
9b74bc52d6 | ||
|
|
05f3e1a125 | ||
|
|
16c591d741 | ||
|
|
755b816086 | ||
|
|
7aa92fa8b5 | ||
|
|
5077683b70 | ||
|
|
7f6cefd9c9 | ||
|
|
647ce61dec | ||
|
|
d3326efd4b | ||
|
|
6920677f1e | ||
|
|
1838e4a1ee | ||
|
|
96233b2d9b | ||
|
|
e082ac42a0 | ||
|
|
d7ef224447 | ||
|
|
8c6f4062ef | ||
|
|
0d860c53a6 | ||
| bcc890e47c | |||
|
|
f9bf562393 | ||
| dfa50a888d | |||
|
|
e63b9015ae | ||
| 3627e47f04 | |||
|
|
7d41c2224e | ||
|
|
68927e875b | ||
|
|
0c19a87c89 | ||
|
|
f73ec65ac3 | ||
|
|
b27e71ae64 | ||
|
|
0299517f0d | ||
|
|
e1404a1100 | ||
|
|
057d61063f | ||
|
|
73a3f596e8 | ||
|
|
0287ab8751 | ||
|
|
42c28f2b6d | ||
|
|
a2bb0bea2d | ||
|
|
9f9c748f9b | ||
|
|
a5e9dc21ca | ||
|
|
3f4b58b58c | ||
|
|
0a79db498c | ||
|
|
7c5e3b0f30 | ||
|
|
07fbb06ed8 | ||
|
|
a4252ba69c | ||
|
|
8fe4f1f2c3 | ||
|
|
2e04fcd718 | ||
|
|
7355455020 | ||
|
|
040577fb74 | ||
|
|
4e7621da18 | ||
|
|
8e30ede8c7 | ||
|
|
630ceb5dad | ||
|
|
85ae232936 | ||
|
|
13ea0eec02 | ||
|
|
09ec20fded | ||
|
|
3480e0a64e | ||
|
|
318a80f824 | ||
|
|
6d9a38a747 | ||
|
|
58d6726380 | ||
|
|
e5db6ea8a7 | ||
|
|
3468331506 | ||
|
|
1bac08cc0e | ||
|
|
333320dcb8 | ||
|
|
ce12c040fa | ||
|
|
ee36e26edc | ||
|
|
3ffb899951 | ||
|
|
00bd2bc65f | ||
|
|
86f4235aaf | ||
|
|
b37f9c94b8 | ||
|
|
0ed1c70d29 | ||
|
|
db8e10834b | ||
|
|
0a937745cd | ||
|
|
f5ed7e5833 | ||
|
|
859b9e7786 | ||
|
|
2b033b6495 | ||
|
|
51caf2a24e | ||
|
|
eaa46c2de7 | ||
|
|
90dca65f55 | ||
| 97ae2979ad | |||
|
|
6f309dada3 | ||
|
|
f99b436ec2 | ||
|
|
c817f29d6e | ||
|
|
f744c6248f | ||
|
|
4ff114e798 | ||
|
|
c58ac35dfc | ||
|
|
b5314829fa | ||
|
|
41c7688697 | ||
|
|
b021546c09 | ||
|
|
6898a65508 | ||
|
|
d577dd6abe | ||
|
|
3c633e3577 | ||
|
|
174e44b846 | ||
|
|
a63480a81a | ||
|
|
6d9d94b105 | ||
|
|
64e9185386 | ||
|
|
1b5c83f3cc | ||
|
|
979af853b9 | ||
|
|
a64078b5c3 | ||
|
|
58845d7012 | ||
|
|
dcf01ebbf0 | ||
|
|
355ad2a1af | ||
|
|
b882ff3df9 | ||
|
|
6337c92cd0 | ||
|
|
a95accad56 | ||
|
|
6532b6208b | ||
|
|
0f5c53b6e4 | ||
|
|
deea8fecf4 | ||
|
|
df3e5364ca | ||
|
|
132c1e35fe | ||
|
|
5967a68e78 | ||
|
|
104b7d8f8d | ||
|
|
4f0f4326df | ||
|
|
ee02511658 | ||
|
|
a8fc5590a8 | ||
|
|
bc60f23ff6 | ||
|
|
2000da6fd8 | ||
|
|
5ab475636a | ||
|
|
51b4ab0abc | ||
|
|
687a5e2e58 | ||
|
|
fcd307d902 | ||
|
|
00cd07a3a6 | ||
|
|
a2a1f4dfbd | ||
|
|
2b506b1a2a | ||
|
|
a2550820d3 | ||
|
|
c940961574 | ||
|
|
695cc9d526 | ||
|
|
87543b2c8a | ||
|
|
5f9c3a6efd | ||
|
|
2b889087bd | ||
|
|
ece02a6d21 | ||
|
|
58c6bbbf6b | ||
|
|
c16f4b741c | ||
|
|
83cecdb294 | ||
|
|
8c7c27c573 | ||
|
|
5fd36d6cc0 | ||
|
|
3b40fde68b | ||
|
|
9d3365c4be | ||
|
|
2efb459c66 | ||
|
|
6add74b4f6 | ||
|
|
c67d78f1ef | ||
|
|
a89610ffea | ||
|
|
da9bb11240 | ||
|
|
a3a1a84172 | ||
|
|
1179d7e89a | ||
|
|
a5df913926 | ||
|
|
9ce706191b | ||
|
|
0327f30e3c | ||
|
|
0ce132eb8f | ||
|
|
00b76e0a64 | ||
|
|
aec27be9b4 | ||
|
|
86dfcb819b | ||
|
|
d613804b0a | ||
| 92e9d05432 | |||
|
|
8dfd93e915 | ||
|
|
e5136332e3 | ||
|
|
f10fd4b479 | ||
|
|
74368026a5 | ||
|
|
fde1aa9d6a | ||
|
|
f45cdba9ba | ||
|
|
73c2f25935 | ||
|
|
b270860b79 | ||
|
|
8e81cea96f | ||
|
|
cdd8f9fd82 | ||
|
|
e7ba930236 | ||
|
|
a346064eaf | ||
|
|
47bdd5c993 | ||
|
|
9af12ff9df | ||
| 6c7bcf3883 | |||
|
|
bbc3b7040b | ||
|
|
582f2e8b94 | ||
|
|
bd3ef67328 | ||
|
|
1ccd4cd940 | ||
|
|
aa679f7a73 | ||
|
|
003731ace9 | ||
|
|
7b379bf1f1 | ||
|
|
b6f3e8b715 | ||
|
|
8f6ec21b91 | ||
|
|
e4e56b0f3f | ||
|
|
219d29f664 | ||
|
|
8fb138af06 | ||
|
|
a39e6515da | ||
|
|
2c7fd286de | ||
|
|
8aa2712b4d | ||
|
|
b5c2121cef | ||
|
|
ca9b877646 | ||
|
|
ff49b4fc71 | ||
|
|
222431b57a | ||
|
|
ddb51529a7 | ||
|
|
7c048f331a | ||
|
|
8475183bbb | ||
|
|
10f35db3ed | ||
|
|
b68af8ab63 | ||
|
|
266602a3d6 | ||
|
|
15d5142d9b | ||
|
|
0d88650511 | ||
|
|
8660370dc9 | ||
|
|
73339dde85 | ||
|
|
3f354f3c30 | ||
|
|
c08eb5f892 | ||
|
|
27e41c4ad5 | ||
|
|
1da8a640da | ||
|
|
e7ad031eb8 | ||
|
|
192f548c83 | ||
|
|
d85b4197b4 | ||
|
|
bb5f2e5525 | ||
|
|
f00b696282 | ||
|
|
e2d662608c | ||
|
|
9e9773c9c7 | ||
|
|
2f8e2487f7 | ||
|
|
d6c1fbf841 | ||
|
|
c3b404bab0 | ||
|
|
cd283654eb | ||
|
|
5712cc9c62 | ||
|
|
be5a10a4d4 | ||
|
|
b53a2c67f1 | ||
|
|
cc79692d68 | ||
|
|
796a598b37 | ||
|
|
73b4423682 | ||
|
|
198e942889 | ||
|
|
95703e4565 | ||
|
|
2729569f66 | ||
|
|
67146ecdc2 | ||
|
|
bd049a51e6 | ||
|
|
ffd89599a0 | ||
|
|
496293afa1 | ||
|
|
7d7f4046e8 | ||
|
|
c800070477 | ||
|
|
877fe5fb0a | ||
|
|
dad011cab4 | ||
|
|
f939200b34 | ||
|
|
9c072a372c | ||
|
|
fbb91c6cb7 | ||
|
|
b2cf501952 | ||
|
|
1fce0f02b6 | ||
|
|
12061bd006 | ||
|
|
856c73b341 | ||
|
|
4de19805ba | ||
|
|
c09b871a6b | ||
|
|
e876cb917d | ||
|
|
8f5ef012ff | ||
|
|
287992cef3 | ||
|
|
344ce061ce | ||
|
|
5d14970a55 | ||
|
|
0615fe825f | ||
|
|
6a661fd08c | ||
|
|
f6d9416a9e | ||
|
|
a29676b86a | ||
|
|
dc10f88c12 | ||
|
|
e61edf6280 | ||
|
|
3410a0949b | ||
|
|
9216a47724 | ||
|
|
cf37d64819 | ||
|
|
f0c0f3d4c4 | ||
|
|
c4e3f8c336 | ||
|
|
8875719619 | ||
|
|
c19b349e96 | ||
|
|
14155c7e02 | ||
|
|
3d36398818 | ||
|
|
56a7897c91 | ||
|
|
5efb386224 | ||
|
|
4d2de923cd | ||
|
|
f64b1468a5 | ||
|
|
56f281002b | ||
|
|
0d7250352f | ||
|
|
ef43f01da4 | ||
|
|
c9e5ae6a2b | ||
|
|
8a802d8f7a | ||
|
|
331ab3715c | ||
|
|
6b451a2197 | ||
|
|
b4c0224746 | ||
|
|
500d793fc4 | ||
|
|
16af265e8b | ||
|
|
b222cf8e13 | ||
|
|
f549e20fa2 | ||
|
|
83e9c1fad2 | ||
|
|
c220686c29 | ||
|
|
1b18730d54 | ||
|
|
5764519f5b | ||
|
|
957001f8ad | ||
|
|
0955671e45 | ||
|
|
57b060bac5 | ||
|
|
3136e1409e | ||
|
|
ca791d7e8d | ||
|
|
05858b746c | ||
|
|
09d1450d7d | ||
|
|
1ca1fd411a | ||
|
|
a2d4d62f1c | ||
|
|
8ab20f501c | ||
|
|
366fb228e5 | ||
|
|
bba8c254d7 | ||
|
|
ebd53794af | ||
|
|
2299244332 | ||
|
|
cf61d98635 | ||
|
|
15d1967aaf | ||
|
|
410fec8ee4 | ||
|
|
c831b6c03a | ||
|
|
290595f88e | ||
|
|
ba64518ebd | ||
|
|
b14d63b4a1 | ||
|
|
af2db0a0d9 | ||
|
|
fb255a7e5a | ||
|
|
47c1cb433e | ||
|
|
3fac7d79ab | ||
|
|
25c937fd0e | ||
|
|
e5c1fbc591 | ||
|
|
d781c9ae2d | ||
|
|
54951740bf | ||
|
|
0fb4734e67 | ||
|
|
346e034d0a | ||
|
|
116cc1e9ee | ||
|
|
762a5bc9e8 | ||
|
|
d39b723511 | ||
|
|
5494e935f4 | ||
|
|
e0863bb7eb | ||
|
|
89f86167ad | ||
|
|
dfec1ad6b1 | ||
|
|
66d3e4a595 | ||
|
|
19a03babc4 | ||
|
|
ee24b4f083 | ||
|
|
937ecc8d2f | ||
|
|
6078adb8bc | ||
|
|
39f18b0b70 | ||
|
|
efb64b7dbb | ||
|
|
f892b41299 | ||
|
|
1eb63635b5 | ||
|
|
688228377b | ||
|
|
506006f0fa | ||
|
|
22de001a80 | ||
|
|
d90cc8bf2f | ||
|
|
5a8f19589b | ||
|
|
d61df6363b | ||
|
|
f062013a7b | ||
|
|
fea93a5b7a | ||
|
|
30dc29e17f | ||
|
|
5387f73b5d | ||
| 90dd1f63c8 | |||
|
|
38cdb9cc2f | ||
|
|
b3915b6020 | ||
|
|
08a2de1ef5 | ||
|
|
7a2dea6cc2 | ||
|
|
2a14feed93 | ||
|
|
e8105d22ad | ||
|
|
c76a5e608d | ||
|
|
359b8bc503 | ||
|
|
fccab8ad27 | ||
|
|
46d09ec2bc | ||
|
|
7792dbc34d | ||
|
|
1d29f126f9 | ||
|
|
bcddb98867 | ||
|
|
f48dec1fb0 | ||
|
|
ef9f65c78b | ||
|
|
99f8be5b3d | ||
|
|
9d2fdb84be | ||
|
|
8b004feb9a | ||
|
|
70736e24c0 | ||
| d44adafca5 | |||
|
|
e57b20c1f8 | ||
|
|
94971aee77 | ||
|
|
0ed66d0664 | ||
|
|
7e27c97c6c | ||
|
|
669364985e | ||
|
|
c7440274dd | ||
|
|
8ccbf53dfc | ||
|
|
f908110fb6 | ||
| 9bb1904a38 | |||
| 06220ab8c8 | |||
|
|
da66561d1e | ||
|
|
52861310b0 | ||
| 414b2ec5f8 | |||
|
|
b875868a39 | ||
|
|
8148e0a0f8 | ||
|
|
25c82245b1 | ||
|
|
95a7e21f5f | ||
|
|
83ab47333f | ||
|
|
8d7d6ef358 | ||
|
|
494e0b5060 | ||
|
|
434997e676 | ||
|
|
ec44b67e9f | ||
| e0c9234c0e | |||
| 980b772801 | |||
| a22da39e1c | |||
|
|
7253635cd3 | ||
|
|
0a030f3d8a | ||
|
|
edee3b6765 | ||
|
|
d62ae23a9c | ||
|
|
c68226e9b0 | ||
|
|
041d68b1ce | ||
|
|
85a8f36565 | ||
|
|
f34d58cfbd | ||
|
|
e0a651bfc3 | ||
|
|
cd35f503a0 | ||
|
|
8b3d223fc0 | ||
|
|
bb25bd94c8 | ||
|
|
986e3d3c35 | ||
|
|
616db3442a | ||
|
|
5fe3c0bfe5 | ||
|
|
8849b85a7f | ||
|
|
893bbafa03 | ||
|
|
4c05623a3c | ||
|
|
ec929b3d2f | ||
|
|
2299559397 | ||
|
|
6be53536d3 | ||
|
|
99d4b2b31a | ||
|
|
ff8ad7d4ca | ||
|
|
b692c045b5 | ||
|
|
b5f53595ca | ||
|
|
88176dca53 | ||
|
|
020476614c | ||
|
|
0f42346976 | ||
|
|
692b27578c | ||
|
|
316b81f790 | ||
|
|
5e3262a62c | ||
|
|
053fa0e457 | ||
|
|
501a6bf3ea | ||
|
|
7345ce9192 | ||
| 6fa27373ed | |||
|
|
e55c23164a | ||
|
|
e25ab9f4ca | ||
| bea092f082 | |||
|
|
d7ecb58f6c | ||
|
|
05d2b34e10 | ||
|
|
211fd9b9a3 | ||
|
|
19f0836e70 | ||
|
|
075d7d4fef | ||
|
|
e5c215556e | ||
|
|
7c31edd5f8 | ||
|
|
c4acf3c2cb | ||
|
|
53db26a614 | ||
|
|
dde8f18b52 | ||
|
|
6071b74fb5 | ||
|
|
ab041012b2 | ||
|
|
226d6ad22b | ||
|
|
a08ff90976 | ||
|
|
ae8404d760 | ||
| f00fd7a49e | |||
|
|
ecbc42755c | ||
|
|
16db76bee2 | ||
|
|
dca5305e8a | ||
|
|
c0b0f63757 | ||
|
|
ac5822f1ee | ||
|
|
ae56becbbd | ||
|
|
5bb18e685e | ||
|
|
2dfaab439c | ||
| 62cedf64da | |||
|
|
9961ebb46e | ||
|
|
fe31a71557 | ||
|
|
c9a6b07c52 | ||
|
|
f1e6eff303 | ||
|
|
2de03e5fd7 | ||
|
|
234c192faf | ||
|
|
ea29920afb | ||
| 4d177a782e | |||
| b700d425a4 | |||
|
|
9a5b8ff50d | ||
|
|
c787fd0483 | ||
|
|
1134ea5ff3 | ||
|
|
fd16cd298f | ||
|
|
67e7aa223a | ||
|
|
9d0381306d | ||
|
|
f8837bfcbd | ||
| 268651df18 | |||
|
|
e17825474f | ||
|
|
0c75a0306b | ||
|
|
d61d30ef66 | ||
|
|
71d4253927 | ||
|
|
e515005083 | ||
|
|
4bdc18d64a | ||
|
|
f840a5003e | ||
|
|
5ef1698632 | ||
|
|
1bb6967a38 | ||
|
|
a056bdce7c | ||
|
|
b08c636b44 | ||
|
|
d03a02f2e4 | ||
|
|
08ca61c121 | ||
|
|
962588b649 | ||
|
|
cf67d460b7 | ||
|
|
4e539361fa | ||
| 3ce2ab88f5 | |||
| 0a2363b49b | |||
|
|
ec80ceb8c2 | ||
|
|
ea2bb0275c | ||
|
|
51d4f737b8 | ||
|
|
3f81f685df | ||
|
|
bb1ccf09e8 | ||
|
|
c878237567 | ||
|
|
ac8b6f944e | ||
|
|
0f2006ac50 | ||
|
|
c697eed1be | ||
|
|
b887d91f94 | ||
|
|
39470c1b11 | ||
|
|
6d553cb6fe | ||
|
|
c612d86480 | ||
|
|
3f3d2f5027 | ||
| bc71640fd9 | |||
|
|
b979db6d9d | ||
|
|
57b758db7e | ||
|
|
b5f546b137 | ||
|
|
d4b2c948dd | ||
|
|
b9a5e9d610 | ||
|
|
57853b2849 | ||
|
|
e5268dd0a6 | ||
|
|
4fd12430d0 | ||
|
|
d134b469be | ||
|
|
9a685b2df5 | ||
|
|
6a666c9c7d | ||
|
|
53549b6b30 | ||
|
|
0a27a08184 | ||
|
|
77f0abb0ba | ||
|
|
29cccd0b4a | ||
|
|
bf65dc71c7 | ||
|
|
5bc8ee39f7 | ||
| 8c1b477279 | |||
|
|
f9cfbe96c0 | ||
|
|
2e379ca7d0 | ||
|
|
2659215d5e | ||
| 1063b954de | |||
|
|
4ff959ef50 | ||
|
|
bc1c8223e6 | ||
|
|
27eb7db1c2 | ||
|
|
3ede494945 | ||
|
|
f102aba4c1 | ||
|
|
f2dd091ec0 | ||
|
|
c1ad6d6c7c | ||
|
|
1e7cd8c484 | ||
|
|
bf8ebf8ad2 | ||
|
|
1768958af7 | ||
|
|
bf41d8d28e | ||
|
|
45700eaabe | ||
|
|
48dd30c4c2 | ||
|
|
8ff86ae08b | ||
|
|
b2079669f7 | ||
|
|
2c1d1afd71 | ||
|
|
9a73828782 | ||
|
|
c5d085cff8 | ||
|
|
9f4286fc4e | ||
|
|
77f5cc5023 | ||
| 8400aba81c | |||
|
|
cc027d900e | ||
|
|
bc0dc2e509 | ||
| 1fbc056dd4 | |||
|
|
98fc3dfbad | ||
|
|
4b0e27413e | ||
|
|
6b524e2c55 | ||
|
|
4cac7dcc48 | ||
|
|
e907d24e3b | ||
|
|
39c352f210 | ||
|
|
67cd59d7bc | ||
|
|
3735b0e529 | ||
|
|
4362a885eb | ||
|
|
038b936ce9 | ||
| 6aaad7d63f | |||
|
|
aaee01b1a7 | ||
|
|
71538adfdc | ||
|
|
2ea5b33955 | ||
|
|
3df87510a1 | ||
|
|
9d598836c3 | ||
|
|
05ac3ff274 | ||
|
|
76f6f80318 | ||
|
|
cb96949551 | ||
|
|
87cc4f273b | ||
|
|
f0980e9b30 | ||
|
|
977934f8fd | ||
|
|
9e116731b1 | ||
|
|
316424f0f7 | ||
|
|
bd23dc1f18 | ||
|
|
501fc5c059 | ||
|
|
11e1e9120a | ||
|
|
a81d86ed08 | ||
|
|
7206d5f964 | ||
|
|
b5f5027549 | ||
|
|
e8a86585da | ||
|
|
5374896ed0 | ||
|
|
b6348ba59a | ||
|
|
ca11c4a672 | ||
|
|
900b2d24f9 | ||
|
|
3324d140c0 | ||
|
|
3a378eb7d6 | ||
|
|
574bf5ac69 | ||
|
|
774c0d30a7 | ||
|
|
0f570d98e1 | ||
|
|
7f07e1a642 | ||
|
|
9b546a7242 | ||
|
|
c4442a7533 | ||
|
|
bea7c3f7e7 | ||
|
|
cca9773269 | ||
|
|
600b20fb81 | ||
|
|
31a1ea6fae | ||
| bc7579f1d8 | |||
| 38aed6f0f6 | |||
|
|
7f8b35e295 | ||
|
|
b09dd9a689 | ||
|
|
a82ce4d1ae | ||
|
|
34234fc486 | ||
|
|
4a850ff8a0 | ||
|
|
350dd41732 | ||
|
|
d8cca31738 | ||
|
|
b864b3e350 | ||
|
|
41b746e435 | ||
|
|
906263291b | ||
|
|
46f0bda31e | ||
|
|
d0e47206cc | ||
|
|
ed6fe67880 | ||
|
|
1374a9e528 | ||
|
|
a9c0e043d2 | ||
|
|
49a568e9c0 | ||
|
|
dea2d7ab9f | ||
|
|
ebb1a42d48 | ||
|
|
1096c8fb39 | ||
|
|
91b9c3f92e | ||
|
|
183c8bfb81 | ||
|
|
49a1130281 | ||
|
|
760233b858 | ||
|
|
ede076e899 | ||
|
|
fdcb013f24 | ||
|
|
ce23ab36cb | ||
|
|
61f0619e97 | ||
|
|
cfde3ec3d9 | ||
|
|
4a4c666528 | ||
|
|
8b35c264eb | ||
|
|
4999f6dfd4 | ||
|
|
31c4452fc7 | ||
|
|
2cafa289b6 | ||
| 0241197c6a | |||
|
|
0a15ae9b9d | ||
|
|
d2b6d35220 | ||
|
|
e1bc0f6288 | ||
|
|
cd3d704aa5 | ||
|
|
9de69529ce | ||
|
|
623f0c0c90 | ||
|
|
c766679687 | ||
|
|
df8c0bb5e1 | ||
|
|
d577c32563 | ||
|
|
365dfe9df5 | ||
|
|
ae15793fc3 | ||
|
|
15fcd5ecef | ||
|
|
1750fd8d10 | ||
|
|
525ab094c8 | ||
|
|
bb51b8203e | ||
|
|
75a75c56ad | ||
|
|
26cb6bf5b9 | ||
|
|
9cbbd71855 | ||
|
|
f7655b71ea | ||
|
|
8e4d9e1702 | ||
|
|
20c95d94cd | ||
|
|
0a7d8afe67 | ||
| 7b7a859a03 | |||
| 8a8742f867 | |||
|
|
68b0238a5d | ||
| 1490aff38e | |||
| 3a22efbd7d | |||
| 5e65a46be3 | |||
| 18061723bb | |||
| d6bef84de0 | |||
|
|
82bc3cbf8d | ||
|
|
cffb0a1eae | ||
|
|
134bc1c68a | ||
|
|
6c6c5359b1 | ||
|
|
8dbb5153f4 | ||
|
|
2674790694 | ||
|
|
9fb1d476a2 | ||
|
|
36386354d7 | ||
|
|
bd70820b6b | ||
|
|
956029ae3d | ||
|
|
e0c7f48d20 | ||
|
|
d659e435c6 | ||
|
|
3d274ab6a2 | ||
|
|
1222d076f2 | ||
|
|
76ade7efd9 | ||
| c706ebe3fb | |||
| 9e6db79860 | |||
| 756b346672 | |||
|
|
4db2f5e79d | ||
| 3d7d5ce6b4 | |||
|
|
8282e781e4 | ||
| e4acc63d5f | |||
|
|
45ee5e9ad1 | ||
|
|
b60fde0e64 | ||
|
|
011a783a9e | ||
|
|
6723d17b22 | ||
|
|
47c7181d41 | ||
| b38da6ced0 | |||
|
|
1a53307a78 | ||
|
|
e204f3e2e8 | ||
|
|
329bd09f93 | ||
|
|
4648fd0d09 | ||
|
|
c905df3be6 | ||
|
|
62f9a054a4 | ||
|
|
00e7804f96 | ||
|
|
42b6bf5bbf | ||
|
|
844c456839 | ||
|
|
7182ca1fd0 | ||
| 02839cfba5 | |||
|
|
776a7d6cd6 | ||
| beaa434610 | |||
|
|
6efc5556e5 | ||
|
|
8d0826a031 | ||
|
|
378d03eb66 | ||
|
|
56af826230 | ||
|
|
511ebd8ec2 | ||
|
|
1057ef6acb | ||
|
|
35e2a68a98 | ||
|
|
0e56382107 | ||
|
|
dff98355be | ||
| 8100d26430 | |||
|
|
397a8638f4 | ||
|
|
eb4d2ae6aa | ||
|
|
914340585c | ||
|
|
84b4eb5404 | ||
|
|
fd664f4392 | ||
|
|
86272a3064 | ||
|
|
c1978265ab | ||
|
|
e0e77f3983 | ||
| 9826ddbd64 | |||
|
|
87617be227 | ||
| d559587807 | |||
|
|
9501512219 | ||
|
|
d2f153d795 | ||
|
|
e49be1da42 | ||
|
|
4c6f68d537 | ||
| cbb958def5 | |||
|
|
40ff6ddfcf | ||
|
|
5ad7c36bd4 | ||
|
|
99c3fe2bb8 | ||
|
|
1bd340701b | ||
|
|
c91bf7e9e7 | ||
| 41f8a8cb00 | |||
|
|
cd04111e3d | ||
|
|
00cd2448a4 | ||
|
|
8a41d369f2 | ||
|
|
638c219736 | ||
|
|
b3c631dd38 | ||
| ab73127063 | |||
|
|
03031a694d | ||
|
|
5712aafba9 | ||
| ac333d9d47 | |||
|
|
247707f583 | ||
|
|
d91c14eb30 | ||
|
|
ca8684a886 | ||
|
|
0449138f61 | ||
| 609f4826b3 | |||
| 60993e6275 | |||
|
|
e803fb0855 | ||
| 3543b275e0 | |||
| fbde872e7f | |||
|
|
078dd4eb9b | ||
| 62a644ddd8 | |||
|
|
d8cfa7a295 | ||
|
|
47f1203e97 | ||
|
|
1b4e881d74 | ||
|
|
20ce61da5a | ||
|
|
eef4825be4 | ||
|
|
be9c6141f5 | ||
|
|
1ca4619506 | ||
|
|
f55493993c | ||
| 7b385bf163 | |||
|
|
4125ae8d53 | ||
|
|
48b2a5c37c | ||
|
|
ed83c27f0e | ||
| 241614ff68 | |||
|
|
1a4f608ed1 | ||
| 43b0dbb123 | |||
|
|
b344171c80 | ||
|
|
e3ce45495a | ||
| f01664a551 | |||
| 8ecbdc1cd6 | |||
| 55c19afb0b | |||
| 077063c212 | |||
|
|
9a7a65f05e | ||
| 8ee5607254 | |||
| 11be2c68b9 | |||
| a864f812f1 | |||
|
|
ae60bea8d8 | ||
|
|
a851b9db7a | ||
| d807dac2a7 | |||
| ce2ba71002 | |||
|
|
67c26c71b6 | ||
|
|
9386f36a13 | ||
|
|
6d803d9e45 | ||
|
|
6a9001bdb1 | ||
|
|
3f0c28a815 | ||
|
|
49ffc60afb | ||
|
|
beb5e80e87 | ||
|
|
eebd69c995 | ||
|
|
bc71989e2c | ||
| 89ba602e17 | |||
|
|
f6102bde70 | ||
|
|
7cad77bfc0 | ||
|
|
1f2e067f71 | ||
| b555269b1b | |||
|
|
9200c70202 | ||
|
|
d8377e09c9 | ||
| 0754229878 | |||
| 6b8930a960 | |||
| d0a978bd50 | |||
|
|
afe6861e2f | ||
|
|
962567ef42 | ||
|
|
e21ed3a183 | ||
| 64a5ce9607 | |||
|
|
d651b16acd | ||
|
|
1034837f69 | ||
|
|
80f2bfd5d0 | ||
|
|
6aaaf54275 | ||
|
|
603d37b135 | ||
|
|
53c3bff819 | ||
|
|
dcf859098b | ||
| b4a743898e | |||
|
|
f1e7ea3020 | ||
|
|
5e8d5834eb | ||
|
|
ffdf986aac | ||
|
|
56f8115ea8 | ||
| 5b0175c2e5 | |||
|
|
01d88601c0 | ||
|
|
d467236f8f | ||
| 24d574ae71 | |||
| cf0b39eaac | |||
| 1f767ba18c | |||
|
|
915c424213 | ||
| 117f56ebf7 | |||
|
|
ceed8942fc | ||
| d1e25e7ead | |||
|
|
7e24c0c1cf | ||
|
|
ca251ba111 | ||
|
|
116855572b | ||
|
|
ee977acfef | ||
|
|
3fa7c26946 | ||
|
|
88457b812e | ||
|
|
78df154a4d | ||
|
|
c7eed618c2 | ||
|
|
36bcd3bd82 | ||
|
|
f4118dc357 | ||
| 58598d0fe0 | |||
| 6248f05f74 | |||
|
|
aa9a0a8d23 | ||
|
|
1e40c86dfe | ||
|
|
9696efde02 | ||
|
|
b3fc8be24e | ||
|
|
fc5339a368 | ||
|
|
964b7dee3f | ||
|
|
158949d0d0 | ||
|
|
eed8a0bf50 | ||
|
|
c691d116ab | ||
|
|
cbe8b7dd09 | ||
|
|
203486fd31 | ||
|
|
d9b3b17582 | ||
|
|
e080ecb43a | ||
|
|
ea70711dd3 | ||
| 6dc942bc19 | |||
|
|
d76baf59de | ||
| 79ad1e6fe3 | |||
| 2764de9a1a | |||
|
|
d1d6eada98 | ||
|
|
4a03183481 | ||
|
|
8ea84ac3eb | ||
|
|
4401c12e6c | ||
| c706afcf04 | |||
|
|
ca1d0b94c3 | ||
| 67acd9288b | |||
| 0bf6c9fc08 | |||
| 99807a680c | |||
| f0f7f860d6 | |||
|
|
ef537270ad | ||
|
|
c7f075d157 | ||
|
|
812ea78604 | ||
|
|
0755084a59 | ||
|
|
fe7f5a4134 | ||
|
|
19c454ec4b | ||
|
|
0e9b4c26a4 | ||
|
|
d40b13a045 | ||
|
|
4079b22c1e | ||
|
|
fdfb2bc4c1 | ||
|
|
dbeb7cfe9c | ||
|
|
512df2628f | ||
|
|
92571db693 | ||
|
|
16552620e0 | ||
|
|
5414195dc3 | ||
|
|
0591760932 | ||
|
|
48b9f3f5e9 | ||
|
|
e46278a766 | ||
|
|
2925b1615c | ||
|
|
fc4191c647 | ||
|
|
111126c780 | ||
|
|
54371bba6a | ||
|
|
c28737e88e | ||
|
|
c7d922fac2 | ||
|
|
f8e696bd30 | ||
|
|
6aef28dad2 | ||
| 7105e4099c | |||
|
|
f0762bbb6b | ||
|
|
243c6a4246 | ||
|
|
9983aea928 | ||
|
|
aa58a9749b | ||
|
|
d8110b70a3 | ||
| a44dc90d45 | |||
|
|
12181bd441 | ||
|
|
8d44f7226f | ||
|
|
bf747a86f4 | ||
|
|
4333f00a43 | ||
| 7ab3a31ac4 | |||
|
|
a1d5d6831f | ||
|
|
1b9cabd654 | ||
|
|
d621548120 | ||
|
|
449bcb46fe | ||
|
|
079102ea59 | ||
|
|
a9d371e727 | ||
|
|
27efbc8779 | ||
| efb59d9709 | |||
|
|
003f00b483 | ||
|
|
0be22c98c6 | ||
|
|
bafedf1aad | ||
|
|
98d55545fd | ||
|
|
5f6271b044 | ||
|
|
87753ad289 | ||
|
|
ffb9da0230 | ||
|
|
50ac642666 | ||
|
|
f6fcfcb8fc | ||
|
|
21e0932339 | ||
| f23638c036 | |||
|
|
2b1e0a6fd6 | ||
|
|
0889b814bb | ||
|
|
a648c0d99c | ||
|
|
a291af59c4 | ||
|
|
d9f12731e1 | ||
|
|
4a838a8210 | ||
|
|
5969cc358e | ||
|
|
9f1a7e1139 | ||
|
|
dc257f5066 | ||
|
|
49b86c56e3 | ||
|
|
1be8258721 | ||
|
|
b2980aecb7 | ||
|
|
de3c3b27b2 | ||
|
|
a09eea8d4d | ||
|
|
62c067adcd | ||
|
|
59f6ca5564 | ||
|
|
895aa896bc | ||
|
|
74762edc42 | ||
|
|
101017a29c | ||
|
|
c725b4c797 | ||
|
|
1108cc5e91 | ||
|
|
6a9f5fac61 | ||
| e6a34bcbe7 | |||
|
|
1d00f2f771 | ||
|
|
4a6570a772 | ||
|
|
a997a86e49 | ||
|
|
2d28ff72d7 | ||
|
|
2b1844971c | ||
|
|
49cc022ca2 | ||
|
|
8d2dc8a822 | ||
|
|
eeb6944ce5 | ||
|
|
add78f2967 | ||
|
|
bd6a5af2b5 | ||
|
|
ae08e9c106 | ||
|
|
77c2a021da | ||
|
|
1c19678d04 | ||
|
|
6d636b7ab3 | ||
|
|
009c598049 | ||
|
|
5cceb00df2 | ||
|
|
3e24276eb1 | ||
|
|
29c1076950 | ||
|
|
8dbacb34f8 | ||
|
|
8b306780ee | ||
|
|
9f7d374691 | ||
|
|
a18e53f028 | ||
|
|
2208839027 | ||
|
|
7a17a221ff | ||
|
|
8e1ff80b9e | ||
|
|
94bb0f4c08 | ||
|
|
dbe209ebe4 | ||
|
|
fa0d884cfe | ||
|
|
ed2bd68d28 | ||
|
|
97928e88f8 | ||
|
|
04cf86070c | ||
|
|
9df19e826e | ||
|
|
3f3c1919f4 | ||
|
|
d918694346 | ||
|
|
54fb61bba4 | ||
|
|
fac75866d9 | ||
|
|
a47ff65529 | ||
|
|
093bcedfe7 | ||
|
|
7b6d560bec | ||
|
|
43aa4e23bd | ||
|
|
bdd9ec560b | ||
|
|
0c03bf064b | ||
|
|
9a8c1b7ab8 | ||
|
|
e1ca40c1fc | ||
|
|
e75b99f89c | ||
|
|
ee7304a795 | ||
|
|
a82fd19209 | ||
|
|
1983d607f3 | ||
|
|
10093a0ea2 | ||
|
|
fc08a9146c | ||
|
|
cafd280718 | ||
|
|
11b104677a | ||
|
|
105596a0e5 | ||
|
|
0a37767127 | ||
|
|
31e195bac7 | ||
|
|
bdf1d20f4e | ||
|
|
8d6f82707a | ||
|
|
058fd8adbf | ||
|
|
13d1d2fa08 | ||
|
|
e666d0b807 | ||
|
|
39d7938405 | ||
|
|
65df711b01 | ||
|
|
fd6eb23307 | ||
|
|
fb3927fb8c | ||
|
|
891af703be | ||
|
|
474472eedd | ||
|
|
23d65145e6 | ||
|
|
9ea4919b9b | ||
|
|
f78e30770e | ||
|
|
bf9f319cdf | ||
|
|
ad28b72dd3 | ||
|
|
8425ae77f8 | ||
|
|
d7b9b2713b | ||
|
|
be788415ad | ||
|
|
a03791c581 | ||
|
|
ee922a3da6 | ||
|
|
624f1c1980 | ||
|
|
607a226e34 | ||
|
|
bac1bbfd97 | ||
|
|
7f9b3b5556 | ||
|
|
ba12513199 | ||
|
|
8fcfbc0d20 | ||
|
|
472186c1be | ||
|
|
60c05bd899 | ||
|
|
793e6013e5 | ||
|
|
071ab7aede | ||
|
|
eda8b00f84 | ||
|
|
eac2ab3c28 | ||
|
|
76fba34c2f | ||
|
|
491a42d352 | ||
|
|
5e85194a13 | ||
|
|
689ae7cfc7 | ||
|
|
19dbd77402 | ||
|
|
b194b3adc9 | ||
|
|
33a9b3bc17 | ||
|
|
6562154573 | ||
|
|
b32ebddf85 | ||
|
|
b3e3dac975 | ||
|
|
f20e4daa60 | ||
|
|
f67d87e99d | ||
|
|
36928b716c | ||
|
|
7c7b0ced5f | ||
|
|
c67ef7e017 | ||
|
|
dcd925f1e5 | ||
|
|
0ea56a5ffe | ||
|
|
821fda41ae | ||
|
|
1d311ab457 | ||
|
|
b699d969e4 | ||
|
|
994d371ff1 | ||
|
|
a91dad04ee | ||
|
|
b4261e8cf9 | ||
|
|
efcac3d009 | ||
|
|
770c7686ba | ||
|
|
11904e1137 | ||
|
|
1e009e52dd | ||
|
|
bf42c028fb | ||
|
|
0a4bd02503 | ||
|
|
63edfaa852 | ||
|
|
802cc8239a | ||
|
|
75b1a62af3 | ||
|
|
50b20413d3 | ||
|
|
fa5b3ee9d9 | ||
|
|
f50a50eeb3 | ||
|
|
e1e6199743 | ||
|
|
61dd2b8489 | ||
|
|
6ca298c61d | ||
| f4fb923fb2 | |||
|
|
32a2005f6d | ||
|
|
37d1139a57 | ||
|
|
a90a74c9e2 | ||
|
|
78aed5beed | ||
|
|
59fccb82ec | ||
|
|
048065fe96 | ||
|
|
0b8ff3a8bb | ||
|
|
1892bd05a5 | ||
|
|
be6e8a7c78 | ||
|
|
df9055f69c | ||
|
|
649dd235c3 | ||
|
|
1af82df8b1 | ||
|
|
7098e59b5c | ||
|
|
31362bc331 | ||
|
|
4e2339749c | ||
|
|
9cecf2e097 | ||
|
|
fe9c68238f | ||
|
|
225b17559b | ||
|
|
e697912ee5 | ||
|
|
6358b9277d | ||
|
|
45c986c5f1 | ||
|
|
fa01ff6604 | ||
|
|
fe1e018e8e | ||
|
|
60ea537bbc | ||
|
|
488dc31743 | ||
|
|
b6915f0898 | ||
|
|
04dfe4e867 | ||
|
|
d8fe030a4b | ||
|
|
f40d4578d5 | ||
|
|
e0078bbcd5 | ||
|
|
c145f355dd | ||
| 943445270f | |||
|
|
61cde4a9f4 | ||
|
|
22aa7d14b3 | ||
|
|
a09b6729cc | ||
|
|
2fe64001c0 | ||
|
|
14c9c412cd | ||
|
|
361fdfba04 | ||
|
|
5c8d1ae2b9 | ||
|
|
81e20160f5 | ||
|
|
ef95b28e3d | ||
|
|
1781542964 | ||
|
|
45208992b5 | ||
|
|
847a01df82 | ||
|
|
ce33e3b072 | ||
|
|
ae12fd1021 | ||
|
|
95ae2688a2 | ||
|
|
2bcfb85613 | ||
|
|
d52a111735 | ||
|
|
d4bec24eb7 | ||
|
|
2338e7c9d2 | ||
|
|
f0e841595c | ||
|
|
a82af19d43 | ||
|
|
5d7254e79a | ||
|
|
7c21a1b92a | ||
|
|
587f64a87a | ||
|
|
cb9c4c3aef | ||
|
|
dda96cb87e | ||
|
|
ebae497a72 | ||
|
|
515014fbeb | ||
|
|
e9efcbe8dc | ||
|
|
5a52593e66 | ||
|
|
c61e12d5ee | ||
|
|
c2d59c1f4d | ||
|
|
812fe9e640 | ||
|
|
f95bccce84 | ||
|
|
81e7edd666 | ||
|
|
46fd205eda | ||
|
|
c2b307e5bb | ||
|
|
b7ac62f7d2 | ||
|
|
50d5c6402b | ||
|
|
d2a3fd0b04 | ||
|
|
51f4bc6d56 | ||
|
|
c3607c23e7 | ||
|
|
34b1c403bb | ||
|
|
91e057440d | ||
|
|
53ca742c66 | ||
|
|
b35dfb1086 | ||
|
|
d502e2f58a | ||
|
|
bc30efcf70 | ||
|
|
0415ead504 | ||
|
|
e95f44d3f8 | ||
|
|
0489ae91e9 | ||
|
|
4e02f444fd | ||
|
|
6027a81f06 | ||
|
|
01f0e70213 | ||
|
|
cb15fadcee | ||
|
|
1c8d15fe4b | ||
|
|
55f5937c8b | ||
|
|
56619f2745 | ||
|
|
0b59e2ce3d | ||
|
|
1ea6390eae | ||
|
|
303adca500 | ||
|
|
03700ae6c0 | ||
|
|
a1ddfa827e | ||
|
|
59751c02e6 | ||
|
|
e8e112144f | ||
|
|
59246e0412 | ||
|
|
0131e9468f | ||
|
|
d5951f1d7c | ||
|
|
f33b562c16 | ||
|
|
572fe58314 | ||
|
|
5602b93d7a | ||
|
|
64e438a8d4 | ||
|
|
b0b6b8fce2 | ||
|
|
417a05db60 | ||
|
|
11e42aac69 | ||
|
|
885ba8f905 | ||
|
|
caa74d1b5f | ||
|
|
f6b4a9da1c | ||
|
|
74c5102e41 | ||
|
|
7bd50cd251 | ||
|
|
df728aaddd | ||
|
|
ae934c19f1 | ||
|
|
e260cc4a24 | ||
|
|
631faff7b8 | ||
|
|
fd531349d7 | ||
|
|
27bab29e3c | ||
|
|
27af221fd2 | ||
|
|
6e28e7a86f | ||
|
|
ff69d46c98 | ||
|
|
e966944ae5 | ||
|
|
8da77a3ddc | ||
|
|
8289dbabc4 | ||
|
|
d50f30d743 | ||
|
|
f8533551a4 | ||
|
|
7e46ff5d92 | ||
|
|
6b1eef5354 | ||
|
|
b29da80539 | ||
|
|
a725ab2c94 | ||
|
|
1582aa1572 | ||
|
|
5352d53346 | ||
|
|
7d884eff9d | ||
|
|
c9af88184b | ||
|
|
f25ad35f0a | ||
|
|
55ab44c8be | ||
|
|
59c1680594 | ||
|
|
b804303aa0 | ||
|
|
55a15ecf12 | ||
|
|
caa1bcf9fe | ||
|
|
e2b2a30668 | ||
|
|
a9be1288d2 | ||
|
|
679214e598 | ||
|
|
6064e1426c | ||
|
|
d28a868e46 | ||
|
|
398acc67ca | ||
|
|
ce578800d0 | ||
|
|
6307d6ba51 | ||
|
|
283c85d256 | ||
|
|
9bd0fb9125 | ||
|
|
c445aed6b1 | ||
|
|
1d8c66780e | ||
|
|
219efd27e9 | ||
|
|
ac9001ac74 | ||
|
|
2896614595 | ||
|
|
bca12a7003 | ||
| 1c5a4c470f | |||
|
|
744b19d625 | ||
|
|
5865e89bed | ||
|
|
3a10b1cdde | ||
|
|
5db7514a91 | ||
| a957e90ca8 | |||
|
|
18cf025056 | ||
|
|
b55b7d6b20 | ||
|
|
26b5d1994a | ||
|
|
30aec5b872 | ||
|
|
b0626089f3 | ||
|
|
af3d4e595f | ||
|
|
40c09eed1c | ||
|
|
fe46e7a9e9 | ||
|
|
78647c7027 | ||
|
|
26755f86b1 | ||
|
|
898848795b | ||
|
|
24efbe6a41 | ||
|
|
97433f716f | ||
|
|
5200febaea | ||
| a1eaf9cc20 | |||
|
|
3f8af8c1e0 | ||
|
|
becaeefcba | ||
| 7d5bdcf993 | |||
|
|
81a7cf458c | ||
|
|
a4ea61334b | ||
|
|
3fbba9f83d | ||
|
|
4a11a4c546 | ||
|
|
265233517e | ||
| c581ceb1dc | |||
|
|
bb1a1358b7 | ||
|
|
29fb58db39 | ||
|
|
b6f0164501 | ||
|
|
2c0801fc1c | ||
|
|
34c92f4964 | ||
|
|
f9b900b2ca | ||
|
|
b5d65305db | ||
| b8e96f45d4 | |||
| 90f9b0d0c9 | |||
|
|
972c0989af | ||
|
|
5f04fd58ab | ||
|
|
da04aa8ae8 | ||
|
|
62bf0df864 | ||
|
|
91e9c0cb62 | ||
|
|
3356b83f24 | ||
|
|
795ec509fd | ||
|
|
93d66afe8c | ||
|
|
c840cee404 | ||
|
|
e48cce6485 | ||
|
|
b2ecd93404 | ||
|
|
2928c66624 | ||
|
|
4613a820ca | ||
|
|
ecdadef633 | ||
|
|
7f1dea72f2 | ||
|
|
612f872f76 | ||
|
|
13d2a9ec7a | ||
|
|
9fab47ecdb | ||
|
|
265e8ade05 | ||
|
|
60e0e81523 | ||
|
|
b01357058a | ||
|
|
7deafbc5ce | ||
|
|
9e177be560 | ||
|
|
ee9776e7b2 | ||
|
|
3f7f2afc7b | ||
|
|
4e965e4ce5 | ||
|
|
6f1c30aef5 | ||
|
|
00bbb3ac61 | ||
|
|
ce1942c578 | ||
|
|
df7169c9f2 | ||
|
|
5e59db4c6d | ||
|
|
25b3fda25b | ||
|
|
9678daeafa | ||
|
|
9ed257f151 | ||
|
|
107124e5dc | ||
|
|
ce4acfa892 | ||
|
|
6bf4828296 | ||
|
|
76b4e78a6a | ||
|
|
8b6475b8d4 | ||
|
|
c3ed83dfba | ||
|
|
114bc1e18b | ||
|
|
af94899b54 | ||
|
|
266b6dbc64 | ||
|
|
5932dd753c | ||
| 86a6328254 | |||
|
|
0d33c029a9 | ||
|
|
8ecd2381a4 | ||
|
|
7318807dce | ||
|
|
811275be26 | ||
|
|
3f3fd38601 | ||
|
|
44dd0b1302 | ||
|
|
6475c1f3ad | ||
|
|
deabf0b8c9 | ||
|
|
04ee4b04ad | ||
|
|
2892686c5f | ||
|
|
8ee31a63f1 | ||
|
|
4e363da91f | ||
|
|
8b63df7a98 | ||
|
|
b06854b0d5 | ||
|
|
39bf71376a | ||
|
|
12d9c5b187 | ||
|
|
d2eba3f8f9 | ||
|
|
19f2f8b161 | ||
|
|
d41185eb84 | ||
|
|
c420fa2dec | ||
|
|
db03a564fb | ||
| 9763820c75 | |||
|
|
9095b99f6b | ||
|
|
080363e8c4 | ||
|
|
252667398e | ||
|
|
f82c267d81 | ||
|
|
61fe552ac4 | ||
|
|
7013e7467f | ||
|
|
dbc537007d | ||
|
|
95045be83d | ||
|
|
52ccd900c7 | ||
|
|
64a251d69a | ||
|
|
cae4148594 | ||
|
|
38e29c5101 | ||
|
|
8dc3fb964e | ||
|
|
212144d658 | ||
|
|
11d81221cc | ||
|
|
55252cbc32 | ||
|
|
c87a58db0a | ||
| 49d73faa5f | |||
| da6c1be607 | |||
|
|
94d409b180 | ||
|
|
d6e97c5970 | ||
|
|
64d5a528ca | ||
|
|
538d3752f9 | ||
|
|
fb5b358ae2 | ||
|
|
6a0082741c | ||
|
|
5744050943 | ||
|
|
168cc06827 | ||
|
|
fa01cadc35 | ||
|
|
342c29de7d | ||
|
|
eeed493766 | ||
|
|
90d7a87914 | ||
|
|
a1c6cdf193 | ||
|
|
bec13a45cd | ||
|
|
4107733453 | ||
|
|
97c1300f53 | ||
|
|
0af8be35bb | ||
|
|
a91b3f3e8b | ||
|
|
383658edf2 | ||
|
|
16754a7477 | ||
|
|
58b25d7241 | ||
|
|
1c7d44282e | ||
|
|
8e9eef794f | ||
|
|
920c026f14 | ||
|
|
946c76cb03 | ||
|
|
43d11a9b8d | ||
|
|
9f481542f3 | ||
|
|
cffa5b6b50 | ||
|
|
8867539d78 | ||
|
|
671408b3a5 | ||
|
|
bdb62e8ed1 | ||
|
|
72522a869a | ||
|
|
fd5c29addc | ||
|
|
65b1283459 | ||
|
|
5ffe367cae | ||
|
|
5ae3e179b9 | ||
| c696a859be | |||
|
|
174b01ca29 | ||
|
|
c433de80cd | ||
|
|
f2b4c07a00 | ||
|
|
929ffdcc42 | ||
|
|
5aa28dfb0d | ||
|
|
a9e8fc6039 | ||
|
|
0b1e6d7eaf | ||
|
|
1ffa289d39 | ||
|
|
68419cc024 | ||
| 8227206208 | |||
|
|
6f28852e1b | ||
|
|
0e3550229b | ||
|
|
f9400ba713 | ||
|
|
ce080d76c6 | ||
|
|
254045e9f3 | ||
|
|
b84134581c | ||
|
|
f67c5e779f | ||
|
|
4a694c9d02 | ||
|
|
24b8d2a315 | ||
|
|
2f3c251b00 | ||
|
|
f933457cc1 |
24
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
24
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
---
|
||||||
|
name: Bug report
|
||||||
|
about: For reporting bugs in go-micro
|
||||||
|
title: "[BUG]"
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Describe the bug**
|
||||||
|
|
||||||
|
1. What are you trying to do?
|
||||||
|
2. What did you expect to happen?
|
||||||
|
3. What happens instead?
|
||||||
|
|
||||||
|
**How to reproduce the bug:**
|
||||||
|
|
||||||
|
If possible, please include a minimal code snippet here.
|
||||||
|
|
||||||
|
**Environment:**
|
||||||
|
Go Version: please paste `go version` output here
|
||||||
|
```
|
||||||
|
please paste `go env` output here
|
||||||
|
```
|
||||||
17
.github/ISSUE_TEMPLATE/feature-request---enhancement.md
vendored
Normal file
17
.github/ISSUE_TEMPLATE/feature-request---enhancement.md
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
---
|
||||||
|
name: Feature request / Enhancement
|
||||||
|
about: If you have a need not served by go-micro
|
||||||
|
title: "[FEATURE]"
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Is your feature request related to a problem? Please describe.**
|
||||||
|
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||||
|
|
||||||
|
**Describe the solution you'd like**
|
||||||
|
A clear and concise description of what you want to happen.
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context or screenshots about the feature request here.
|
||||||
14
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
14
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
---
|
||||||
|
name: Question
|
||||||
|
about: Ask a question about go-micro
|
||||||
|
title: ''
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Before asking, please check if your question has already been answered:
|
||||||
|
|
||||||
|
1. Check the documentation - https://micro.mu/docs/
|
||||||
|
2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins
|
||||||
|
3. Search existing issues
|
||||||
9
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
9
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
## Pull Request template
|
||||||
|
Please, go through these steps before clicking submit on this PR.
|
||||||
|
|
||||||
|
1. Give a descriptive title to your PR.
|
||||||
|
2. Provide a description of your changes.
|
||||||
|
3. Make sure you have some relevant tests.
|
||||||
|
4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable).
|
||||||
|
|
||||||
|
**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING**
|
||||||
15
.github/generate.sh
vendored
Executable file
15
.github/generate.sh
vendored
Executable file
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/bash -e
|
||||||
|
|
||||||
|
find . -type f -name '*.pb.*.go' -o -name '*.pb.go' -a ! -name 'message.pb.go' -delete
|
||||||
|
PROTOS=$(find . -type f -name '*.proto' | grep -v proto/google/api)
|
||||||
|
|
||||||
|
mkdir -p proto/google/api
|
||||||
|
curl -s -o proto/google/api/annotations.proto -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto
|
||||||
|
curl -s -o proto/google/api/http.proto -L https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto
|
||||||
|
|
||||||
|
for PROTO in $PROTOS; do
|
||||||
|
echo $PROTO
|
||||||
|
protoc -I./proto -I. -I$(dirname $PROTO) --go-grpc_out=paths=source_relative:. --go_out=paths=source_relative:. --micro_out=paths=source_relative:. $PROTO
|
||||||
|
done
|
||||||
|
|
||||||
|
rm -r proto
|
||||||
13
.github/stale.sh
vendored
Executable file
13
.github/stale.sh
vendored
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/bash -ex
|
||||||
|
|
||||||
|
export PATH=$PATH:$(pwd)/bin
|
||||||
|
export GO111MODULE=on
|
||||||
|
export GOBIN=$(pwd)/bin
|
||||||
|
|
||||||
|
#go get github.com/rvflash/goup@v0.4.1
|
||||||
|
|
||||||
|
#goup -v ./...
|
||||||
|
#go get github.com/psampaz/go-mod-outdated@v0.6.0
|
||||||
|
go list -u -m -mod=mod -json all | go-mod-outdated -update -direct -ci || true
|
||||||
|
|
||||||
|
#go list -u -m -json all | go-mod-outdated -update
|
||||||
62
.github/workflows/build.yml
vendored
Normal file
62
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
name: build
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: setup
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.15
|
||||||
|
- name: cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: ${{ runner.os }}-go-
|
||||||
|
- name: sdk checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: sdk deps
|
||||||
|
run: go get -v -t -d ./...
|
||||||
|
- name: sdk test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
||||||
|
- name: tests checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
repository: unistack-org/micro-tests
|
||||||
|
ref: refs/heads/master
|
||||||
|
path: micro-tests
|
||||||
|
fetch-depth: 1
|
||||||
|
- name: tests deps
|
||||||
|
run: |
|
||||||
|
cd micro-tests
|
||||||
|
go mod edit -replace="github.com/unistack-org/micro/v3=../"
|
||||||
|
go get -v -t -d ./...
|
||||||
|
- name: tests test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: cd micro-tests && go test -mod readonly -v ./...
|
||||||
|
lint:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: lint
|
||||||
|
uses: golangci/golangci-lint-action@v1
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.30
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
||||||
62
.github/workflows/pr.yml
vendored
Normal file
62
.github/workflows/pr.yml
vendored
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
name: prbuild
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: setup
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.15
|
||||||
|
- name: cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: ${{ runner.os }}-go-
|
||||||
|
- name: sdk checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: sdk deps
|
||||||
|
run: go get -v -t -d ./...
|
||||||
|
- name: sdk test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: go test -mod readonly -v ./...
|
||||||
|
- name: tests checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
repository: unistack-org/micro-tests
|
||||||
|
ref: refs/heads/master
|
||||||
|
path: micro-tests
|
||||||
|
fetch-depth: 1
|
||||||
|
- name: tests deps
|
||||||
|
run: |
|
||||||
|
cd micro-tests
|
||||||
|
go mod edit -replace="github.com/unistack-org/micro/v3=../"
|
||||||
|
go get -v -t -d ./...
|
||||||
|
- name: tests test
|
||||||
|
env:
|
||||||
|
INTEGRATION_TESTS: yes
|
||||||
|
run: cd micro-tests && go test -mod readonly -v ./...
|
||||||
|
lint:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: lint
|
||||||
|
uses: golangci/golangci-lint-action@v1
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.30
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
||||||
40
.golangci.yml
Normal file
40
.golangci.yml
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
run:
|
||||||
|
deadline: 5m
|
||||||
|
modules-download-mode: readonly
|
||||||
|
skip-dirs:
|
||||||
|
- util/mdns.new
|
||||||
|
skip-files:
|
||||||
|
- ".*\\.pb\\.go$"
|
||||||
|
- ".*\\.pb\\.micro\\.go$"
|
||||||
|
linters:
|
||||||
|
disable-all: false
|
||||||
|
enable-all: false
|
||||||
|
enable:
|
||||||
|
- megacheck
|
||||||
|
- staticcheck
|
||||||
|
- deadcode
|
||||||
|
- varcheck
|
||||||
|
- gosimple
|
||||||
|
- unused
|
||||||
|
- prealloc
|
||||||
|
- scopelint
|
||||||
|
- gocritic
|
||||||
|
- goimports
|
||||||
|
- unconvert
|
||||||
|
- govet
|
||||||
|
- nakedret
|
||||||
|
- structcheck
|
||||||
|
- gosec
|
||||||
|
disable:
|
||||||
|
- maligned
|
||||||
|
- interfacer
|
||||||
|
- typecheck
|
||||||
|
- dupl
|
||||||
|
output:
|
||||||
|
format: colored-line-number
|
||||||
|
# print lines of code with issue, default is true
|
||||||
|
print-issued-lines: true
|
||||||
|
# print linter name in the end of issue text, default is true
|
||||||
|
print-linter-name: true
|
||||||
|
# make issues output unique by line, default is true
|
||||||
|
uniq-by-line: true
|
||||||
11
.travis.yml
11
.travis.yml
@@ -1,11 +0,0 @@
|
|||||||
language: go
|
|
||||||
go:
|
|
||||||
- 1.13.x
|
|
||||||
env:
|
|
||||||
- GO111MODULE=on IN_TRAVIS_CI=yes
|
|
||||||
notifications:
|
|
||||||
slack:
|
|
||||||
secure: aEvhLbhujaGaKSrOokiG3//PaVHTIrc3fBpoRbCRqfZpyq6WREoapJJhF+tIpWWOwaC9GmChbD6aHo/jMUgwKXVyPSaNjiEL87YzUUpL8B2zslNp1rgfTg/LrzthOx3Q1TYwpaAl3to0fuHUVFX4yMeC2vuThq7WSXgMMxFCtbc=
|
|
||||||
cache:
|
|
||||||
directories:
|
|
||||||
- $GOPATH/pkg/mod
|
|
||||||
3
LICENSE
3
LICENSE
@@ -176,7 +176,8 @@
|
|||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
Copyright 2015 Asim Aslam.
|
Copyright 2015-2020 Asim Aslam.
|
||||||
|
Copyright 2019-2020 Unistack LLC.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
|||||||
55
README.md
55
README.md
@@ -1,27 +1,27 @@
|
|||||||
# Go Micro [](https://opensource.org/licenses/Apache-2.0) [](https://godoc.org/github.com/micro/go-micro) [](https://travis-ci.org/micro/go-micro) [](https://goreportcard.com/report/github.com/micro/go-micro)
|
# Micro [](https://opensource.org/licenses/Apache-2.0) [](https://pkg.go.dev/github.com/unistack-org/micro/v3?tab=overview) [](https://github.com/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Amaster+event%3Apush) [](https://goreportcard.com/report/github.com/unistack-org/micro) [](https://unistack-org.slack.com/messages/default)
|
||||||
|
|
||||||
Go Micro is a framework for microservice development.
|
Micro is a standard library for microservices.
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
Go Micro provides the core requirements for distributed systems development including RPC and Event driven communication.
|
Micro provides the core requirements for distributed systems development including RPC and Event driven communication.
|
||||||
The **micro** philosophy is sane defaults with a pluggable architecture. We provide defaults to get you started quickly
|
|
||||||
but everything can be easily swapped out.
|
|
||||||
|
|
||||||
<img src="https://micro.mu/docs/images/go-micro.svg" />
|
|
||||||
|
|
||||||
Plugins are available at [github.com/micro/go-plugins](https://github.com/micro/go-plugins).
|
|
||||||
|
|
||||||
Follow us on [Twitter](https://twitter.com/microhq) or join the [Slack](http://slack.micro.mu/) community.
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
Go Micro abstracts away the details of distributed systems. Here are the main features.
|
Micro abstracts away the details of distributed systems. Here are the main features.
|
||||||
|
|
||||||
|
- **Authentication** - Auth is built in as a first class citizen. Authentication and authorization enable secure
|
||||||
|
zero trust networking by providing every service an identity and certificates. This additionally includes rule
|
||||||
|
based access control.
|
||||||
|
|
||||||
|
- **Dynamic Config** - Load and hot reload dynamic config from anywhere. The config interface provides a way to load application
|
||||||
|
level config from any source such as env vars, file, etcd. You can merge the sources and even define fallbacks.
|
||||||
|
|
||||||
|
- **Data Storage** - A simple data store interface to read, write and delete records. It includes support for memory, file and
|
||||||
|
CockroachDB by default. State and persistence becomes a core requirement beyond prototyping and Micro looks to build that into the framework.
|
||||||
|
|
||||||
- **Service Discovery** - Automatic service registration and name resolution. Service discovery is at the core of micro service
|
- **Service Discovery** - Automatic service registration and name resolution. Service discovery is at the core of micro service
|
||||||
development. When service A needs to speak to service B it needs the location of that service. The default discovery mechanism is
|
development. When service A needs to speak to service B it needs the location of that service.
|
||||||
multicast DNS (mdns), a zeroconf system. You can optionally set gossip using the SWIM protocol for p2p networks or consul for a
|
|
||||||
resilient cloud-native setup.
|
|
||||||
|
|
||||||
- **Load Balancing** - Client side load balancing built on service discovery. Once we have the addresses of any number of instances
|
- **Load Balancing** - Client side load balancing built on service discovery. Once we have the addresses of any number of instances
|
||||||
of a service we now need a way to decide which node to route to. We use random hashed load balancing to provide even distribution
|
of a service we now need a way to decide which node to route to. We use random hashed load balancing to provide even distribution
|
||||||
@@ -29,21 +29,24 @@ across the services and retry a different node if there's a problem.
|
|||||||
|
|
||||||
- **Message Encoding** - Dynamic message encoding based on content-type. The client and server will use codecs along with content-type
|
- **Message Encoding** - Dynamic message encoding based on content-type. The client and server will use codecs along with content-type
|
||||||
to seamlessly encode and decode Go types for you. Any variety of messages could be encoded and sent from different clients. The client
|
to seamlessly encode and decode Go types for you. Any variety of messages could be encoded and sent from different clients. The client
|
||||||
and server handle this by default. This includes protobuf and json by default.
|
and server handle this by default.
|
||||||
|
|
||||||
- **Request/Response** - RPC based request/response with support for bidirectional streaming. We provide an abstraction for synchronous
|
- **Transport** - gRPC or http based request/response with support for bidirectional streaming. We provide an abstraction for synchronous communication. A request made to a service will be automatically resolved, load balanced, dialled and streamed.
|
||||||
communication. A request made to a service will be automatically resolved, load balanced, dialled and streamed. The default
|
|
||||||
transport is http/1.1 or http2 when tls is enabled.
|
|
||||||
|
|
||||||
- **Async Messaging** - PubSub is built in as a first class citizen for asynchronous communication and event driven architectures.
|
- **Async Messaging** - PubSub is built in as a first class citizen for asynchronous communication and event driven architectures.
|
||||||
Event notifications are a core pattern in micro service development. The default messaging is point-to-point http/1.1 or http2 when tls
|
Event notifications are a core pattern in micro service development.
|
||||||
is enabled.
|
|
||||||
|
|
||||||
- **Pluggable Interfaces** - Go Micro makes use of Go interfaces for each distributed system abstraction. Because of this these interfaces
|
- **Synchronization** - Distributed systems are often built in an eventually consistent manner. Support for distributed locking and
|
||||||
are pluggable and allows Go Micro to be runtime agnostic. You can plugin any underlying technology. Find plugins in
|
leadership are built in as a Sync interface. When using an eventually consistent database or scheduling use the Sync interface.
|
||||||
[github.com/micro/go-plugins](https://github.com/micro/go-plugins).
|
|
||||||
|
- **Pluggable Interfaces** - Micro makes use of Go interfaces for each system abstraction. Because of this these interfaces
|
||||||
|
are pluggable and allows Micro to be runtime agnostic.
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
See the [docs](https://micro.mu/docs/go-micro.html) for detailed information on the architecture, installation and use of go-micro.
|
To be created.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Micro is Apache 2.0 licensed.
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
# Go Micro [](https://opensource.org/licenses/Apache-2.0) [](https://godoc.org/github.com/micro/go-micro) [](https://travis-ci.org/micro/go-micro) [](https://goreportcard.com/report/github.com/micro/go-micro)
|
|
||||||
|
|
||||||
Go Micro是基于Golang的微服务开发框架。
|
|
||||||
|
|
||||||
## 概览
|
|
||||||
|
|
||||||
Go Micro提供分布式系统开发的核心库,包含RPC与事件驱动的通信机制。
|
|
||||||
|
|
||||||
**micro**的设计哲学是可插拔的架构理念,她提供可快速构建系统的组件,并且可以根据自身的需求剥离默认实现并自行定制。
|
|
||||||
|
|
||||||
<img src="https://micro.mu/docs/images/go-micro.svg" />
|
|
||||||
|
|
||||||
所有插件可在仓库[github.com/micro/go-plugins](https://github.com/micro/go-plugins)中找到。
|
|
||||||
|
|
||||||
可以订阅我们的[Twitter](https://twitter.com/microhq)或者加入[Slack](http://slack.micro.mu/)论坛。
|
|
||||||
|
|
||||||
## 特性
|
|
||||||
|
|
||||||
Go Micro把分布式系统的各种细节抽象出来。下面是它的主要特性。
|
|
||||||
|
|
||||||
- **服务发现(Service Discovery)** - 自动服务注册与名称解析。服务发现是微服务开发中的核心。当服务A要与服务B协作时,它得知道B在哪里。默认的服务发现系统是Consul,而multicast DNS (mdns,组播)机制作为本地解决方案,或者零依赖的P2P网络中的SWIM协议(gossip)。
|
|
||||||
|
|
||||||
- **负载均衡(Load Balancing)** - 在服务发现之上构建了负载均衡机制。当我们得到一个服务的任意多个的实例节点时,我们要一个机制去决定要路由到哪一个节点。我们使用随机处理过的哈希负载均衡机制来保证对服务请求颁发的均匀分布,并且在发生问题时进行重试。
|
|
||||||
|
|
||||||
- **消息编码(Message Encoding)** - 支持基于内容类型(content-type)动态编码消息。客户端和服务端会一起使用content-type的格式来对Go进行无缝编/解码。各种各样的消息被编码会发送到不同的客户端,客户端服服务端默认会处理这些消息。content-type默认包含proto-rpc和json-rpc。
|
|
||||||
|
|
||||||
- **Request/Response** - RPC通信基于支持双向流的请求/响应方式,我们提供有抽象的同步通信机制。请求发送到服务时,会自动解析、负载均衡、拨号、转成字节流。默认的传输协议是http/1.1,而tls下使用http2协议。
|
|
||||||
|
|
||||||
- **异步消息(Async Messaging)** - 发布订阅(PubSub)头等功能内置在异步通信与事件驱动架构中。事件通知在微服务开发中处于核心位置。默认的消息传送使用点到点http/1.1,激活tls时则使用http2。
|
|
||||||
|
|
||||||
- **可插拔接口(Pluggable Interfaces)** - Go Micro为每个分布式系统抽象出接口。因此,Go Micro的接口都是可插拔的,允许其在运行时不可知的情况下仍可支持。所以只要实现接口,可以在内部使用任何的技术。更多插件请参考:[github.com/micro/go-plugins](https://github.com/micro/go-plugins)。
|
|
||||||
|
|
||||||
## 快速上手
|
|
||||||
|
|
||||||
更多关于架构、安装的资料可以查看[文档](https://micro.mu/docs/cn/)。
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
// Package agent provides an interface for building robots
|
|
||||||
package agent
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
// Package command is an interface for defining bot commands
|
|
||||||
package command
|
|
||||||
|
|
||||||
var (
|
|
||||||
// Commmands keyed by golang/regexp patterns
|
|
||||||
// regexp.Match(key, input) is used to match
|
|
||||||
Commands = map[string]Command{}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Command is the interface for specific named
|
|
||||||
// commands executed via plugins or the bot.
|
|
||||||
type Command interface {
|
|
||||||
// Executes the command with args passed in
|
|
||||||
Exec(args ...string) ([]byte, error)
|
|
||||||
// Usage of the command
|
|
||||||
Usage() string
|
|
||||||
// Description of the command
|
|
||||||
Description() string
|
|
||||||
// Name of the command
|
|
||||||
String() string
|
|
||||||
}
|
|
||||||
|
|
||||||
type cmd struct {
|
|
||||||
name string
|
|
||||||
usage string
|
|
||||||
description string
|
|
||||||
exec func(args ...string) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cmd) Description() string {
|
|
||||||
return c.description
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cmd) Exec(args ...string) ([]byte, error) {
|
|
||||||
return c.exec(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cmd) Usage() string {
|
|
||||||
return c.usage
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cmd) String() string {
|
|
||||||
return c.name
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewCommand helps quickly create a new command
|
|
||||||
func NewCommand(name, usage, description string, exec func(args ...string) ([]byte, error)) Command {
|
|
||||||
return &cmd{
|
|
||||||
name: name,
|
|
||||||
usage: usage,
|
|
||||||
description: description,
|
|
||||||
exec: exec,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCommand(t *testing.T) {
|
|
||||||
c := &cmd{
|
|
||||||
name: "test",
|
|
||||||
usage: "test usage",
|
|
||||||
description: "test description",
|
|
||||||
exec: func(args ...string) ([]byte, error) {
|
|
||||||
return []byte("test"), nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.String() != c.name {
|
|
||||||
t.Fatalf("expected name %s got %s", c.name, c.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.Usage() != c.usage {
|
|
||||||
t.Fatalf("expected usage %s got %s", c.usage, c.Usage())
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.Description() != c.description {
|
|
||||||
t.Fatalf("expected description %s got %s", c.description, c.Description())
|
|
||||||
}
|
|
||||||
|
|
||||||
if r, err := c.Exec(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
} else if string(r) != "test" {
|
|
||||||
t.Fatalf("expected exec result test got %s", string(r))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewCommand(t *testing.T) {
|
|
||||||
c := &cmd{
|
|
||||||
name: "test",
|
|
||||||
usage: "test usage",
|
|
||||||
description: "test description",
|
|
||||||
exec: func(args ...string) ([]byte, error) {
|
|
||||||
return []byte("test"), nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
nc := NewCommand(c.name, c.usage, c.description, c.exec)
|
|
||||||
|
|
||||||
if nc.String() != c.name {
|
|
||||||
t.Fatalf("expected name %s got %s", c.name, nc.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
if nc.Usage() != c.usage {
|
|
||||||
t.Fatalf("expected usage %s got %s", c.usage, nc.Usage())
|
|
||||||
}
|
|
||||||
|
|
||||||
if nc.Description() != c.description {
|
|
||||||
t.Fatalf("expected description %s got %s", c.description, nc.Description())
|
|
||||||
}
|
|
||||||
|
|
||||||
if r, err := nc.Exec(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
} else if string(r) != "test" {
|
|
||||||
t.Fatalf("expected exec result test got %s", string(r))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# Discord input for micro-bot
|
|
||||||
[Discord](https://discordapp.com) support for micro bot based on [discordgo](github.com/bwmarrin/discordgo).
|
|
||||||
|
|
||||||
This was originally written by Aleksandr Tihomirov (@zet4) and can be found at https://github.com/zet4/micro-misc/.
|
|
||||||
|
|
||||||
## Options
|
|
||||||
### discord_token
|
|
||||||
|
|
||||||
You have to supply an application token via `--discord_token`.
|
|
||||||
|
|
||||||
Head over to Discord's [developer introduction](https://discordapp.com/developers/docs/intro)
|
|
||||||
to learn how to create applications and how the API works.
|
|
||||||
|
|
||||||
### discord_prefix
|
|
||||||
|
|
||||||
Set a command prefix with `--discord_prefix`. The default prefix is `Micro `.
|
|
||||||
You can mention the bot or use the prefix to run a command.
|
|
||||||
|
|
||||||
### discord_whitelist
|
|
||||||
|
|
||||||
Pass a list of comma-separated user IDs with `--discord_whitelist`. Only allow
|
|
||||||
these users to use the bot.
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
package discord
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
"github.com/micro/go-micro/agent/input"
|
|
||||||
"github.com/micro/go-micro/util/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type discordConn struct {
|
|
||||||
master *discordInput
|
|
||||||
exit chan struct{}
|
|
||||||
recv chan *discordgo.Message
|
|
||||||
|
|
||||||
sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func newConn(master *discordInput) *discordConn {
|
|
||||||
conn := &discordConn{
|
|
||||||
master: master,
|
|
||||||
exit: make(chan struct{}),
|
|
||||||
recv: make(chan *discordgo.Message),
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.master.session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
||||||
if m.Author.ID == master.botID {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
whitelisted := false
|
|
||||||
for _, ID := range conn.master.whitelist {
|
|
||||||
if m.Author.ID == ID {
|
|
||||||
whitelisted = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(master.whitelist) > 0 && !whitelisted {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var valid bool
|
|
||||||
m.Message.Content, valid = conn.master.prefixfn(m.Message.Content)
|
|
||||||
if !valid {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.recv <- m.Message
|
|
||||||
})
|
|
||||||
|
|
||||||
return conn
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dc *discordConn) Recv(event *input.Event) error {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-dc.exit:
|
|
||||||
return errors.New("connection closed")
|
|
||||||
case msg := <-dc.recv:
|
|
||||||
|
|
||||||
event.From = msg.ChannelID + ":" + msg.Author.ID
|
|
||||||
event.To = dc.master.botID
|
|
||||||
event.Type = input.TextEvent
|
|
||||||
event.Data = []byte(msg.Content)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dc *discordConn) Send(e *input.Event) error {
|
|
||||||
fields := strings.Split(e.To, ":")
|
|
||||||
_, err := dc.master.session.ChannelMessageSend(fields[0], string(e.Data))
|
|
||||||
if err != nil {
|
|
||||||
log.Log("[bot][loop][send]", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dc *discordConn) Close() error {
|
|
||||||
if err := dc.master.session.Close(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-dc.exit:
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
close(dc.exit)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,153 +0,0 @@
|
|||||||
package discord
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
"github.com/micro/cli"
|
|
||||||
"github.com/micro/go-micro/agent/input"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
input.Inputs["discord"] = newInput()
|
|
||||||
}
|
|
||||||
|
|
||||||
func newInput() *discordInput {
|
|
||||||
return &discordInput{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type discordInput struct {
|
|
||||||
token string
|
|
||||||
whitelist []string
|
|
||||||
prefix string
|
|
||||||
prefixfn func(string) (string, bool)
|
|
||||||
botID string
|
|
||||||
|
|
||||||
session *discordgo.Session
|
|
||||||
|
|
||||||
sync.Mutex
|
|
||||||
running bool
|
|
||||||
exit chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *discordInput) Flags() []cli.Flag {
|
|
||||||
return []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "discord_token",
|
|
||||||
EnvVar: "MICRO_DISCORD_TOKEN",
|
|
||||||
Usage: "Discord token (prefix with Bot if it's for bot account)",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "discord_whitelist",
|
|
||||||
EnvVar: "MICRO_DISCORD_WHITELIST",
|
|
||||||
Usage: "Discord Whitelist (seperated by ,)",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "discord_prefix",
|
|
||||||
Usage: "Discord Prefix",
|
|
||||||
EnvVar: "MICRO_DISCORD_PREFIX",
|
|
||||||
Value: "Micro ",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *discordInput) Init(ctx *cli.Context) error {
|
|
||||||
token := ctx.String("discord_token")
|
|
||||||
whitelist := ctx.String("discord_whitelist")
|
|
||||||
prefix := ctx.String("discord_prefix")
|
|
||||||
|
|
||||||
if len(token) == 0 {
|
|
||||||
return errors.New("require token")
|
|
||||||
}
|
|
||||||
|
|
||||||
d.token = token
|
|
||||||
d.prefix = prefix
|
|
||||||
|
|
||||||
if len(whitelist) > 0 {
|
|
||||||
d.whitelist = strings.Split(whitelist, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *discordInput) Start() error {
|
|
||||||
if len(d.token) == 0 {
|
|
||||||
return errors.New("missing discord configuration")
|
|
||||||
}
|
|
||||||
|
|
||||||
d.Lock()
|
|
||||||
defer d.Unlock()
|
|
||||||
|
|
||||||
if d.running {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
d.session, err = discordgo.New(d.token)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := d.session.User("@me")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
d.botID = u.ID
|
|
||||||
d.prefixfn = CheckPrefixFactory(fmt.Sprintf("<@%s> ", d.botID), fmt.Sprintf("<@!%s> ", d.botID), d.prefix)
|
|
||||||
|
|
||||||
d.exit = make(chan struct{})
|
|
||||||
d.running = true
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *discordInput) Stream() (input.Conn, error) {
|
|
||||||
d.Lock()
|
|
||||||
defer d.Unlock()
|
|
||||||
if !d.running {
|
|
||||||
return nil, errors.New("not running")
|
|
||||||
}
|
|
||||||
|
|
||||||
//Fire-n-forget close just in case...
|
|
||||||
d.session.Close()
|
|
||||||
|
|
||||||
conn := newConn(d)
|
|
||||||
if err := d.session.Open(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *discordInput) Stop() error {
|
|
||||||
d.Lock()
|
|
||||||
defer d.Unlock()
|
|
||||||
|
|
||||||
if !d.running {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
close(d.exit)
|
|
||||||
d.running = false
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *discordInput) String() string {
|
|
||||||
return "discord"
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckPrefixFactory Creates a prefix checking function and stuff.
|
|
||||||
func CheckPrefixFactory(prefixes ...string) func(string) (string, bool) {
|
|
||||||
return func(content string) (string, bool) {
|
|
||||||
for _, prefix := range prefixes {
|
|
||||||
if strings.HasPrefix(content, prefix) {
|
|
||||||
return strings.TrimPrefix(content, prefix), true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
// Package input is an interface for bot inputs
|
|
||||||
package input
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/micro/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
type EventType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
TextEvent EventType = "text"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// Inputs keyed by name
|
|
||||||
// Example slack or hipchat
|
|
||||||
Inputs = map[string]Input{}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Event is the unit sent and received
|
|
||||||
type Event struct {
|
|
||||||
Type EventType
|
|
||||||
From string
|
|
||||||
To string
|
|
||||||
Data []byte
|
|
||||||
Meta map[string]interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Input is an interface for sources which
|
|
||||||
// provide a way to communicate with the bot.
|
|
||||||
// Slack, HipChat, XMPP, etc.
|
|
||||||
type Input interface {
|
|
||||||
// Provide cli flags
|
|
||||||
Flags() []cli.Flag
|
|
||||||
// Initialise input using cli context
|
|
||||||
Init(*cli.Context) error
|
|
||||||
// Stream events from the input
|
|
||||||
Stream() (Conn, error)
|
|
||||||
// Start the input
|
|
||||||
Start() error
|
|
||||||
// Stop the input
|
|
||||||
Stop() error
|
|
||||||
// name of the input
|
|
||||||
String() string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Conn interface provides a way to
|
|
||||||
// send and receive events. Send and
|
|
||||||
// Recv both block until succeeding
|
|
||||||
// or failing.
|
|
||||||
type Conn interface {
|
|
||||||
Close() error
|
|
||||||
Recv(*Event) error
|
|
||||||
Send(*Event) error
|
|
||||||
}
|
|
||||||
@@ -1,160 +0,0 @@
|
|||||||
package slack
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/agent/input"
|
|
||||||
"github.com/nlopes/slack"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Satisfies the input.Conn interface
|
|
||||||
type slackConn struct {
|
|
||||||
auth *slack.AuthTestResponse
|
|
||||||
rtm *slack.RTM
|
|
||||||
exit chan bool
|
|
||||||
|
|
||||||
sync.Mutex
|
|
||||||
names map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *slackConn) run() {
|
|
||||||
// func retrieves user names and maps to IDs
|
|
||||||
setNames := func() {
|
|
||||||
names := make(map[string]string)
|
|
||||||
users, err := s.rtm.Client.GetUsers()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, user := range users {
|
|
||||||
names[user.ID] = user.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
s.Lock()
|
|
||||||
s.names = names
|
|
||||||
s.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
setNames()
|
|
||||||
|
|
||||||
t := time.NewTicker(time.Minute)
|
|
||||||
defer t.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-s.exit:
|
|
||||||
return
|
|
||||||
case <-t.C:
|
|
||||||
setNames()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *slackConn) getName(id string) string {
|
|
||||||
s.Lock()
|
|
||||||
name := s.names[id]
|
|
||||||
s.Unlock()
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *slackConn) Close() error {
|
|
||||||
select {
|
|
||||||
case <-s.exit:
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
close(s.exit)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *slackConn) Recv(event *input.Event) error {
|
|
||||||
if event == nil {
|
|
||||||
return errors.New("event cannot be nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-s.exit:
|
|
||||||
return errors.New("connection closed")
|
|
||||||
case e := <-s.rtm.IncomingEvents:
|
|
||||||
switch ev := e.Data.(type) {
|
|
||||||
case *slack.MessageEvent:
|
|
||||||
// only accept type message
|
|
||||||
if ev.Type != "message" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// only accept DMs or messages to me
|
|
||||||
switch {
|
|
||||||
case strings.HasPrefix(ev.Channel, "D"):
|
|
||||||
case strings.HasPrefix(ev.Text, s.auth.User):
|
|
||||||
case strings.HasPrefix(ev.Text, fmt.Sprintf("<@%s>", s.auth.UserID)):
|
|
||||||
default:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strip username from text
|
|
||||||
switch {
|
|
||||||
case strings.HasPrefix(ev.Text, s.auth.User):
|
|
||||||
args := strings.Split(ev.Text, " ")[1:]
|
|
||||||
ev.Text = strings.Join(args, " ")
|
|
||||||
event.To = s.auth.User
|
|
||||||
case strings.HasPrefix(ev.Text, fmt.Sprintf("<@%s>", s.auth.UserID)):
|
|
||||||
args := strings.Split(ev.Text, " ")[1:]
|
|
||||||
ev.Text = strings.Join(args, " ")
|
|
||||||
event.To = s.auth.UserID
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.Meta == nil {
|
|
||||||
event.Meta = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// fill in the blanks
|
|
||||||
event.From = ev.Channel + ":" + ev.User
|
|
||||||
event.Type = input.TextEvent
|
|
||||||
event.Data = []byte(ev.Text)
|
|
||||||
event.Meta["reply"] = ev
|
|
||||||
return nil
|
|
||||||
case *slack.InvalidAuthEvent:
|
|
||||||
return errors.New("invalid credentials")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *slackConn) Send(event *input.Event) error {
|
|
||||||
var channel, message, name string
|
|
||||||
|
|
||||||
if len(event.To) == 0 {
|
|
||||||
return errors.New("require Event.To")
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := strings.Split(event.To, ":")
|
|
||||||
|
|
||||||
if len(parts) == 2 {
|
|
||||||
channel = parts[0]
|
|
||||||
name = s.getName(parts[1])
|
|
||||||
// try using reply meta
|
|
||||||
} else if ev, ok := event.Meta["reply"]; ok {
|
|
||||||
channel = ev.(*slack.MessageEvent).Channel
|
|
||||||
name = s.getName(ev.(*slack.MessageEvent).User)
|
|
||||||
}
|
|
||||||
|
|
||||||
// don't know where to send the message
|
|
||||||
if len(channel) == 0 {
|
|
||||||
return errors.New("could not determine who message is to")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(name) == 0 || strings.HasPrefix(channel, "D") {
|
|
||||||
message = string(event.Data)
|
|
||||||
} else {
|
|
||||||
message = fmt.Sprintf("@%s: %s", name, string(event.Data))
|
|
||||||
}
|
|
||||||
|
|
||||||
s.rtm.SendMessage(s.rtm.NewOutgoingMessage(message, channel))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
package slack
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/micro/cli"
|
|
||||||
"github.com/micro/go-micro/agent/input"
|
|
||||||
"github.com/nlopes/slack"
|
|
||||||
)
|
|
||||||
|
|
||||||
type slackInput struct {
|
|
||||||
debug bool
|
|
||||||
token string
|
|
||||||
|
|
||||||
sync.Mutex
|
|
||||||
running bool
|
|
||||||
exit chan bool
|
|
||||||
|
|
||||||
api *slack.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
input.Inputs["slack"] = NewInput()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *slackInput) Flags() []cli.Flag {
|
|
||||||
return []cli.Flag{
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "slack_debug",
|
|
||||||
Usage: "Slack debug output",
|
|
||||||
EnvVar: "MICRO_SLACK_DEBUG",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "slack_token",
|
|
||||||
Usage: "Slack token",
|
|
||||||
EnvVar: "MICRO_SLACK_TOKEN",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *slackInput) Init(ctx *cli.Context) error {
|
|
||||||
debug := ctx.Bool("slack_debug")
|
|
||||||
token := ctx.String("slack_token")
|
|
||||||
|
|
||||||
if len(token) == 0 {
|
|
||||||
return errors.New("missing slack token")
|
|
||||||
}
|
|
||||||
|
|
||||||
p.debug = debug
|
|
||||||
p.token = token
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *slackInput) Stream() (input.Conn, error) {
|
|
||||||
p.Lock()
|
|
||||||
defer p.Unlock()
|
|
||||||
|
|
||||||
if !p.running {
|
|
||||||
return nil, errors.New("not running")
|
|
||||||
}
|
|
||||||
|
|
||||||
// test auth
|
|
||||||
auth, err := p.api.AuthTest()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rtm := p.api.NewRTM()
|
|
||||||
exit := make(chan bool)
|
|
||||||
|
|
||||||
go rtm.ManageConnection()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-p.exit:
|
|
||||||
select {
|
|
||||||
case <-exit:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
close(exit)
|
|
||||||
}
|
|
||||||
case <-exit:
|
|
||||||
}
|
|
||||||
|
|
||||||
rtm.Disconnect()
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn := &slackConn{
|
|
||||||
auth: auth,
|
|
||||||
rtm: rtm,
|
|
||||||
exit: exit,
|
|
||||||
names: make(map[string]string),
|
|
||||||
}
|
|
||||||
|
|
||||||
go conn.run()
|
|
||||||
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *slackInput) Start() error {
|
|
||||||
if len(p.token) == 0 {
|
|
||||||
return errors.New("missing slack token")
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Lock()
|
|
||||||
defer p.Unlock()
|
|
||||||
|
|
||||||
if p.running {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
api := slack.New(p.token, slack.OptionDebug(p.debug))
|
|
||||||
|
|
||||||
// test auth
|
|
||||||
_, err := api.AuthTest()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
p.api = api
|
|
||||||
p.exit = make(chan bool)
|
|
||||||
p.running = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *slackInput) Stop() error {
|
|
||||||
p.Lock()
|
|
||||||
defer p.Unlock()
|
|
||||||
|
|
||||||
if !p.running {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
close(p.exit)
|
|
||||||
p.running = false
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *slackInput) String() string {
|
|
||||||
return "slack"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewInput() input.Input {
|
|
||||||
return &slackInput{}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
# Telegram Messenger input for micro bot
|
|
||||||
[Telegram](https://telegram.org) support for micro bot based on [telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api).
|
|
||||||
|
|
||||||
## Options
|
|
||||||
### --telegram_token (required)
|
|
||||||
|
|
||||||
Sets bot's token for interacting with API.
|
|
||||||
|
|
||||||
Head over to Telegram's [API documentation](https://core.telegram.org/bots/api)
|
|
||||||
to learn how to create bots and how the API works.
|
|
||||||
|
|
||||||
### --telegram_debug
|
|
||||||
|
|
||||||
Sets the debug flag to make the bot's output verbose.
|
|
||||||
|
|
||||||
### --telegram_whitelist
|
|
||||||
|
|
||||||
Sets a list of comma-separated nicknames (without @ symbol in the beginning) for interacting with bot. Only these users can use the bot.
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
package telegram
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/forestgiant/sliceutil"
|
|
||||||
"github.com/micro/go-micro/agent/input"
|
|
||||||
"github.com/micro/go-micro/util/log"
|
|
||||||
"gopkg.in/telegram-bot-api.v4"
|
|
||||||
)
|
|
||||||
|
|
||||||
type telegramConn struct {
|
|
||||||
input *telegramInput
|
|
||||||
|
|
||||||
recv <-chan tgbotapi.Update
|
|
||||||
exit chan bool
|
|
||||||
|
|
||||||
syncCond *sync.Cond
|
|
||||||
mutex sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func newConn(input *telegramInput) (*telegramConn, error) {
|
|
||||||
conn := &telegramConn{
|
|
||||||
input: input,
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.syncCond = sync.NewCond(&conn.mutex)
|
|
||||||
|
|
||||||
go conn.run()
|
|
||||||
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *telegramConn) run() {
|
|
||||||
u := tgbotapi.NewUpdate(0)
|
|
||||||
u.Timeout = 60
|
|
||||||
updates, err := tc.input.api.GetUpdatesChan(u)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tc.recv = updates
|
|
||||||
tc.syncCond.Signal()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-tc.exit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *telegramConn) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *telegramConn) Recv(event *input.Event) error {
|
|
||||||
if event == nil {
|
|
||||||
return errors.New("event cannot be nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
if tc.recv == nil {
|
|
||||||
tc.mutex.Lock()
|
|
||||||
tc.syncCond.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
update := <-tc.recv
|
|
||||||
|
|
||||||
if update.Message == nil || (len(tc.input.whitelist) > 0 && !sliceutil.Contains(tc.input.whitelist, update.Message.From.UserName)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.Meta == nil {
|
|
||||||
event.Meta = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
event.Type = input.TextEvent
|
|
||||||
event.From = update.Message.From.UserName
|
|
||||||
event.To = tc.input.api.Self.UserName
|
|
||||||
event.Data = []byte(update.Message.Text)
|
|
||||||
event.Meta["chatId"] = update.Message.Chat.ID
|
|
||||||
event.Meta["chatType"] = update.Message.Chat.Type
|
|
||||||
event.Meta["messageId"] = update.Message.MessageID
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *telegramConn) Send(event *input.Event) error {
|
|
||||||
messageText := strings.TrimSpace(string(event.Data))
|
|
||||||
|
|
||||||
chatId := event.Meta["chatId"].(int64)
|
|
||||||
chatType := ChatType(event.Meta["chatType"].(string))
|
|
||||||
|
|
||||||
msgConfig := tgbotapi.NewMessage(chatId, messageText)
|
|
||||||
msgConfig.ParseMode = tgbotapi.ModeHTML
|
|
||||||
|
|
||||||
if sliceutil.Contains([]ChatType{Group, Supergroup}, chatType) {
|
|
||||||
msgConfig.ReplyToMessageID = event.Meta["messageId"].(int)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := tc.input.api.Send(msgConfig)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
// probably it could be because of nested HTML tags -- telegram doesn't allow nested tags
|
|
||||||
log.Log("[telegram][Send] error:", err)
|
|
||||||
msgConfig.Text = "This bot couldn't send the response (Internal error)"
|
|
||||||
tc.input.api.Send(msgConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
package telegram
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/micro/cli"
|
|
||||||
"github.com/micro/go-micro/agent/input"
|
|
||||||
"gopkg.in/telegram-bot-api.v4"
|
|
||||||
)
|
|
||||||
|
|
||||||
type telegramInput struct {
|
|
||||||
sync.Mutex
|
|
||||||
|
|
||||||
debug bool
|
|
||||||
token string
|
|
||||||
whitelist []string
|
|
||||||
|
|
||||||
api *tgbotapi.BotAPI
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChatType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
Private ChatType = "private"
|
|
||||||
Group ChatType = "group"
|
|
||||||
Supergroup ChatType = "supergroup"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
input.Inputs["telegram"] = &telegramInput{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *telegramInput) Flags() []cli.Flag {
|
|
||||||
return []cli.Flag{
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "telegram_debug",
|
|
||||||
EnvVar: "MICRO_TELEGRAM_DEBUG",
|
|
||||||
Usage: "Telegram debug output",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "telegram_token",
|
|
||||||
EnvVar: "MICRO_TELEGRAM_TOKEN",
|
|
||||||
Usage: "Telegram token",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "telegram_whitelist",
|
|
||||||
EnvVar: "MICRO_TELEGRAM_WHITELIST",
|
|
||||||
Usage: "Telegram bot's users (comma-separated values)",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *telegramInput) Init(ctx *cli.Context) error {
|
|
||||||
ti.debug = ctx.Bool("telegram_debug")
|
|
||||||
ti.token = ctx.String("telegram_token")
|
|
||||||
|
|
||||||
whitelist := ctx.String("telegram_whitelist")
|
|
||||||
|
|
||||||
if whitelist != "" {
|
|
||||||
ti.whitelist = strings.Split(whitelist, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ti.token) == 0 {
|
|
||||||
return errors.New("missing telegram token")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *telegramInput) Stream() (input.Conn, error) {
|
|
||||||
ti.Lock()
|
|
||||||
defer ti.Unlock()
|
|
||||||
|
|
||||||
return newConn(ti)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *telegramInput) Start() error {
|
|
||||||
ti.Lock()
|
|
||||||
defer ti.Unlock()
|
|
||||||
|
|
||||||
api, err := tgbotapi.NewBotAPI(ti.token)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ti.api = api
|
|
||||||
|
|
||||||
api.Debug = ti.debug
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *telegramInput) Stop() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *telegramInput) String() string {
|
|
||||||
return "telegram"
|
|
||||||
}
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
|
||||||
// source: github.com/micro/go-bot/proto/bot.proto
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package go_micro_bot is a generated protocol buffer package.
|
|
||||||
|
|
||||||
It is generated from these files:
|
|
||||||
github.com/micro/go-bot/proto/bot.proto
|
|
||||||
|
|
||||||
It has these top-level messages:
|
|
||||||
HelpRequest
|
|
||||||
HelpResponse
|
|
||||||
ExecRequest
|
|
||||||
ExecResponse
|
|
||||||
*/
|
|
||||||
package go_micro_bot
|
|
||||||
|
|
||||||
import proto "github.com/golang/protobuf/proto"
|
|
||||||
import fmt "fmt"
|
|
||||||
import math "math"
|
|
||||||
|
|
||||||
import (
|
|
||||||
context "context"
|
|
||||||
client "github.com/micro/go-micro/client"
|
|
||||||
server "github.com/micro/go-micro/server"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ = proto.Marshal
|
|
||||||
var _ = fmt.Errorf
|
|
||||||
var _ = math.Inf
|
|
||||||
|
|
||||||
// This is a compile-time assertion to ensure that this generated file
|
|
||||||
// is compatible with the proto package it is being compiled against.
|
|
||||||
// A compilation error at this line likely means your copy of the
|
|
||||||
// proto package needs to be updated.
|
|
||||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ context.Context
|
|
||||||
var _ client.Option
|
|
||||||
var _ server.Option
|
|
||||||
|
|
||||||
// Client API for Command service
|
|
||||||
|
|
||||||
type CommandService interface {
|
|
||||||
Help(ctx context.Context, in *HelpRequest, opts ...client.CallOption) (*HelpResponse, error)
|
|
||||||
Exec(ctx context.Context, in *ExecRequest, opts ...client.CallOption) (*ExecResponse, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type commandService struct {
|
|
||||||
c client.Client
|
|
||||||
name string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCommandService(name string, c client.Client) CommandService {
|
|
||||||
if c == nil {
|
|
||||||
c = client.NewClient()
|
|
||||||
}
|
|
||||||
if len(name) == 0 {
|
|
||||||
name = "go.micro.bot"
|
|
||||||
}
|
|
||||||
return &commandService{
|
|
||||||
c: c,
|
|
||||||
name: name,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *commandService) Help(ctx context.Context, in *HelpRequest, opts ...client.CallOption) (*HelpResponse, error) {
|
|
||||||
req := c.c.NewRequest(c.name, "Command.Help", in)
|
|
||||||
out := new(HelpResponse)
|
|
||||||
err := c.c.Call(ctx, req, out, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *commandService) Exec(ctx context.Context, in *ExecRequest, opts ...client.CallOption) (*ExecResponse, error) {
|
|
||||||
req := c.c.NewRequest(c.name, "Command.Exec", in)
|
|
||||||
out := new(ExecResponse)
|
|
||||||
err := c.c.Call(ctx, req, out, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server API for Command service
|
|
||||||
|
|
||||||
type CommandHandler interface {
|
|
||||||
Help(context.Context, *HelpRequest, *HelpResponse) error
|
|
||||||
Exec(context.Context, *ExecRequest, *ExecResponse) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterCommandHandler(s server.Server, hdlr CommandHandler, opts ...server.HandlerOption) error {
|
|
||||||
type _command interface {
|
|
||||||
Help(ctx context.Context, in *HelpRequest, out *HelpResponse) error
|
|
||||||
Exec(ctx context.Context, in *ExecRequest, out *ExecResponse) error
|
|
||||||
}
|
|
||||||
type Command struct {
|
|
||||||
_command
|
|
||||||
}
|
|
||||||
h := &commandHandler{hdlr}
|
|
||||||
return s.Handle(s.NewHandler(&Command{h}, opts...))
|
|
||||||
}
|
|
||||||
|
|
||||||
type commandHandler struct {
|
|
||||||
CommandHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *commandHandler) Help(ctx context.Context, in *HelpRequest, out *HelpResponse) error {
|
|
||||||
return h.CommandHandler.Help(ctx, in, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *commandHandler) Exec(ctx context.Context, in *ExecRequest, out *ExecResponse) error {
|
|
||||||
return h.CommandHandler.Exec(ctx, in, out)
|
|
||||||
}
|
|
||||||
@@ -1,210 +0,0 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
|
||||||
// source: github.com/micro/go-bot/proto/bot.proto
|
|
||||||
|
|
||||||
package go_micro_bot
|
|
||||||
|
|
||||||
import proto "github.com/golang/protobuf/proto"
|
|
||||||
import fmt "fmt"
|
|
||||||
import math "math"
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ = proto.Marshal
|
|
||||||
var _ = fmt.Errorf
|
|
||||||
var _ = math.Inf
|
|
||||||
|
|
||||||
// This is a compile-time assertion to ensure that this generated file
|
|
||||||
// is compatible with the proto package it is being compiled against.
|
|
||||||
// A compilation error at this line likely means your copy of the
|
|
||||||
// proto package needs to be updated.
|
|
||||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
|
||||||
|
|
||||||
type HelpRequest struct {
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *HelpRequest) Reset() { *m = HelpRequest{} }
|
|
||||||
func (m *HelpRequest) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*HelpRequest) ProtoMessage() {}
|
|
||||||
func (*HelpRequest) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_bot_654832eab83ed4b5, []int{0}
|
|
||||||
}
|
|
||||||
func (m *HelpRequest) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_HelpRequest.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *HelpRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_HelpRequest.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *HelpRequest) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_HelpRequest.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *HelpRequest) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_HelpRequest.Size(m)
|
|
||||||
}
|
|
||||||
func (m *HelpRequest) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_HelpRequest.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_HelpRequest proto.InternalMessageInfo
|
|
||||||
|
|
||||||
type HelpResponse struct {
|
|
||||||
Usage string `protobuf:"bytes,1,opt,name=usage,proto3" json:"usage,omitempty"`
|
|
||||||
Description string `protobuf:"bytes,2,opt,name=description,proto3" json:"description,omitempty"`
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *HelpResponse) Reset() { *m = HelpResponse{} }
|
|
||||||
func (m *HelpResponse) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*HelpResponse) ProtoMessage() {}
|
|
||||||
func (*HelpResponse) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_bot_654832eab83ed4b5, []int{1}
|
|
||||||
}
|
|
||||||
func (m *HelpResponse) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_HelpResponse.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *HelpResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_HelpResponse.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *HelpResponse) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_HelpResponse.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *HelpResponse) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_HelpResponse.Size(m)
|
|
||||||
}
|
|
||||||
func (m *HelpResponse) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_HelpResponse.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_HelpResponse proto.InternalMessageInfo
|
|
||||||
|
|
||||||
func (m *HelpResponse) GetUsage() string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Usage
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *HelpResponse) GetDescription() string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Description
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
type ExecRequest struct {
|
|
||||||
Args []string `protobuf:"bytes,1,rep,name=args,proto3" json:"args,omitempty"`
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ExecRequest) Reset() { *m = ExecRequest{} }
|
|
||||||
func (m *ExecRequest) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*ExecRequest) ProtoMessage() {}
|
|
||||||
func (*ExecRequest) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_bot_654832eab83ed4b5, []int{2}
|
|
||||||
}
|
|
||||||
func (m *ExecRequest) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_ExecRequest.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *ExecRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_ExecRequest.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *ExecRequest) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_ExecRequest.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *ExecRequest) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_ExecRequest.Size(m)
|
|
||||||
}
|
|
||||||
func (m *ExecRequest) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_ExecRequest.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_ExecRequest proto.InternalMessageInfo
|
|
||||||
|
|
||||||
func (m *ExecRequest) GetArgs() []string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Args
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ExecResponse struct {
|
|
||||||
Result []byte `protobuf:"bytes,1,opt,name=result,proto3" json:"result,omitempty"`
|
|
||||||
Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ExecResponse) Reset() { *m = ExecResponse{} }
|
|
||||||
func (m *ExecResponse) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*ExecResponse) ProtoMessage() {}
|
|
||||||
func (*ExecResponse) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_bot_654832eab83ed4b5, []int{3}
|
|
||||||
}
|
|
||||||
func (m *ExecResponse) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_ExecResponse.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *ExecResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_ExecResponse.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *ExecResponse) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_ExecResponse.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *ExecResponse) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_ExecResponse.Size(m)
|
|
||||||
}
|
|
||||||
func (m *ExecResponse) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_ExecResponse.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_ExecResponse proto.InternalMessageInfo
|
|
||||||
|
|
||||||
func (m *ExecResponse) GetResult() []byte {
|
|
||||||
if m != nil {
|
|
||||||
return m.Result
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ExecResponse) GetError() string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Error
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
proto.RegisterType((*HelpRequest)(nil), "go.micro.bot.HelpRequest")
|
|
||||||
proto.RegisterType((*HelpResponse)(nil), "go.micro.bot.HelpResponse")
|
|
||||||
proto.RegisterType((*ExecRequest)(nil), "go.micro.bot.ExecRequest")
|
|
||||||
proto.RegisterType((*ExecResponse)(nil), "go.micro.bot.ExecResponse")
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
proto.RegisterFile("github.com/micro/go-bot/proto/bot.proto", fileDescriptor_bot_654832eab83ed4b5)
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileDescriptor_bot_654832eab83ed4b5 = []byte{
|
|
||||||
// 246 bytes of a gzipped FileDescriptorProto
|
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x41, 0x4b, 0xc4, 0x30,
|
|
||||||
0x10, 0x85, 0xb7, 0xba, 0xae, 0xec, 0xb4, 0x5e, 0x82, 0x48, 0xdd, 0x53, 0xcd, 0xc5, 0xbd, 0x98,
|
|
||||||
0x82, 0x5e, 0x05, 0x0f, 0xa2, 0x78, 0xee, 0x3f, 0x68, 0xba, 0x43, 0x2c, 0x6c, 0x3b, 0x35, 0x99,
|
|
||||||
0x82, 0xff, 0xc1, 0x3f, 0x2d, 0x4d, 0x72, 0x08, 0xcb, 0xde, 0xe6, 0x65, 0x86, 0xf7, 0xbe, 0x17,
|
|
||||||
0x78, 0x34, 0x3d, 0x7f, 0xcf, 0x5a, 0x75, 0x34, 0xd4, 0x43, 0xdf, 0x59, 0xaa, 0x0d, 0x3d, 0x69,
|
|
||||||
0xe2, 0x7a, 0xb2, 0xc4, 0x54, 0x6b, 0x62, 0xe5, 0x27, 0x51, 0x18, 0x52, 0xfe, 0x40, 0x69, 0x62,
|
|
||||||
0x79, 0x03, 0xf9, 0x17, 0x1e, 0xa7, 0x06, 0x7f, 0x66, 0x74, 0x2c, 0x3f, 0xa1, 0x08, 0xd2, 0x4d,
|
|
||||||
0x34, 0x3a, 0x14, 0xb7, 0x70, 0x35, 0xbb, 0xd6, 0x60, 0x99, 0x55, 0xd9, 0x7e, 0xdb, 0x04, 0x21,
|
|
||||||
0x2a, 0xc8, 0x0f, 0xe8, 0x3a, 0xdb, 0x4f, 0xdc, 0xd3, 0x58, 0x5e, 0xf8, 0x5d, 0xfa, 0x24, 0x1f,
|
|
||||||
0x20, 0xff, 0xf8, 0xc5, 0x2e, 0xda, 0x0a, 0x01, 0xeb, 0xd6, 0x1a, 0x57, 0x66, 0xd5, 0xe5, 0x7e,
|
|
||||||
0xdb, 0xf8, 0x59, 0xbe, 0x42, 0x11, 0x4e, 0x62, 0xd4, 0x1d, 0x6c, 0x2c, 0xba, 0xf9, 0xc8, 0x3e,
|
|
||||||
0xab, 0x68, 0xa2, 0x5a, 0x10, 0xd0, 0x5a, 0xb2, 0x31, 0x26, 0x88, 0xe7, 0xbf, 0x0c, 0xae, 0xdf,
|
|
||||||
0x69, 0x18, 0xda, 0xf1, 0x20, 0xde, 0x60, 0xbd, 0x40, 0x8b, 0x7b, 0x95, 0x56, 0x53, 0x49, 0xaf,
|
|
||||||
0xdd, 0xee, 0xdc, 0x2a, 0x04, 0xcb, 0xd5, 0x62, 0xb0, 0xa0, 0x9c, 0x1a, 0x24, 0x0d, 0x4e, 0x0d,
|
|
||||||
0x52, 0x72, 0xb9, 0xd2, 0x1b, 0xff, 0xb5, 0x2f, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff, 0xcb, 0x77,
|
|
||||||
0xdf, 0x28, 0x85, 0x01, 0x00, 0x00,
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
syntax = "proto3";
|
|
||||||
|
|
||||||
package go.micro.bot;
|
|
||||||
|
|
||||||
service Command {
|
|
||||||
rpc Help(HelpRequest) returns (HelpResponse) {};
|
|
||||||
rpc Exec(ExecRequest) returns (ExecResponse) {};
|
|
||||||
}
|
|
||||||
|
|
||||||
message HelpRequest {
|
|
||||||
}
|
|
||||||
|
|
||||||
message HelpResponse {
|
|
||||||
string usage = 1;
|
|
||||||
string description = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
message ExecRequest {
|
|
||||||
repeated string args = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
message ExecResponse {
|
|
||||||
bytes result = 1;
|
|
||||||
string error = 2;
|
|
||||||
}
|
|
||||||
67
api/api.go
67
api/api.go
@@ -5,10 +5,27 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/unistack-org/micro/v3/registry"
|
||||||
"github.com/micro/go-micro/server"
|
"github.com/unistack-org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Api interface {
|
||||||
|
// Initialise options
|
||||||
|
Init(...Option) error
|
||||||
|
// Get the options
|
||||||
|
Options() Options
|
||||||
|
// Register a http handler
|
||||||
|
Register(*Endpoint) error
|
||||||
|
// Register a route
|
||||||
|
Deregister(*Endpoint) error
|
||||||
|
// Implementation of api
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Options struct{}
|
||||||
|
|
||||||
|
type Option func(*Options) error
|
||||||
|
|
||||||
// Endpoint is a mapping between an RPC method and HTTP endpoint
|
// Endpoint is a mapping between an RPC method and HTTP endpoint
|
||||||
type Endpoint struct {
|
type Endpoint struct {
|
||||||
// RPC Method e.g. Greeter.Hello
|
// RPC Method e.g. Greeter.Hello
|
||||||
@@ -23,6 +40,12 @@ type Endpoint struct {
|
|||||||
Method []string
|
Method []string
|
||||||
// HTTP Path e.g /greeter. Expect POSIX regex
|
// HTTP Path e.g /greeter. Expect POSIX regex
|
||||||
Path []string
|
Path []string
|
||||||
|
// Body destination
|
||||||
|
// "*" or "" - top level message value
|
||||||
|
// "string" - inner message value
|
||||||
|
Body string
|
||||||
|
// Stream flag
|
||||||
|
Stream bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Service represents an API service
|
// Service represents an API service
|
||||||
@@ -57,14 +80,25 @@ func Encode(e *Endpoint) map[string]string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return map[string]string{
|
// endpoint map
|
||||||
"endpoint": e.Name,
|
ep := make(map[string]string)
|
||||||
"description": e.Description,
|
|
||||||
"method": strings.Join(e.Method, ","),
|
// set vals only if they exist
|
||||||
"path": strings.Join(e.Path, ","),
|
set := func(k, v string) {
|
||||||
"host": strings.Join(e.Host, ","),
|
if len(v) == 0 {
|
||||||
"handler": e.Handler,
|
return
|
||||||
|
}
|
||||||
|
ep[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set("endpoint", e.Name)
|
||||||
|
set("description", e.Description)
|
||||||
|
set("handler", e.Handler)
|
||||||
|
set("method", strings.Join(e.Method, ","))
|
||||||
|
set("path", strings.Join(e.Path, ","))
|
||||||
|
set("host", strings.Join(e.Host, ","))
|
||||||
|
|
||||||
|
return ep
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode decodes endpoint metadata into an endpoint
|
// Decode decodes endpoint metadata into an endpoint
|
||||||
@@ -94,9 +128,18 @@ func Validate(e *Endpoint) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range e.Path {
|
for _, p := range e.Path {
|
||||||
_, err := regexp.CompilePOSIX(p)
|
ps := p[0]
|
||||||
if err != nil {
|
pe := p[len(p)-1]
|
||||||
return err
|
|
||||||
|
if ps == '^' && pe == '$' {
|
||||||
|
_, err := regexp.CompilePOSIX(p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if ps == '^' && pe != '$' {
|
||||||
|
return errors.New("invalid path")
|
||||||
|
} else if ps != '^' && pe == '$' {
|
||||||
|
return errors.New("invalid path")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,3 +111,42 @@ func TestEncoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidate(t *testing.T) {
|
||||||
|
epPcre := &Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
Description: "A test endpoint",
|
||||||
|
Handler: "meta",
|
||||||
|
Host: []string{"foo.com"},
|
||||||
|
Method: []string{"GET"},
|
||||||
|
Path: []string{"^/test/?$"},
|
||||||
|
}
|
||||||
|
if err := Validate(epPcre); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
epGpath := &Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
Description: "A test endpoint",
|
||||||
|
Handler: "meta",
|
||||||
|
Host: []string{"foo.com"},
|
||||||
|
Method: []string{"GET"},
|
||||||
|
Path: []string{"/test/{id}"},
|
||||||
|
}
|
||||||
|
if err := Validate(epGpath); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
epPcreInvalid := &Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
Description: "A test endpoint",
|
||||||
|
Handler: "meta",
|
||||||
|
Host: []string{"foo.com"},
|
||||||
|
Method: []string{"GET"},
|
||||||
|
Path: []string{"/test/?$"},
|
||||||
|
}
|
||||||
|
if err := Validate(epPcreInvalid); err == nil {
|
||||||
|
t.Fatalf("invalid pcre %v", epPcreInvalid.Path[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ package api
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
goapi "github.com/micro/go-micro/api"
|
goapi "github.com/unistack-org/micro/v3/api"
|
||||||
"github.com/micro/go-micro/api/handler"
|
"github.com/unistack-org/micro/v3/api/handler"
|
||||||
api "github.com/micro/go-micro/api/proto"
|
api "github.com/unistack-org/micro/v3/api/proto"
|
||||||
"github.com/micro/go-micro/client"
|
"github.com/unistack-org/micro/v3/client"
|
||||||
"github.com/micro/go-micro/client/selector"
|
"github.com/unistack-org/micro/v3/errors"
|
||||||
"github.com/micro/go-micro/errors"
|
"github.com/unistack-org/micro/v3/util/ctx"
|
||||||
"github.com/micro/go-micro/util/ctx"
|
"github.com/unistack-org/micro/v3/util/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
type apiHandler struct {
|
type apiHandler struct {
|
||||||
@@ -24,6 +24,12 @@ const (
|
|||||||
|
|
||||||
// API handler is the default handler which takes api.Request and returns api.Response
|
// API handler is the default handler which takes api.Request and returns api.Response
|
||||||
func (a *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (a *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
bsize := handler.DefaultMaxRecvSize
|
||||||
|
if a.opts.MaxRecvSize > 0 {
|
||||||
|
bsize = a.opts.MaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
||||||
request, err := requestToProto(r)
|
request, err := requestToProto(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er := errors.InternalServerError("go.micro.api", err.Error())
|
er := errors.InternalServerError("go.micro.api", err.Error())
|
||||||
@@ -59,16 +65,14 @@ func (a *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create request and response
|
// create request and response
|
||||||
c := a.opts.Service.Client()
|
c := a.opts.Client
|
||||||
req := c.NewRequest(service.Name, service.Endpoint.Name, request)
|
req := c.NewRequest(service.Name, service.Endpoint.Name, request)
|
||||||
rsp := &api.Response{}
|
rsp := &api.Response{}
|
||||||
|
|
||||||
// create the context from headers
|
// create the context from headers
|
||||||
cx := ctx.FromRequest(r)
|
cx := ctx.FromRequest(r)
|
||||||
// create strategy
|
|
||||||
so := selector.WithStrategy(strategy(service.Services))
|
|
||||||
|
|
||||||
if err := c.Call(cx, req, rsp, client.WithSelectOption(so)); err != nil {
|
if err := c.Call(cx, req, rsp, client.WithRouter(router.New(service.Services))); err != nil {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
ce := errors.Parse(err.Error())
|
ce := errors.Parse(err.Error())
|
||||||
switch ce.Code {
|
switch ce.Code {
|
||||||
|
|||||||
@@ -2,15 +2,18 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"mime"
|
"mime"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
api "github.com/micro/go-micro/api/proto"
|
"github.com/oxtoacart/bpool"
|
||||||
"github.com/micro/go-micro/client/selector"
|
api "github.com/unistack-org/micro/v3/api/proto"
|
||||||
"github.com/micro/go-micro/registry"
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// need to calculate later to specify useful defaults
|
||||||
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
||||||
)
|
)
|
||||||
|
|
||||||
func requestToProto(r *http.Request) (*api.Request, error) {
|
func requestToProto(r *http.Request) (*api.Request, error) {
|
||||||
@@ -29,16 +32,23 @@ func requestToProto(r *http.Request) (*api.Request, error) {
|
|||||||
|
|
||||||
ct, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
ct, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ct = "application/x-www-form-urlencoded"
|
ct = "text/plain; charset=UTF-8" //default CT is text/plain
|
||||||
r.Header.Set("Content-Type", ct)
|
r.Header.Set("Content-Type", ct)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ct {
|
//set the body:
|
||||||
case "application/x-www-form-urlencoded":
|
if r.Body != nil {
|
||||||
// expect form vals
|
switch ct {
|
||||||
default:
|
case "application/x-www-form-urlencoded":
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
// expect form vals in Post data
|
||||||
req.Body = string(data)
|
default:
|
||||||
|
buf := bufferPool.Get()
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
if _, err = buf.ReadFrom(r.Body); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Body = buf.String()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set X-Forwarded-For if it does not exist
|
// Set X-Forwarded-For if it does not exist
|
||||||
@@ -97,11 +107,3 @@ func requestToProto(r *http.Request) (*api.Request, error) {
|
|||||||
|
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// strategy is a hack for selection
|
|
||||||
func strategy(services []*registry.Service) selector.Strategy {
|
|
||||||
return func(_ []*registry.Service) selector.Next {
|
|
||||||
// ignore input to this function, use services above
|
|
||||||
return selector.Random(services)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,268 +0,0 @@
|
|||||||
// Package broker provides a go-micro/broker handler
|
|
||||||
package broker
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
"github.com/micro/go-micro/api/handler"
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
"github.com/micro/go-micro/util/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
Handler = "broker"
|
|
||||||
|
|
||||||
pingTime = (readDeadline * 9) / 10
|
|
||||||
readLimit = 16384
|
|
||||||
readDeadline = 60 * time.Second
|
|
||||||
writeDeadline = 10 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
type brokerHandler struct {
|
|
||||||
opts handler.Options
|
|
||||||
u websocket.Upgrader
|
|
||||||
}
|
|
||||||
|
|
||||||
type conn struct {
|
|
||||||
b broker.Broker
|
|
||||||
cType string
|
|
||||||
topic string
|
|
||||||
queue string
|
|
||||||
exit chan bool
|
|
||||||
|
|
||||||
sync.Mutex
|
|
||||||
ws *websocket.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
once sync.Once
|
|
||||||
contentType = "text/plain"
|
|
||||||
)
|
|
||||||
|
|
||||||
func checkOrigin(r *http.Request) bool {
|
|
||||||
origin := r.Header["Origin"]
|
|
||||||
if len(origin) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
u, err := url.Parse(origin[0])
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return u.Host == r.Host
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *conn) close() {
|
|
||||||
select {
|
|
||||||
case <-c.exit:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
close(c.exit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *conn) readLoop() {
|
|
||||||
defer func() {
|
|
||||||
c.close()
|
|
||||||
c.ws.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// set read limit/deadline
|
|
||||||
c.ws.SetReadLimit(readLimit)
|
|
||||||
c.ws.SetReadDeadline(time.Now().Add(readDeadline))
|
|
||||||
|
|
||||||
// set close handler
|
|
||||||
ch := c.ws.CloseHandler()
|
|
||||||
c.ws.SetCloseHandler(func(code int, text string) error {
|
|
||||||
err := ch(code, text)
|
|
||||||
c.close()
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
|
|
||||||
// set pong handler
|
|
||||||
c.ws.SetPongHandler(func(string) error {
|
|
||||||
c.ws.SetReadDeadline(time.Now().Add(readDeadline))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
for {
|
|
||||||
_, message, err := c.ws.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.b.Publish(c.topic, &broker.Message{
|
|
||||||
Header: map[string]string{"Content-Type": c.cType},
|
|
||||||
Body: message,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *conn) write(mType int, data []byte) error {
|
|
||||||
c.Lock()
|
|
||||||
c.ws.SetWriteDeadline(time.Now().Add(writeDeadline))
|
|
||||||
err := c.ws.WriteMessage(mType, data)
|
|
||||||
c.Unlock()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *conn) writeLoop() {
|
|
||||||
ticker := time.NewTicker(pingTime)
|
|
||||||
|
|
||||||
var opts []broker.SubscribeOption
|
|
||||||
|
|
||||||
if len(c.queue) > 0 {
|
|
||||||
opts = append(opts, broker.Queue(c.queue))
|
|
||||||
}
|
|
||||||
|
|
||||||
subscriber, err := c.b.Subscribe(c.topic, func(p broker.Event) error {
|
|
||||||
b, err := json.Marshal(p.Message())
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return c.write(websocket.TextMessage, b)
|
|
||||||
}, opts...)
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
subscriber.Unsubscribe()
|
|
||||||
ticker.Stop()
|
|
||||||
c.ws.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Log(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case <-c.exit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *brokerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
br := b.opts.Service.Client().Options().Broker
|
|
||||||
|
|
||||||
// Setup the broker
|
|
||||||
once.Do(func() {
|
|
||||||
br.Init()
|
|
||||||
br.Connect()
|
|
||||||
})
|
|
||||||
|
|
||||||
// Parse
|
|
||||||
r.ParseForm()
|
|
||||||
topic := r.Form.Get("topic")
|
|
||||||
|
|
||||||
// Can't do anything without a topic
|
|
||||||
if len(topic) == 0 {
|
|
||||||
http.Error(w, "Topic not specified", 400)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Post assumed to be Publish
|
|
||||||
if r.Method == "POST" {
|
|
||||||
// Create a broker message
|
|
||||||
msg := &broker.Message{
|
|
||||||
Header: make(map[string]string),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set header
|
|
||||||
for k, v := range r.Header {
|
|
||||||
msg.Header[k] = strings.Join(v, ", ")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read body
|
|
||||||
b, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set body
|
|
||||||
msg.Body = b
|
|
||||||
|
|
||||||
// Publish
|
|
||||||
br.Publish(topic, msg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// now back to our regularly scheduled programming
|
|
||||||
|
|
||||||
if r.Method != "GET" {
|
|
||||||
http.Error(w, "Method not allowed", 405)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
queue := r.Form.Get("queue")
|
|
||||||
|
|
||||||
ws, err := b.u.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Log(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cType := r.Header.Get("Content-Type")
|
|
||||||
if len(cType) == 0 {
|
|
||||||
cType = contentType
|
|
||||||
}
|
|
||||||
|
|
||||||
c := &conn{
|
|
||||||
b: br,
|
|
||||||
cType: cType,
|
|
||||||
topic: topic,
|
|
||||||
queue: queue,
|
|
||||||
exit: make(chan bool),
|
|
||||||
ws: ws,
|
|
||||||
}
|
|
||||||
|
|
||||||
go c.writeLoop()
|
|
||||||
c.readLoop()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *brokerHandler) String() string {
|
|
||||||
return "broker"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
|
||||||
return &brokerHandler{
|
|
||||||
u: websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
ReadBufferSize: 1024,
|
|
||||||
WriteBufferSize: 1024,
|
|
||||||
},
|
|
||||||
opts: handler.NewOptions(opts...),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WithCors(cors map[string]bool, opts ...handler.Option) handler.Handler {
|
|
||||||
return &brokerHandler{
|
|
||||||
u: websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
|
||||||
if origin := r.Header.Get("Origin"); cors[origin] {
|
|
||||||
return true
|
|
||||||
} else if len(origin) > 0 && cors["*"] {
|
|
||||||
return true
|
|
||||||
} else if checkOrigin(r) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
},
|
|
||||||
ReadBufferSize: 1024,
|
|
||||||
WriteBufferSize: 1024,
|
|
||||||
},
|
|
||||||
opts: handler.NewOptions(opts...),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
// Package cloudevents provides a cloudevents handler publishing the event using the go-micro/client
|
|
||||||
package cloudevents
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"path"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/handler"
|
|
||||||
"github.com/micro/go-micro/util/ctx"
|
|
||||||
)
|
|
||||||
|
|
||||||
type event struct {
|
|
||||||
options handler.Options
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
Handler = "cloudevents"
|
|
||||||
versionRe = regexp.MustCompilePOSIX("^v[0-9]+$")
|
|
||||||
)
|
|
||||||
|
|
||||||
func eventName(parts []string) string {
|
|
||||||
return strings.Join(parts, ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
func evRoute(ns, p string) (string, string) {
|
|
||||||
p = path.Clean(p)
|
|
||||||
p = strings.TrimPrefix(p, "/")
|
|
||||||
|
|
||||||
if len(p) == 0 {
|
|
||||||
return ns, "event"
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := strings.Split(p, "/")
|
|
||||||
|
|
||||||
// no path
|
|
||||||
if len(parts) == 0 {
|
|
||||||
// topic: namespace
|
|
||||||
// action: event
|
|
||||||
return strings.Trim(ns, "."), "event"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Treat /v[0-9]+ as versioning
|
|
||||||
// /v1/foo/bar => topic: v1.foo action: bar
|
|
||||||
if len(parts) >= 2 && versionRe.Match([]byte(parts[0])) {
|
|
||||||
topic := ns + "." + strings.Join(parts[:2], ".")
|
|
||||||
action := eventName(parts[1:])
|
|
||||||
return topic, action
|
|
||||||
}
|
|
||||||
|
|
||||||
// /foo => topic: ns.foo action: foo
|
|
||||||
// /foo/bar => topic: ns.foo action: bar
|
|
||||||
topic := ns + "." + strings.Join(parts[:1], ".")
|
|
||||||
action := eventName(parts[1:])
|
|
||||||
|
|
||||||
return topic, action
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// request to topic:event
|
|
||||||
// create event
|
|
||||||
// publish to topic
|
|
||||||
topic, _ := evRoute(e.options.Namespace, r.URL.Path)
|
|
||||||
|
|
||||||
// create event
|
|
||||||
ev, err := FromRequest(r)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// get client
|
|
||||||
c := e.options.Service.Client()
|
|
||||||
|
|
||||||
// create publication
|
|
||||||
p := c.NewMessage(topic, ev)
|
|
||||||
|
|
||||||
// publish event
|
|
||||||
if err := c.Publish(ctx.FromRequest(r), p); err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *event) String() string {
|
|
||||||
return "cloudevents"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
|
||||||
return &event{
|
|
||||||
options: handler.NewOptions(opts...),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,282 +0,0 @@
|
|||||||
/*
|
|
||||||
* From: https://github.com/serverless/event-gateway/blob/master/event/event.go
|
|
||||||
* Modified: Strip to handler requirements
|
|
||||||
*
|
|
||||||
* Copyright 2017 Serverless, Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package cloudevents
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"mime"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"gopkg.in/go-playground/validator.v9"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// TransformationVersion is indicative of the revision of how Event Gateway transforms a request into CloudEvents format.
|
|
||||||
TransformationVersion = "0.1"
|
|
||||||
|
|
||||||
// CloudEventsVersion currently supported by Event Gateway
|
|
||||||
CloudEventsVersion = "0.1"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Event is a default event structure. All data that passes through the Event Gateway
|
|
||||||
// is formatted to a format defined CloudEvents v0.1 spec.
|
|
||||||
type Event struct {
|
|
||||||
EventType string `json:"eventType" validate:"required"`
|
|
||||||
EventTypeVersion string `json:"eventTypeVersion,omitempty"`
|
|
||||||
CloudEventsVersion string `json:"cloudEventsVersion" validate:"required"`
|
|
||||||
Source string `json:"source" validate:"uri,required"`
|
|
||||||
EventID string `json:"eventID" validate:"required"`
|
|
||||||
EventTime *time.Time `json:"eventTime,omitempty"`
|
|
||||||
SchemaURL string `json:"schemaURL,omitempty"`
|
|
||||||
Extensions map[string]interface{} `json:"extensions,omitempty"`
|
|
||||||
ContentType string `json:"contentType,omitempty"`
|
|
||||||
Data interface{} `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// New return new instance of Event.
|
|
||||||
func New(eventType string, mimeType string, payload interface{}) *Event {
|
|
||||||
now := time.Now()
|
|
||||||
|
|
||||||
event := &Event{
|
|
||||||
EventType: eventType,
|
|
||||||
CloudEventsVersion: CloudEventsVersion,
|
|
||||||
Source: "https://micro.mu",
|
|
||||||
EventID: uuid.New().String(),
|
|
||||||
EventTime: &now,
|
|
||||||
ContentType: mimeType,
|
|
||||||
Data: payload,
|
|
||||||
Extensions: map[string]interface{}{
|
|
||||||
"eventgateway": map[string]interface{}{
|
|
||||||
"transformed": "true",
|
|
||||||
"transformation-version": TransformationVersion,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
event.Data = normalizePayload(event.Data, event.ContentType)
|
|
||||||
return event
|
|
||||||
}
|
|
||||||
|
|
||||||
// FromRequest takes an HTTP request and returns an Event along with path. Most of the implementation
|
|
||||||
// is based on https://github.com/cloudevents/spec/blob/master/http-transport-binding.md.
|
|
||||||
// This function also supports legacy mode where event type is sent in Event header.
|
|
||||||
func FromRequest(r *http.Request) (*Event, error) {
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
|
||||||
mimeType, _, err := mime.ParseMediaType(contentType)
|
|
||||||
if err != nil {
|
|
||||||
if err.Error() != "mime: no media type" {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
mimeType = "application/octet-stream"
|
|
||||||
}
|
|
||||||
// Read request body
|
|
||||||
body := []byte{}
|
|
||||||
if r.Body != nil {
|
|
||||||
body, err = ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var event *Event
|
|
||||||
if mimeType == mimeCloudEventsJSON { // CloudEvents Structured Content Mode
|
|
||||||
return parseAsCloudEvent(mimeType, body)
|
|
||||||
} else if isCloudEventsBinaryContentMode(r.Header) { // CloudEvents Binary Content Mode
|
|
||||||
return parseAsCloudEventBinary(r.Header, body)
|
|
||||||
} else if isLegacyMode(r.Header) {
|
|
||||||
if mimeType == mimeJSON { // CloudEvent in Legacy Mode
|
|
||||||
event, err = parseAsCloudEvent(mimeType, body)
|
|
||||||
if err != nil {
|
|
||||||
return New(string(r.Header.Get("event")), mimeType, body), nil
|
|
||||||
}
|
|
||||||
return event, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return New(string(r.Header.Get("event")), mimeType, body), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return New("http.request", mimeJSON, newHTTPRequestData(r, body)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate Event struct
|
|
||||||
func (e *Event) Validate() error {
|
|
||||||
validate := validator.New()
|
|
||||||
err := validate.Struct(e)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("CloudEvent not valid: %v", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isLegacyMode(headers http.Header) bool {
|
|
||||||
if headers.Get("Event") != "" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func isCloudEventsBinaryContentMode(headers http.Header) bool {
|
|
||||||
if headers.Get("CE-EventType") != "" &&
|
|
||||||
headers.Get("CE-CloudEventsVersion") != "" &&
|
|
||||||
headers.Get("CE-Source") != "" &&
|
|
||||||
headers.Get("CE-EventID") != "" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseAsCloudEventBinary(headers http.Header, payload interface{}) (*Event, error) {
|
|
||||||
event := &Event{
|
|
||||||
EventType: headers.Get("CE-EventType"),
|
|
||||||
EventTypeVersion: headers.Get("CE-EventTypeVersion"),
|
|
||||||
CloudEventsVersion: headers.Get("CE-CloudEventsVersion"),
|
|
||||||
Source: headers.Get("CE-Source"),
|
|
||||||
EventID: headers.Get("CE-EventID"),
|
|
||||||
ContentType: headers.Get("Content-Type"),
|
|
||||||
Data: payload,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := event.Validate()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if headers.Get("CE-EventTime") != "" {
|
|
||||||
val, err := time.Parse(time.RFC3339, headers.Get("CE-EventTime"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
event.EventTime = &val
|
|
||||||
}
|
|
||||||
|
|
||||||
if val := headers.Get("CE-SchemaURL"); len(val) > 0 {
|
|
||||||
event.SchemaURL = val
|
|
||||||
}
|
|
||||||
|
|
||||||
event.Extensions = map[string]interface{}{}
|
|
||||||
for key, val := range flatten(headers) {
|
|
||||||
if strings.HasPrefix(key, "Ce-X-") {
|
|
||||||
key = strings.TrimLeft(key, "Ce-X-")
|
|
||||||
// Make first character lowercase
|
|
||||||
runes := []rune(key)
|
|
||||||
runes[0] = unicode.ToLower(runes[0])
|
|
||||||
event.Extensions[string(runes)] = val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
event.Data = normalizePayload(event.Data, event.ContentType)
|
|
||||||
return event, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func flatten(h http.Header) map[string]string {
|
|
||||||
headers := map[string]string{}
|
|
||||||
for key, header := range h {
|
|
||||||
headers[key] = header[0]
|
|
||||||
if len(header) > 1 {
|
|
||||||
headers[key] = strings.Join(header, ", ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return headers
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseAsCloudEvent(mime string, payload interface{}) (*Event, error) {
|
|
||||||
body, ok := payload.([]byte)
|
|
||||||
if ok {
|
|
||||||
event := &Event{}
|
|
||||||
err := json.Unmarshal(body, event)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = event.Validate()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
event.Data = normalizePayload(event.Data, event.ContentType)
|
|
||||||
return event, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("couldn't cast to []byte")
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
mimeJSON = "application/json"
|
|
||||||
mimeFormMultipart = "multipart/form-data"
|
|
||||||
mimeFormURLEncoded = "application/x-www-form-urlencoded"
|
|
||||||
mimeCloudEventsJSON = "application/cloudevents+json"
|
|
||||||
)
|
|
||||||
|
|
||||||
// normalizePayload takes anything, checks if it's []byte array and depending on provided mime
|
|
||||||
// type converts it to either string or map[string]interface to avoid having base64 string after
|
|
||||||
// JSON marshaling.
|
|
||||||
func normalizePayload(payload interface{}, mime string) interface{} {
|
|
||||||
if bytePayload, ok := payload.([]byte); ok && len(bytePayload) > 0 {
|
|
||||||
switch {
|
|
||||||
case mime == mimeJSON || strings.HasSuffix(mime, "+json"):
|
|
||||||
var result map[string]interface{}
|
|
||||||
err := json.Unmarshal(bytePayload, &result)
|
|
||||||
if err != nil {
|
|
||||||
return payload
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
case strings.HasPrefix(mime, mimeFormMultipart), mime == mimeFormURLEncoded:
|
|
||||||
return string(bytePayload)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return payload
|
|
||||||
}
|
|
||||||
|
|
||||||
// HTTPRequestData is a event schema used for sending events to HTTP subscriptions.
|
|
||||||
type HTTPRequestData struct {
|
|
||||||
Headers map[string]string `json:"headers"`
|
|
||||||
Query map[string][]string `json:"query"`
|
|
||||||
Body interface{} `json:"body"`
|
|
||||||
Host string `json:"host"`
|
|
||||||
Path string `json:"path"`
|
|
||||||
Method string `json:"method"`
|
|
||||||
Params map[string]string `json:"params"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHTTPRequestData returns a new instance of HTTPRequestData
|
|
||||||
func newHTTPRequestData(r *http.Request, eventData interface{}) *HTTPRequestData {
|
|
||||||
req := &HTTPRequestData{
|
|
||||||
Headers: flatten(r.Header),
|
|
||||||
Query: r.URL.Query(),
|
|
||||||
Body: eventData,
|
|
||||||
Host: r.Host,
|
|
||||||
Path: r.URL.Path,
|
|
||||||
Method: r.Method,
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Body = normalizePayload(req.Body, r.Header.Get("content-type"))
|
|
||||||
return req
|
|
||||||
}
|
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
package event
|
package event
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -11,13 +11,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/micro/go-micro/api/handler"
|
"github.com/oxtoacart/bpool"
|
||||||
proto "github.com/micro/go-micro/api/proto"
|
"github.com/unistack-org/micro/v3/api/handler"
|
||||||
"github.com/micro/go-micro/util/ctx"
|
proto "github.com/unistack-org/micro/v3/api/proto"
|
||||||
|
"github.com/unistack-org/micro/v3/util/ctx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
||||||
)
|
)
|
||||||
|
|
||||||
type event struct {
|
type event struct {
|
||||||
options handler.Options
|
opts handler.Options
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -63,11 +68,18 @@ func evRoute(ns, p string) (string, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
bsize := handler.DefaultMaxRecvSize
|
||||||
|
if e.opts.MaxRecvSize > 0 {
|
||||||
|
bsize = e.opts.MaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
||||||
|
|
||||||
// request to topic:event
|
// request to topic:event
|
||||||
// create event
|
// create event
|
||||||
// publish to topic
|
// publish to topic
|
||||||
|
|
||||||
topic, action := evRoute(e.options.Namespace, r.URL.Path)
|
topic, action := evRoute(e.opts.Namespace, r.URL.Path)
|
||||||
|
|
||||||
// create event
|
// create event
|
||||||
ev := &proto.Event{
|
ev := &proto.Event{
|
||||||
@@ -91,15 +103,22 @@ func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set body
|
// set body
|
||||||
b, err := ioutil.ReadAll(r.Body)
|
if r.Method == "GET" {
|
||||||
if err != nil {
|
bytes, _ := json.Marshal(r.URL.Query())
|
||||||
http.Error(w, err.Error(), 500)
|
ev.Data = string(bytes)
|
||||||
return
|
} else {
|
||||||
|
// Read body
|
||||||
|
buf := bufferPool.Get()
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
if _, err := buf.ReadFrom(r.Body); err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ev.Data = buf.String()
|
||||||
}
|
}
|
||||||
ev.Data = string(b)
|
|
||||||
|
|
||||||
// get client
|
// get client
|
||||||
c := e.options.Service.Client()
|
c := e.opts.Client
|
||||||
|
|
||||||
// create publication
|
// create publication
|
||||||
p := c.NewMessage(topic, ev)
|
p := c.NewMessage(topic, ev)
|
||||||
@@ -117,6 +136,6 @@ func (e *event) String() string {
|
|||||||
|
|
||||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
return &event{
|
return &event{
|
||||||
options: handler.NewOptions(opts...),
|
opts: handler.NewOptions(opts...),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
// Package file serves file relative to the current directory
|
|
||||||
package file
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Handler struct{}
|
|
||||||
|
|
||||||
func (h *Handler) Serve(w http.ResponseWriter, r *http.Request) {
|
|
||||||
http.ServeFile(w, r, "."+r.URL.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) String() string {
|
|
||||||
return "file"
|
|
||||||
}
|
|
||||||
@@ -4,13 +4,14 @@ package http
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api"
|
"github.com/unistack-org/micro/v3/api"
|
||||||
"github.com/micro/go-micro/api/handler"
|
"github.com/unistack-org/micro/v3/api/handler"
|
||||||
"github.com/micro/go-micro/client/selector"
|
"github.com/unistack-org/micro/v3/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -20,7 +21,7 @@ const (
|
|||||||
type httpHandler struct {
|
type httpHandler struct {
|
||||||
options handler.Options
|
options handler.Options
|
||||||
|
|
||||||
// set with different initialiser
|
// set with different initializer
|
||||||
s *api.Service
|
s *api.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,16 +65,20 @@ func (h *httpHandler) getService(r *http.Request) (string, error) {
|
|||||||
return "", errors.New("no route found")
|
return "", errors.New("no route found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a random selector
|
if len(service.Services) == 0 {
|
||||||
next := selector.Random(service.Services)
|
return "", errors.New("no route found")
|
||||||
|
|
||||||
// get the next node
|
|
||||||
s, err := next()
|
|
||||||
if err != nil {
|
|
||||||
return "", nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("http://%s", s.Address), nil
|
// get the nodes for this service
|
||||||
|
nodes := make([]*registry.Node, 0, len(service.Services))
|
||||||
|
for _, srv := range service.Services {
|
||||||
|
nodes = append(nodes, srv.Nodes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// select a random node
|
||||||
|
node := nodes[rand.Int()%len(nodes)]
|
||||||
|
|
||||||
|
return fmt.Sprintf("http://%s", node.Address), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpHandler) String() string {
|
func (h *httpHandler) String() string {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// +build ignore
|
||||||
|
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -6,17 +8,17 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/handler"
|
"github.com/unistack-org/micro/v3/api/handler"
|
||||||
"github.com/micro/go-micro/api/router"
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
regRouter "github.com/micro/go-micro/api/router/registry"
|
"github.com/unistack-org/micro/v3/api/resolver/vpath"
|
||||||
"github.com/micro/go-micro/config/cmd"
|
"github.com/unistack-org/micro/v3/api/router"
|
||||||
"github.com/micro/go-micro/registry"
|
regRouter "github.com/unistack-org/micro/v3/api/router/registry"
|
||||||
"github.com/micro/go-micro/registry/memory"
|
"github.com/unistack-org/micro/v3/registry"
|
||||||
|
"github.com/unistack-org/micro/v3/registry/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testHttp(t *testing.T, path, service, ns string) {
|
func testHttp(t *testing.T, path, service, ns string) {
|
||||||
r := memory.NewRegistry()
|
r := memory.NewRegistry()
|
||||||
cmd.DefaultCmd = cmd.NewCmd(cmd.Registry(&r))
|
|
||||||
|
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -56,7 +58,10 @@ func testHttp(t *testing.T, path, service, ns string) {
|
|||||||
// initialise the handler
|
// initialise the handler
|
||||||
rt := regRouter.NewRouter(
|
rt := regRouter.NewRouter(
|
||||||
router.WithHandler("http"),
|
router.WithHandler("http"),
|
||||||
router.WithNamespace(ns),
|
router.WithRegistry(r),
|
||||||
|
router.WithResolver(vpath.NewResolver(
|
||||||
|
resolver.WithServicePrefix(ns),
|
||||||
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
p := NewHandler(handler.WithRouter(rt))
|
p := NewHandler(handler.WithRouter(rt))
|
||||||
@@ -117,6 +122,8 @@ func TestHttpHandler(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range testData {
|
for _, d := range testData {
|
||||||
testHttp(t, d.path, d.service, d.namespace)
|
t.Run(d.service, func(t *testing.T) {
|
||||||
|
testHttp(t, d.path, d.service, d.namespace)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,19 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/micro/go-micro"
|
"github.com/unistack-org/micro/v3/api/router"
|
||||||
"github.com/micro/go-micro/api/router"
|
"github.com/unistack-org/micro/v3/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultMaxRecvSize int64 = 1024 * 1024 * 100 // 10Mb
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Namespace string
|
MaxRecvSize int64
|
||||||
Router router.Router
|
Namespace string
|
||||||
Service micro.Service
|
Router router.Router
|
||||||
|
Client client.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(o *Options)
|
type Option func(o *Options)
|
||||||
@@ -20,16 +25,15 @@ func NewOptions(opts ...Option) Options {
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create service if its blank
|
|
||||||
if options.Service == nil {
|
|
||||||
WithService(micro.NewService())(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
// set namespace if blank
|
// set namespace if blank
|
||||||
if len(options.Namespace) == 0 {
|
if len(options.Namespace) == 0 {
|
||||||
WithNamespace("go.micro.api")(&options)
|
WithNamespace("go.micro.api")(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.MaxRecvSize == 0 {
|
||||||
|
options.MaxRecvSize = DefaultMaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,9 +51,15 @@ func WithRouter(r router.Router) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithService specifies a micro.Service
|
func WithClient(c client.Client) Option {
|
||||||
func WithService(s micro.Service) Option {
|
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Service = s
|
o.Client = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMaxRecvSize specifies max body size
|
||||||
|
func WithMaxRecvSize(size int64) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.MaxRecvSize = size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,211 +0,0 @@
|
|||||||
// Package registry is a go-micro/registry handler
|
|
||||||
package registry
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
"github.com/micro/go-micro/api/handler"
|
|
||||||
"github.com/micro/go-micro/registry"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
Handler = "registry"
|
|
||||||
|
|
||||||
pingTime = (readDeadline * 9) / 10
|
|
||||||
readLimit = 16384
|
|
||||||
readDeadline = 60 * time.Second
|
|
||||||
writeDeadline = 10 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
type registryHandler struct {
|
|
||||||
opts handler.Options
|
|
||||||
reg registry.Registry
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rh *registryHandler) add(w http.ResponseWriter, r *http.Request) {
|
|
||||||
r.ParseForm()
|
|
||||||
b, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var opts []registry.RegisterOption
|
|
||||||
|
|
||||||
// parse ttl
|
|
||||||
if ttl := r.Form.Get("ttl"); len(ttl) > 0 {
|
|
||||||
d, err := time.ParseDuration(ttl)
|
|
||||||
if err == nil {
|
|
||||||
opts = append(opts, registry.RegisterTTL(d))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var service *registry.Service
|
|
||||||
err = json.Unmarshal(b, &service)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = rh.reg.Register(service, opts...)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rh *registryHandler) del(w http.ResponseWriter, r *http.Request) {
|
|
||||||
r.ParseForm()
|
|
||||||
b, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var service *registry.Service
|
|
||||||
err = json.Unmarshal(b, &service)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = rh.reg.Deregister(service)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rh *registryHandler) get(w http.ResponseWriter, r *http.Request) {
|
|
||||||
r.ParseForm()
|
|
||||||
service := r.Form.Get("service")
|
|
||||||
|
|
||||||
var s []*registry.Service
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if len(service) == 0 {
|
|
||||||
//
|
|
||||||
upgrade := r.Header.Get("Upgrade")
|
|
||||||
connect := r.Header.Get("Connection")
|
|
||||||
|
|
||||||
// watch if websockets
|
|
||||||
if upgrade == "websocket" && connect == "Upgrade" {
|
|
||||||
rw, err := rh.reg.Watch()
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
watch(rw, w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise list services
|
|
||||||
s, err = rh.reg.ListServices()
|
|
||||||
} else {
|
|
||||||
s, err = rh.reg.GetService(service)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if s == nil || (len(service) > 0 && (len(s) == 0 || len(s[0].Name) == 0)) {
|
|
||||||
http.Error(w, "Service not found", 404)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := json.Marshal(s)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
|
|
||||||
w.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ping(ws *websocket.Conn, exit chan bool) {
|
|
||||||
ticker := time.NewTicker(pingTime)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
ws.SetWriteDeadline(time.Now().Add(writeDeadline))
|
|
||||||
err := ws.WriteMessage(websocket.PingMessage, []byte{})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case <-exit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func watch(rw registry.Watcher, w http.ResponseWriter, r *http.Request) {
|
|
||||||
upgrader := websocket.Upgrader{
|
|
||||||
ReadBufferSize: 1024,
|
|
||||||
WriteBufferSize: 1024,
|
|
||||||
}
|
|
||||||
|
|
||||||
ws, err := upgrader.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// we need an exit chan
|
|
||||||
exit := make(chan bool)
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
close(exit)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// ping the socket
|
|
||||||
go ping(ws, exit)
|
|
||||||
|
|
||||||
for {
|
|
||||||
// get next result
|
|
||||||
r, err := rw.Next()
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// write to client
|
|
||||||
ws.SetWriteDeadline(time.Now().Add(writeDeadline))
|
|
||||||
if err := ws.WriteJSON(r); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rh *registryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.Method {
|
|
||||||
case "GET":
|
|
||||||
rh.get(w, r)
|
|
||||||
case "POST":
|
|
||||||
rh.add(w, r)
|
|
||||||
case "DELETE":
|
|
||||||
rh.del(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rh *registryHandler) String() string {
|
|
||||||
return "registry"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
|
||||||
options := handler.NewOptions(opts...)
|
|
||||||
|
|
||||||
return ®istryHandler{
|
|
||||||
opts: options,
|
|
||||||
reg: options.Service.Client().Options().Registry,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,23 +4,25 @@ package rpc
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/joncalhoun/qson"
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
||||||
"github.com/micro/go-micro/api"
|
"github.com/oxtoacart/bpool"
|
||||||
"github.com/micro/go-micro/api/handler"
|
jsonrpc "github.com/unistack-org/micro-codec-jsonrpc"
|
||||||
proto "github.com/micro/go-micro/api/internal/proto"
|
protorpc "github.com/unistack-org/micro-codec-protorpc"
|
||||||
"github.com/micro/go-micro/client"
|
"github.com/unistack-org/micro/v3/api"
|
||||||
"github.com/micro/go-micro/client/selector"
|
"github.com/unistack-org/micro/v3/api/handler"
|
||||||
"github.com/micro/go-micro/codec"
|
"github.com/unistack-org/micro/v3/api/internal/proto"
|
||||||
"github.com/micro/go-micro/codec/jsonrpc"
|
"github.com/unistack-org/micro/v3/client"
|
||||||
"github.com/micro/go-micro/codec/protorpc"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"github.com/micro/go-micro/errors"
|
"github.com/unistack-org/micro/v3/errors"
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/micro/go-micro/util/ctx"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
|
"github.com/unistack-org/micro/v3/util/ctx"
|
||||||
|
"github.com/unistack-org/micro/v3/util/qson"
|
||||||
|
"github.com/unistack-org/micro/v3/util/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -44,6 +46,8 @@ var (
|
|||||||
"application/proto-rpc",
|
"application/proto-rpc",
|
||||||
"application/octet-stream",
|
"application/octet-stream",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
||||||
)
|
)
|
||||||
|
|
||||||
type rpcHandler struct {
|
type rpcHandler struct {
|
||||||
@@ -59,15 +63,14 @@ func (b *buffer) Write(_ []byte) (int, error) {
|
|||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// strategy is a hack for selection
|
|
||||||
func strategy(services []*registry.Service) selector.Strategy {
|
|
||||||
return func(_ []*registry.Service) selector.Next {
|
|
||||||
// ignore input to this function, use services above
|
|
||||||
return selector.Random(services)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
bsize := handler.DefaultMaxRecvSize
|
||||||
|
if h.opts.MaxRecvSize > 0 {
|
||||||
|
bsize = h.opts.MaxRecvSize
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
||||||
|
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
var service *api.Service
|
var service *api.Service
|
||||||
|
|
||||||
@@ -88,12 +91,6 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// only allow post when we have the router
|
|
||||||
if r.Method != "GET" && (h.opts.Router != nil && r.Method != "POST") {
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ct := r.Header.Get("Content-Type")
|
ct := r.Header.Get("Content-Type")
|
||||||
|
|
||||||
// Strip charset from Content-Type (like `application/json; charset=UTF-8`)
|
// Strip charset from Content-Type (like `application/json; charset=UTF-8`)
|
||||||
@@ -102,11 +99,23 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// micro client
|
// micro client
|
||||||
c := h.opts.Service.Client()
|
c := h.opts.Client
|
||||||
|
|
||||||
// create strategy
|
// create context
|
||||||
so := selector.WithStrategy(strategy(service.Services))
|
cx := ctx.FromRequest(r)
|
||||||
|
|
||||||
|
// set merged context to request
|
||||||
|
*r = *r.Clone(cx)
|
||||||
|
// if stream we currently only support json
|
||||||
|
if isStream(r, service) {
|
||||||
|
serveWebsocket(cx, w, r, service, c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create custom router
|
||||||
|
callOpt := client.WithRouter(router.New(service.Services))
|
||||||
|
|
||||||
|
// walk the standard call path
|
||||||
// get payload
|
// get payload
|
||||||
br, err := requestPayload(r)
|
br, err := requestPayload(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -114,9 +123,6 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// create context
|
|
||||||
cx := ctx.FromRequest(r)
|
|
||||||
|
|
||||||
var rsp []byte
|
var rsp []byte
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
@@ -139,13 +145,18 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// make the call
|
// make the call
|
||||||
if err := c.Call(cx, req, response, client.WithSelectOption(so)); err != nil {
|
if err := c.Call(cx, req, response, callOpt); err != nil {
|
||||||
writeError(w, r, err)
|
writeError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// marshall response
|
// marshall response
|
||||||
rsp, _ = response.Marshal()
|
rsp, err = response.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// if json codec is not present set to json
|
// if json codec is not present set to json
|
||||||
if !hasCodec(ct, jsonCodecs) {
|
if !hasCodec(ct, jsonCodecs) {
|
||||||
@@ -168,15 +179,18 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
&request,
|
&request,
|
||||||
client.WithContentType(ct),
|
client.WithContentType(ct),
|
||||||
)
|
)
|
||||||
|
|
||||||
// make the call
|
// make the call
|
||||||
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
|
if err := c.Call(cx, req, &response, callOpt); err != nil {
|
||||||
writeError(w, r, err)
|
writeError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// marshall response
|
// marshall response
|
||||||
rsp, _ = response.MarshalJSON()
|
rsp, err = response.MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the response
|
// write the response
|
||||||
@@ -200,49 +214,205 @@ func hasCodec(ct string, codecs []string) bool {
|
|||||||
// If the request is a GET the query string parameters are extracted and marshaled to JSON and the raw bytes are returned.
|
// If the request is a GET the query string parameters are extracted and marshaled to JSON and the raw bytes are returned.
|
||||||
// If the request method is a POST the request body is read and returned
|
// If the request method is a POST the request body is read and returned
|
||||||
func requestPayload(r *http.Request) ([]byte, error) {
|
func requestPayload(r *http.Request) ([]byte, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
// we have to decode json-rpc and proto-rpc because we suck
|
// we have to decode json-rpc and proto-rpc because we suck
|
||||||
// well actually because there's no proxy codec right now
|
// well actually because there's no proxy codec right now
|
||||||
switch r.Header.Get("Content-Type") {
|
|
||||||
case "application/json-rpc":
|
ct := r.Header.Get("Content-Type")
|
||||||
|
switch {
|
||||||
|
case strings.Contains(ct, "application/json-rpc"):
|
||||||
msg := codec.Message{
|
msg := codec.Message{
|
||||||
Type: codec.Request,
|
Type: codec.Request,
|
||||||
Header: make(map[string]string),
|
Header: metadata.New(0),
|
||||||
}
|
}
|
||||||
c := jsonrpc.NewCodec(&buffer{r.Body})
|
c := jsonrpc.NewCodec(&buffer{r.Body})
|
||||||
if err := c.ReadHeader(&msg, codec.Request); err != nil {
|
if err = c.ReadHeader(&msg, codec.Request); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var raw json.RawMessage
|
var raw json.RawMessage
|
||||||
if err := c.ReadBody(&raw); err != nil {
|
if err = c.ReadBody(&raw); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return ([]byte)(raw), nil
|
return ([]byte)(raw), nil
|
||||||
case "application/proto-rpc", "application/octet-stream":
|
case strings.Contains(ct, "application/proto-rpc"), strings.Contains(ct, "application/octet-stream"):
|
||||||
msg := codec.Message{
|
msg := codec.Message{
|
||||||
Type: codec.Request,
|
Type: codec.Request,
|
||||||
Header: make(map[string]string),
|
Header: metadata.New(0),
|
||||||
}
|
}
|
||||||
c := protorpc.NewCodec(&buffer{r.Body})
|
c := protorpc.NewCodec(&buffer{r.Body})
|
||||||
if err := c.ReadHeader(&msg, codec.Request); err != nil {
|
if err = c.ReadHeader(&msg, codec.Request); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var raw proto.Message
|
var raw proto.Message
|
||||||
if err := c.ReadBody(&raw); err != nil {
|
if err = c.ReadBody(&raw); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
b, _ := raw.Marshal()
|
return raw.Marshal()
|
||||||
return b, nil
|
case strings.Contains(ct, "application/www-x-form-urlencoded"):
|
||||||
|
if err = r.ParseForm(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate a new set of values from the form
|
||||||
|
vals := make(map[string]string, len(r.Form))
|
||||||
|
for k, v := range r.Form {
|
||||||
|
vals[k] = strings.Join(v, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshal
|
||||||
|
return json.Marshal(vals)
|
||||||
|
// TODO: application/grpc
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise as per usual
|
// otherwise as per usual
|
||||||
|
ctx := r.Context()
|
||||||
|
// dont user metadata.FromContext as it mangles names
|
||||||
|
md, ok := metadata.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
md = metadata.New(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocate maximum
|
||||||
|
matches := make(map[string]interface{}, len(md))
|
||||||
|
bodydst := ""
|
||||||
|
|
||||||
|
// get fields from url path
|
||||||
|
for k, v := range md {
|
||||||
|
k = strings.ToLower(k)
|
||||||
|
// filter own keys
|
||||||
|
if strings.HasPrefix(k, "x-api-field-") {
|
||||||
|
matches[strings.TrimPrefix(k, "x-api-field-")] = v
|
||||||
|
delete(md, k)
|
||||||
|
} else if k == "x-api-body" {
|
||||||
|
bodydst = v
|
||||||
|
delete(md, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// map of all fields
|
||||||
|
req := make(map[string]interface{}, len(md))
|
||||||
|
|
||||||
|
// get fields from url values
|
||||||
|
if len(r.URL.RawQuery) > 0 {
|
||||||
|
umd := make(map[string]interface{})
|
||||||
|
err = qson.Unmarshal(&umd, r.URL.RawQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for k, v := range umd {
|
||||||
|
matches[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// restore context without fields
|
||||||
|
*r = *r.Clone(metadata.NewContext(ctx, md))
|
||||||
|
|
||||||
|
for k, v := range matches {
|
||||||
|
ps := strings.Split(k, ".")
|
||||||
|
if len(ps) == 1 {
|
||||||
|
req[k] = v
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
em := make(map[string]interface{})
|
||||||
|
em[ps[len(ps)-1]] = v
|
||||||
|
for i := len(ps) - 2; i > 0; i-- {
|
||||||
|
nm := make(map[string]interface{})
|
||||||
|
nm[ps[i]] = em
|
||||||
|
em = nm
|
||||||
|
}
|
||||||
|
if vm, ok := req[ps[0]]; ok {
|
||||||
|
// nested map
|
||||||
|
nm := vm.(map[string]interface{})
|
||||||
|
for vk, vv := range em {
|
||||||
|
nm[vk] = vv
|
||||||
|
}
|
||||||
|
req[ps[0]] = nm
|
||||||
|
} else {
|
||||||
|
req[ps[0]] = em
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pathbuf := []byte("{}")
|
||||||
|
if len(req) > 0 {
|
||||||
|
pathbuf, err = json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
urlbuf := []byte("{}")
|
||||||
|
out, err := jsonpatch.MergeMergePatches(urlbuf, pathbuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
if len(r.URL.RawQuery) > 0 {
|
// empty response
|
||||||
return qson.ToJSON(r.URL.RawQuery)
|
if strings.Contains(ct, "application/json") && string(out) == "{}" {
|
||||||
|
return out, nil
|
||||||
|
} else if string(out) == "{}" && !strings.Contains(ct, "application/json") {
|
||||||
|
return []byte{}, nil
|
||||||
}
|
}
|
||||||
case "PATCH", "POST":
|
return out, nil
|
||||||
return ioutil.ReadAll(r.Body)
|
case "PATCH", "POST", "PUT", "DELETE":
|
||||||
|
bodybuf := []byte("{}")
|
||||||
|
buf := bufferPool.Get()
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
if _, err := buf.ReadFrom(r.Body); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if b := buf.Bytes(); len(b) > 0 {
|
||||||
|
bodybuf = b
|
||||||
|
}
|
||||||
|
if bodydst == "" || bodydst == "*" {
|
||||||
|
if out, err = jsonpatch.MergeMergePatches(out, bodybuf); err == nil {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var jsonbody map[string]interface{}
|
||||||
|
if json.Valid(bodybuf) {
|
||||||
|
if err = json.Unmarshal(bodybuf, &jsonbody); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dstmap := make(map[string]interface{})
|
||||||
|
ps := strings.Split(bodydst, ".")
|
||||||
|
if len(ps) == 1 {
|
||||||
|
if jsonbody != nil {
|
||||||
|
dstmap[ps[0]] = jsonbody
|
||||||
|
} else {
|
||||||
|
// old unexpected behaviour
|
||||||
|
dstmap[ps[0]] = bodybuf
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
em := make(map[string]interface{})
|
||||||
|
if jsonbody != nil {
|
||||||
|
em[ps[len(ps)-1]] = jsonbody
|
||||||
|
} else {
|
||||||
|
// old unexpected behaviour
|
||||||
|
em[ps[len(ps)-1]] = bodybuf
|
||||||
|
}
|
||||||
|
for i := len(ps) - 2; i > 0; i-- {
|
||||||
|
nm := make(map[string]interface{})
|
||||||
|
nm[ps[i]] = em
|
||||||
|
em = nm
|
||||||
|
}
|
||||||
|
dstmap[ps[0]] = em
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyout, err := json.Marshal(dstmap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if out, err = jsonpatch.MergeMergePatches(out, bodyout); err == nil {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//fallback to previous unknown behaviour
|
||||||
|
return bodybuf, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return []byte{}, nil
|
return []byte{}, nil
|
||||||
@@ -274,7 +444,12 @@ func writeError(w http.ResponseWriter, r *http.Request, err error) {
|
|||||||
w.Header().Set("grpc-message", ce.Detail)
|
w.Header().Set("grpc-message", ce.Detail)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write([]byte(ce.Error()))
|
_, werr := w.Write([]byte(ce.Error()))
|
||||||
|
if werr != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(werr.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) {
|
func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) {
|
||||||
@@ -289,8 +464,19 @@ func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) {
|
|||||||
w.Header().Set("grpc-message", "")
|
w.Header().Set("grpc-message", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write 204 status if rsp is nil
|
||||||
|
if len(rsp) == 0 {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
// write response
|
// write response
|
||||||
w.Write(rsp)
|
_, err := w.Write(rsp)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package rpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
go_api "github.com/unistack-org/micro/v3/api/proto"
|
||||||
"github.com/micro/go-micro/api/proto"
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRequestPayloadFromRequest(t *testing.T) {
|
func TestRequestPayloadFromRequest(t *testing.T) {
|
||||||
@@ -22,11 +22,28 @@ func TestRequestPayloadFromRequest(t *testing.T) {
|
|||||||
t.Fatal("Failed to marshal proto", err)
|
t.Fatal("Failed to marshal proto", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(protoEvent)
|
jsonBytes, err := jsonpb.Marshal(&protoEvent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Failed to marshal proto to JSON ", err)
|
t.Fatal("Failed to marshal proto to JSON ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jsonUrlBytes := []byte(`{"key1":"val1","key2":"val2","name":"Test"}`)
|
||||||
|
|
||||||
|
t.Run("extracting a json from a POST request with url params", func(t *testing.T) {
|
||||||
|
r, err := http.NewRequest("POST", "http://localhost/my/path?key1=val1&key2=val2", bytes.NewReader(jsonBytes))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to created http.Request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
extByte, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to extract payload from request: %v", err)
|
||||||
|
}
|
||||||
|
if string(extByte) != string(jsonUrlBytes) {
|
||||||
|
t.Fatalf("Expected %v and %v to match", string(extByte), jsonUrlBytes)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("extracting a proto from a POST request", func(t *testing.T) {
|
t.Run("extracting a proto from a POST request", func(t *testing.T) {
|
||||||
r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(protoBytes))
|
r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(protoBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
263
api/handler/rpc/stream.go
Normal file
263
api/handler/rpc/stream.go
Normal file
@@ -0,0 +1,263 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gobwas/httphead"
|
||||||
|
"github.com/gobwas/ws"
|
||||||
|
"github.com/gobwas/ws/wsutil"
|
||||||
|
raw "github.com/unistack-org/micro-codec-bytes"
|
||||||
|
"github.com/unistack-org/micro/v3/api"
|
||||||
|
"github.com/unistack-org/micro/v3/client"
|
||||||
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
|
"github.com/unistack-org/micro/v3/util/router"
|
||||||
|
)
|
||||||
|
|
||||||
|
// serveWebsocket will stream rpc back over websockets assuming json
|
||||||
|
func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, service *api.Service, c client.Client) {
|
||||||
|
var op ws.OpCode
|
||||||
|
|
||||||
|
ct := r.Header.Get("Content-Type")
|
||||||
|
// Strip charset from Content-Type (like `application/json; charset=UTF-8`)
|
||||||
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
|
ct = ct[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
// check proto from request
|
||||||
|
switch ct {
|
||||||
|
case "application/json":
|
||||||
|
op = ws.OpText
|
||||||
|
default:
|
||||||
|
op = ws.OpBinary
|
||||||
|
}
|
||||||
|
|
||||||
|
hdr := make(http.Header)
|
||||||
|
if proto, ok := r.Header["Sec-WebSocket-Protocol"]; ok {
|
||||||
|
for _, p := range proto {
|
||||||
|
switch p {
|
||||||
|
case "binary":
|
||||||
|
hdr["Sec-WebSocket-Protocol"] = []string{"binary"}
|
||||||
|
op = ws.OpBinary
|
||||||
|
default:
|
||||||
|
op = ws.OpBinary
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
payload, err := requestPayload(r)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
upgrader := ws.HTTPUpgrader{Timeout: 5 * time.Second,
|
||||||
|
Protocol: func(proto string) bool {
|
||||||
|
if strings.Contains(proto, "binary") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// fallback to support all protocols now
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
Extension: func(httphead.Option) bool {
|
||||||
|
// disable extensions for compatibility
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
Header: hdr,
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, rw, _, err := upgrader.Upgrade(r, w)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err := conn.Close(); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var request interface{}
|
||||||
|
if !bytes.Equal(payload, []byte(`{}`)) {
|
||||||
|
switch ct {
|
||||||
|
case "application/json", "":
|
||||||
|
m := json.RawMessage(payload)
|
||||||
|
request = &m
|
||||||
|
default:
|
||||||
|
request = &raw.Frame{Data: payload}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// we always need to set content type for message
|
||||||
|
if ct == "" {
|
||||||
|
ct = "application/json"
|
||||||
|
}
|
||||||
|
req := c.NewRequest(
|
||||||
|
service.Name,
|
||||||
|
service.Endpoint.Name,
|
||||||
|
request,
|
||||||
|
client.WithContentType(ct),
|
||||||
|
client.StreamingRequest(),
|
||||||
|
)
|
||||||
|
|
||||||
|
// create custom router
|
||||||
|
callOpt := client.WithRouter(router.New(service.Services))
|
||||||
|
|
||||||
|
// create a new stream
|
||||||
|
stream, err := c.Stream(ctx, req, callOpt)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if request != nil {
|
||||||
|
if err = stream.Send(request); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go writeLoop(rw, stream)
|
||||||
|
|
||||||
|
rsp := stream.Response()
|
||||||
|
|
||||||
|
// receive from stream and send to client
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-stream.Context().Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
// read backend response body
|
||||||
|
buf, err := rsp.Read()
|
||||||
|
if err != nil {
|
||||||
|
// wants to avoid import grpc/status.Status
|
||||||
|
if strings.Contains(err.Error(), "context canceled") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the response
|
||||||
|
if err := wsutil.WriteServerMessage(rw, op, buf); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = rw.Flush(); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeLoop
|
||||||
|
func writeLoop(rw io.ReadWriter, stream client.Stream) {
|
||||||
|
// close stream when done
|
||||||
|
defer stream.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stream.Context().Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
buf, op, err := wsutil.ReadClientData(rw)
|
||||||
|
if err != nil {
|
||||||
|
if wserr, ok := err.(wsutil.ClosedError); ok {
|
||||||
|
switch wserr.Code {
|
||||||
|
case ws.StatusGoingAway:
|
||||||
|
// this happens when user leave the page
|
||||||
|
return
|
||||||
|
case ws.StatusNormalClosure, ws.StatusNoStatusRcvd:
|
||||||
|
// this happens when user close ws connection, or we don't get any status
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch op {
|
||||||
|
default:
|
||||||
|
// not relevant
|
||||||
|
continue
|
||||||
|
case ws.OpText, ws.OpBinary:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// send to backend
|
||||||
|
// default to trying json
|
||||||
|
// if the extracted payload isn't empty lets use it
|
||||||
|
request := &raw.Frame{Data: buf}
|
||||||
|
if err := stream.Send(request); err != nil {
|
||||||
|
if logger.V(logger.ErrorLevel) {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isStream(r *http.Request, srv *api.Service) bool {
|
||||||
|
// check if it's a web socket
|
||||||
|
if !isWebSocket(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// check if the endpoint supports streaming
|
||||||
|
for _, service := range srv.Services {
|
||||||
|
for _, ep := range service.Endpoints {
|
||||||
|
// skip if it doesn't match the name
|
||||||
|
if ep.Name != srv.Endpoint.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// matched if the name
|
||||||
|
if v := ep.Metadata["stream"]; v == "true" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isWebSocket(r *http.Request) bool {
|
||||||
|
contains := func(key, val string) bool {
|
||||||
|
vv := strings.Split(r.Header.Get(key), ",")
|
||||||
|
for _, v := range vv {
|
||||||
|
if val == strings.ToLower(strings.TrimSpace(v)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if contains("Connection", "upgrade") && contains("Upgrade", "websocket") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
// Package udp reads and write from a udp connection
|
|
||||||
package udp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Handler struct{}
|
|
||||||
|
|
||||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
c, err := net.Dial("udp", r.Host)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
go io.Copy(c, r.Body)
|
|
||||||
// write response
|
|
||||||
io.Copy(w, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) String() string {
|
|
||||||
return "udp"
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
// Package unix reads from a unix socket expecting it to be in /tmp/path
|
|
||||||
package unix
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Handler struct{}
|
|
||||||
|
|
||||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
sock := fmt.Sprintf("%s.sock", filepath.Clean(r.URL.Path))
|
|
||||||
path := filepath.Join("/tmp", sock)
|
|
||||||
|
|
||||||
c, err := net.Dial("unix", path)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
go io.Copy(c, r.Body)
|
|
||||||
// write response
|
|
||||||
io.Copy(w, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) String() string {
|
|
||||||
return "unix"
|
|
||||||
}
|
|
||||||
@@ -5,15 +5,16 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api"
|
"github.com/unistack-org/micro/v3/api"
|
||||||
"github.com/micro/go-micro/api/handler"
|
"github.com/unistack-org/micro/v3/api/handler"
|
||||||
"github.com/micro/go-micro/client/selector"
|
"github.com/unistack-org/micro/v3/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -70,16 +71,20 @@ func (wh *webHandler) getService(r *http.Request) (string, error) {
|
|||||||
return "", errors.New("no route found")
|
return "", errors.New("no route found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a random selector
|
// get the nodes
|
||||||
next := selector.Random(service.Services)
|
nodes := make([]*registry.Node, 0, len(service.Services))
|
||||||
|
for _, srv := range service.Services {
|
||||||
// get the next node
|
nodes = append(nodes, srv.Nodes...)
|
||||||
s, err := next()
|
|
||||||
if err != nil {
|
|
||||||
return "", nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("http://%s", s.Address), nil
|
if len(nodes) == 0 {
|
||||||
|
return "", errors.New("no route found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// select a random node
|
||||||
|
node := nodes[rand.Int()%len(nodes)]
|
||||||
|
|
||||||
|
return fmt.Sprintf("http://%s", node.Address), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// serveWebSocket used to serve a web socket proxied connection
|
// serveWebSocket used to serve a web socket proxied connection
|
||||||
|
|||||||
@@ -1,65 +1,81 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
// source: github.com/micro/go-micro/api/proto/api.proto
|
// versions:
|
||||||
|
// protoc-gen-go v1.25.0
|
||||||
|
// protoc v3.6.1
|
||||||
|
// source: api/proto/api.proto
|
||||||
|
|
||||||
package go_api
|
package go_api
|
||||||
|
|
||||||
import proto "github.com/golang/protobuf/proto"
|
import (
|
||||||
import fmt "fmt"
|
proto "github.com/golang/protobuf/proto"
|
||||||
import math "math"
|
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
|
reflect "reflect"
|
||||||
|
sync "sync"
|
||||||
|
)
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
const (
|
||||||
var _ = proto.Marshal
|
// Verify that this generated code is sufficiently up-to-date.
|
||||||
var _ = fmt.Errorf
|
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||||
var _ = math.Inf
|
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||||
|
)
|
||||||
|
|
||||||
// This is a compile-time assertion to ensure that this generated file
|
// This is a compile-time assertion that a sufficiently up-to-date version
|
||||||
// is compatible with the proto package it is being compiled against.
|
// of the legacy proto package is being used.
|
||||||
// A compilation error at this line likely means your copy of the
|
const _ = proto.ProtoPackageIsVersion4
|
||||||
// proto package needs to be updated.
|
|
||||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
|
||||||
|
|
||||||
type Pair struct {
|
type Pair struct {
|
||||||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
state protoimpl.MessageState
|
||||||
Values []string `protobuf:"bytes,2,rep,name=values,proto3" json:"values,omitempty"`
|
sizeCache protoimpl.SizeCache
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
unknownFields protoimpl.UnknownFields
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||||
|
Values []string `protobuf:"bytes,2,rep,name=values,proto3" json:"values,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Pair) Reset() { *m = Pair{} }
|
func (x *Pair) Reset() {
|
||||||
func (m *Pair) String() string { return proto.CompactTextString(m) }
|
*x = Pair{}
|
||||||
func (*Pair) ProtoMessage() {}
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[0]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Pair) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Pair) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Pair) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[0]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Pair.ProtoReflect.Descriptor instead.
|
||||||
func (*Pair) Descriptor() ([]byte, []int) {
|
func (*Pair) Descriptor() ([]byte, []int) {
|
||||||
return fileDescriptor_api_17a7876430d97ebd, []int{0}
|
return file_api_proto_api_proto_rawDescGZIP(), []int{0}
|
||||||
}
|
|
||||||
func (m *Pair) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_Pair.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *Pair) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_Pair.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *Pair) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_Pair.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *Pair) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_Pair.Size(m)
|
|
||||||
}
|
|
||||||
func (m *Pair) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_Pair.DiscardUnknown(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var xxx_messageInfo_Pair proto.InternalMessageInfo
|
func (x *Pair) GetKey() string {
|
||||||
|
if x != nil {
|
||||||
func (m *Pair) GetKey() string {
|
return x.Key
|
||||||
if m != nil {
|
|
||||||
return m.Key
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Pair) GetValues() []string {
|
func (x *Pair) GetValues() []string {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Values
|
return x.Values
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -67,87 +83,96 @@ func (m *Pair) GetValues() []string {
|
|||||||
// A HTTP request as RPC
|
// A HTTP request as RPC
|
||||||
// Forward by the api handler
|
// Forward by the api handler
|
||||||
type Request struct {
|
type Request struct {
|
||||||
Method string `protobuf:"bytes,1,opt,name=method,proto3" json:"method,omitempty"`
|
state protoimpl.MessageState
|
||||||
Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
|
sizeCache protoimpl.SizeCache
|
||||||
Header map[string]*Pair `protobuf:"bytes,3,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
unknownFields protoimpl.UnknownFields
|
||||||
Get map[string]*Pair `protobuf:"bytes,4,rep,name=get,proto3" json:"get,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
|
||||||
Post map[string]*Pair `protobuf:"bytes,5,rep,name=post,proto3" json:"post,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
Method string `protobuf:"bytes,1,opt,name=method,proto3" json:"method,omitempty"`
|
||||||
Body string `protobuf:"bytes,6,opt,name=body,proto3" json:"body,omitempty"`
|
Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
|
||||||
Url string `protobuf:"bytes,7,opt,name=url,proto3" json:"url,omitempty"`
|
Header map[string]*Pair `protobuf:"bytes,3,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
Get map[string]*Pair `protobuf:"bytes,4,rep,name=get,proto3" json:"get,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
XXX_unrecognized []byte `json:"-"`
|
Post map[string]*Pair `protobuf:"bytes,5,rep,name=post,proto3" json:"post,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
XXX_sizecache int32 `json:"-"`
|
Body string `protobuf:"bytes,6,opt,name=body,proto3" json:"body,omitempty"` // raw request body; if not application/x-www-form-urlencoded
|
||||||
|
Url string `protobuf:"bytes,7,opt,name=url,proto3" json:"url,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Request) Reset() { *m = Request{} }
|
func (x *Request) Reset() {
|
||||||
func (m *Request) String() string { return proto.CompactTextString(m) }
|
*x = Request{}
|
||||||
func (*Request) ProtoMessage() {}
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[1]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Request) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Request) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Request) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[1]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Request.ProtoReflect.Descriptor instead.
|
||||||
func (*Request) Descriptor() ([]byte, []int) {
|
func (*Request) Descriptor() ([]byte, []int) {
|
||||||
return fileDescriptor_api_17a7876430d97ebd, []int{1}
|
return file_api_proto_api_proto_rawDescGZIP(), []int{1}
|
||||||
}
|
|
||||||
func (m *Request) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_Request.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_Request.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *Request) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_Request.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *Request) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_Request.Size(m)
|
|
||||||
}
|
|
||||||
func (m *Request) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_Request.DiscardUnknown(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var xxx_messageInfo_Request proto.InternalMessageInfo
|
func (x *Request) GetMethod() string {
|
||||||
|
if x != nil {
|
||||||
func (m *Request) GetMethod() string {
|
return x.Method
|
||||||
if m != nil {
|
|
||||||
return m.Method
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Request) GetPath() string {
|
func (x *Request) GetPath() string {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Path
|
return x.Path
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Request) GetHeader() map[string]*Pair {
|
func (x *Request) GetHeader() map[string]*Pair {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Header
|
return x.Header
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Request) GetGet() map[string]*Pair {
|
func (x *Request) GetGet() map[string]*Pair {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Get
|
return x.Get
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Request) GetPost() map[string]*Pair {
|
func (x *Request) GetPost() map[string]*Pair {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Post
|
return x.Post
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Request) GetBody() string {
|
func (x *Request) GetBody() string {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Body
|
return x.Body
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Request) GetUrl() string {
|
func (x *Request) GetUrl() string {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Url
|
return x.Url
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -155,55 +180,64 @@ func (m *Request) GetUrl() string {
|
|||||||
// A HTTP response as RPC
|
// A HTTP response as RPC
|
||||||
// Expected response for the api handler
|
// Expected response for the api handler
|
||||||
type Response struct {
|
type Response struct {
|
||||||
StatusCode int32 `protobuf:"varint,1,opt,name=statusCode,proto3" json:"statusCode,omitempty"`
|
state protoimpl.MessageState
|
||||||
Header map[string]*Pair `protobuf:"bytes,2,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
sizeCache protoimpl.SizeCache
|
||||||
Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
|
unknownFields protoimpl.UnknownFields
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
StatusCode int32 `protobuf:"varint,1,opt,name=statusCode,proto3" json:"statusCode,omitempty"`
|
||||||
XXX_sizecache int32 `json:"-"`
|
Header map[string]*Pair `protobuf:"bytes,2,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
|
Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Response) Reset() { *m = Response{} }
|
func (x *Response) Reset() {
|
||||||
func (m *Response) String() string { return proto.CompactTextString(m) }
|
*x = Response{}
|
||||||
func (*Response) ProtoMessage() {}
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[2]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Response) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Response) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Response) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[2]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Response.ProtoReflect.Descriptor instead.
|
||||||
func (*Response) Descriptor() ([]byte, []int) {
|
func (*Response) Descriptor() ([]byte, []int) {
|
||||||
return fileDescriptor_api_17a7876430d97ebd, []int{2}
|
return file_api_proto_api_proto_rawDescGZIP(), []int{2}
|
||||||
}
|
|
||||||
func (m *Response) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_Response.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_Response.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *Response) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_Response.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *Response) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_Response.Size(m)
|
|
||||||
}
|
|
||||||
func (m *Response) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_Response.DiscardUnknown(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var xxx_messageInfo_Response proto.InternalMessageInfo
|
func (x *Response) GetStatusCode() int32 {
|
||||||
|
if x != nil {
|
||||||
func (m *Response) GetStatusCode() int32 {
|
return x.StatusCode
|
||||||
if m != nil {
|
|
||||||
return m.StatusCode
|
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Response) GetHeader() map[string]*Pair {
|
func (x *Response) GetHeader() map[string]*Pair {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Header
|
return x.Header
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Response) GetBody() string {
|
func (x *Response) GetBody() string {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Body
|
return x.Body
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -211,6 +245,10 @@ func (m *Response) GetBody() string {
|
|||||||
// A HTTP event as RPC
|
// A HTTP event as RPC
|
||||||
// Forwarded by the event handler
|
// Forwarded by the event handler
|
||||||
type Event struct {
|
type Event struct {
|
||||||
|
state protoimpl.MessageState
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
// e.g login
|
// e.g login
|
||||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||||
// uuid
|
// uuid
|
||||||
@@ -220,113 +258,254 @@ type Event struct {
|
|||||||
// event headers
|
// event headers
|
||||||
Header map[string]*Pair `protobuf:"bytes,4,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
Header map[string]*Pair `protobuf:"bytes,4,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||||
// the event data
|
// the event data
|
||||||
Data string `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
|
Data string `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Event) Reset() { *m = Event{} }
|
func (x *Event) Reset() {
|
||||||
func (m *Event) String() string { return proto.CompactTextString(m) }
|
*x = Event{}
|
||||||
func (*Event) ProtoMessage() {}
|
if protoimpl.UnsafeEnabled {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[3]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Event) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Event) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *Event) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_api_proto_api_proto_msgTypes[3]
|
||||||
|
if protoimpl.UnsafeEnabled && x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use Event.ProtoReflect.Descriptor instead.
|
||||||
func (*Event) Descriptor() ([]byte, []int) {
|
func (*Event) Descriptor() ([]byte, []int) {
|
||||||
return fileDescriptor_api_17a7876430d97ebd, []int{3}
|
return file_api_proto_api_proto_rawDescGZIP(), []int{3}
|
||||||
}
|
|
||||||
func (m *Event) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_Event.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *Event) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_Event.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (dst *Event) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_Event.Merge(dst, src)
|
|
||||||
}
|
|
||||||
func (m *Event) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_Event.Size(m)
|
|
||||||
}
|
|
||||||
func (m *Event) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_Event.DiscardUnknown(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var xxx_messageInfo_Event proto.InternalMessageInfo
|
func (x *Event) GetName() string {
|
||||||
|
if x != nil {
|
||||||
func (m *Event) GetName() string {
|
return x.Name
|
||||||
if m != nil {
|
|
||||||
return m.Name
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Event) GetId() string {
|
func (x *Event) GetId() string {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Id
|
return x.Id
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Event) GetTimestamp() int64 {
|
func (x *Event) GetTimestamp() int64 {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Timestamp
|
return x.Timestamp
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Event) GetHeader() map[string]*Pair {
|
func (x *Event) GetHeader() map[string]*Pair {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Header
|
return x.Header
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Event) GetData() string {
|
func (x *Event) GetData() string {
|
||||||
if m != nil {
|
if x != nil {
|
||||||
return m.Data
|
return x.Data
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
var File_api_proto_api_proto protoreflect.FileDescriptor
|
||||||
proto.RegisterType((*Pair)(nil), "go.api.Pair")
|
|
||||||
proto.RegisterType((*Request)(nil), "go.api.Request")
|
var file_api_proto_api_proto_rawDesc = []byte{
|
||||||
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Request.GetEntry")
|
0x0a, 0x13, 0x61, 0x70, 0x69, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x61, 0x70, 0x69, 0x2e,
|
||||||
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Request.HeaderEntry")
|
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x22, 0x30, 0x0a,
|
||||||
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Request.PostEntry")
|
0x04, 0x50, 0x61, 0x69, 0x72, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01,
|
||||||
proto.RegisterType((*Response)(nil), "go.api.Response")
|
0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65,
|
||||||
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Response.HeaderEntry")
|
0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x22,
|
||||||
proto.RegisterType((*Event)(nil), "go.api.Event")
|
0xc1, 0x03, 0x0a, 0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6d,
|
||||||
proto.RegisterMapType((map[string]*Pair)(nil), "go.api.Event.HeaderEntry")
|
0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74,
|
||||||
|
0x68, 0x6f, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||||
|
0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, 0x12, 0x33, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65,
|
||||||
|
0x72, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69,
|
||||||
|
0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x45,
|
||||||
|
0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x2a, 0x0a, 0x03,
|
||||||
|
0x67, 0x65, 0x74, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x67, 0x6f, 0x2e, 0x61,
|
||||||
|
0x70, 0x69, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x47, 0x65, 0x74, 0x45, 0x6e,
|
||||||
|
0x74, 0x72, 0x79, 0x52, 0x03, 0x67, 0x65, 0x74, 0x12, 0x2d, 0x0a, 0x04, 0x70, 0x6f, 0x73, 0x74,
|
||||||
|
0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e,
|
||||||
|
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x50, 0x6f, 0x73, 0x74, 0x45, 0x6e, 0x74, 0x72,
|
||||||
|
0x79, 0x52, 0x04, 0x70, 0x6f, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18,
|
||||||
|
0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x75,
|
||||||
|
0x72, 0x6c, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x1a, 0x47, 0x0a,
|
||||||
|
0x0b, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03,
|
||||||
|
0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x22,
|
||||||
|
0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e,
|
||||||
|
0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x50, 0x61, 0x69, 0x72, 0x52, 0x05, 0x76, 0x61, 0x6c,
|
||||||
|
0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x1a, 0x44, 0x0a, 0x08, 0x47, 0x65, 0x74, 0x45, 0x6e, 0x74,
|
||||||
|
0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||||
|
0x03, 0x6b, 0x65, 0x79, 0x12, 0x22, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20,
|
||||||
|
0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x50, 0x61, 0x69,
|
||||||
|
0x72, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x1a, 0x45, 0x0a, 0x09,
|
||||||
|
0x50, 0x6f, 0x73, 0x74, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79,
|
||||||
|
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x22, 0x0a, 0x05, 0x76,
|
||||||
|
0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x67, 0x6f, 0x2e,
|
||||||
|
0x61, 0x70, 0x69, 0x2e, 0x50, 0x61, 0x69, 0x72, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a,
|
||||||
|
0x02, 0x38, 0x01, 0x22, 0xbd, 0x01, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
|
||||||
|
0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01,
|
||||||
|
0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65,
|
||||||
|
0x12, 0x34, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b,
|
||||||
|
0x32, 0x1c, 0x2e, 0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
|
||||||
|
0x73, 0x65, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x06,
|
||||||
|
0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03,
|
||||||
|
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x1a, 0x47, 0x0a, 0x0b, 0x48, 0x65,
|
||||||
|
0x61, 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79,
|
||||||
|
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x22, 0x0a, 0x05, 0x76,
|
||||||
|
0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x67, 0x6f, 0x2e,
|
||||||
|
0x61, 0x70, 0x69, 0x2e, 0x50, 0x61, 0x69, 0x72, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a,
|
||||||
|
0x02, 0x38, 0x01, 0x22, 0xd9, 0x01, 0x0a, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a,
|
||||||
|
0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d,
|
||||||
|
0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69,
|
||||||
|
0x64, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x03,
|
||||||
|
0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x12,
|
||||||
|
0x31, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32,
|
||||||
|
0x19, 0x2e, 0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x48,
|
||||||
|
0x65, 0x61, 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x68, 0x65, 0x61, 0x64,
|
||||||
|
0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09,
|
||||||
|
0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x1a, 0x47, 0x0a, 0x0b, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72,
|
||||||
|
0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01,
|
||||||
|
0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x22, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65,
|
||||||
|
0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x67, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e,
|
||||||
|
0x50, 0x61, 0x69, 0x72, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x62,
|
||||||
|
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
var (
|
||||||
proto.RegisterFile("github.com/micro/go-micro/api/proto/api.proto", fileDescriptor_api_17a7876430d97ebd)
|
file_api_proto_api_proto_rawDescOnce sync.Once
|
||||||
|
file_api_proto_api_proto_rawDescData = file_api_proto_api_proto_rawDesc
|
||||||
|
)
|
||||||
|
|
||||||
|
func file_api_proto_api_proto_rawDescGZIP() []byte {
|
||||||
|
file_api_proto_api_proto_rawDescOnce.Do(func() {
|
||||||
|
file_api_proto_api_proto_rawDescData = protoimpl.X.CompressGZIP(file_api_proto_api_proto_rawDescData)
|
||||||
|
})
|
||||||
|
return file_api_proto_api_proto_rawDescData
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileDescriptor_api_17a7876430d97ebd = []byte{
|
var file_api_proto_api_proto_msgTypes = make([]protoimpl.MessageInfo, 9)
|
||||||
// 410 bytes of a gzipped FileDescriptorProto
|
var file_api_proto_api_proto_goTypes = []interface{}{
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x53, 0xc1, 0x6e, 0xd4, 0x30,
|
(*Pair)(nil), // 0: go.api.Pair
|
||||||
0x10, 0x55, 0xe2, 0x24, 0x6d, 0x66, 0x11, 0x42, 0x3e, 0x20, 0x53, 0x2a, 0xb4, 0xca, 0x85, 0x15,
|
(*Request)(nil), // 1: go.api.Request
|
||||||
0x52, 0x13, 0x68, 0x39, 0x20, 0xae, 0xb0, 0x2a, 0xc7, 0xca, 0x7f, 0xe0, 0x6d, 0xac, 0xc4, 0x62,
|
(*Response)(nil), // 2: go.api.Response
|
||||||
0x13, 0x9b, 0xd8, 0xa9, 0xb4, 0x1f, 0xc7, 0x81, 0xcf, 0xe0, 0x6f, 0x90, 0x27, 0xde, 0xdd, 0xb2,
|
(*Event)(nil), // 3: go.api.Event
|
||||||
0x5a, 0x2e, 0x74, 0x6f, 0x2f, 0xf6, 0x9b, 0x37, 0x6f, 0xde, 0x38, 0xf0, 0xb6, 0x51, 0xae, 0x1d,
|
nil, // 4: go.api.Request.HeaderEntry
|
||||||
0x57, 0xe5, 0xbd, 0xee, 0xaa, 0x4e, 0xdd, 0x0f, 0xba, 0x6a, 0xf4, 0x95, 0x30, 0xaa, 0x32, 0x83,
|
nil, // 5: go.api.Request.GetEntry
|
||||||
0x76, 0xba, 0x12, 0x46, 0x95, 0x88, 0x68, 0xd6, 0xe8, 0x52, 0x18, 0x55, 0xbc, 0x87, 0xe4, 0x4e,
|
nil, // 6: go.api.Request.PostEntry
|
||||||
0xa8, 0x81, 0xbe, 0x00, 0xf2, 0x5d, 0x6e, 0x58, 0x34, 0x8f, 0x16, 0x39, 0xf7, 0x90, 0xbe, 0x84,
|
nil, // 7: go.api.Response.HeaderEntry
|
||||||
0xec, 0x41, 0xac, 0x47, 0x69, 0x59, 0x3c, 0x27, 0x8b, 0x9c, 0x87, 0xaf, 0xe2, 0x17, 0x81, 0x33,
|
nil, // 8: go.api.Event.HeaderEntry
|
||||||
0x2e, 0x7f, 0x8c, 0xd2, 0x3a, 0xcf, 0xe9, 0xa4, 0x6b, 0x75, 0x1d, 0x0a, 0xc3, 0x17, 0xa5, 0x90,
|
}
|
||||||
0x18, 0xe1, 0x5a, 0x16, 0xe3, 0x29, 0x62, 0x7a, 0x03, 0x59, 0x2b, 0x45, 0x2d, 0x07, 0x46, 0xe6,
|
var file_api_proto_api_proto_depIdxs = []int32{
|
||||||
0x64, 0x31, 0xbb, 0x7e, 0x5d, 0x4e, 0x16, 0xca, 0x20, 0x56, 0x7e, 0xc3, 0xdb, 0x65, 0xef, 0x86,
|
4, // 0: go.api.Request.header:type_name -> go.api.Request.HeaderEntry
|
||||||
0x0d, 0x0f, 0x54, 0xfa, 0x0e, 0x48, 0x23, 0x1d, 0x4b, 0xb0, 0x82, 0x1d, 0x56, 0xdc, 0x4a, 0x37,
|
5, // 1: go.api.Request.get:type_name -> go.api.Request.GetEntry
|
||||||
0xd1, 0x3d, 0x89, 0x5e, 0x41, 0x62, 0xb4, 0x75, 0x2c, 0x45, 0xf2, 0xab, 0x43, 0xf2, 0x9d, 0xb6,
|
6, // 2: go.api.Request.post:type_name -> go.api.Request.PostEntry
|
||||||
0x81, 0x8d, 0x34, 0xef, 0x71, 0xa5, 0xeb, 0x0d, 0xcb, 0x26, 0x8f, 0x1e, 0xfb, 0x14, 0xc6, 0x61,
|
7, // 3: go.api.Response.header:type_name -> go.api.Response.HeaderEntry
|
||||||
0xcd, 0xce, 0xa6, 0x14, 0xc6, 0x61, 0x7d, 0x71, 0x0b, 0xb3, 0x47, 0xbe, 0x8e, 0xc4, 0x54, 0x40,
|
8, // 4: go.api.Event.header:type_name -> go.api.Event.HeaderEntry
|
||||||
0x8a, 0xc1, 0xe0, 0xac, 0xb3, 0xeb, 0x67, 0xdb, 0xb6, 0x3e, 0x55, 0x3e, 0x5d, 0x7d, 0x8e, 0x3f,
|
0, // 5: go.api.Request.HeaderEntry.value:type_name -> go.api.Pair
|
||||||
0x45, 0x17, 0x5f, 0xe1, 0x7c, 0x6b, 0xf7, 0x09, 0x2a, 0x4b, 0xc8, 0x77, 0x73, 0xfc, 0xbf, 0x4c,
|
0, // 6: go.api.Request.GetEntry.value:type_name -> go.api.Pair
|
||||||
0xf1, 0x33, 0x82, 0x73, 0x2e, 0xad, 0xd1, 0xbd, 0x95, 0xf4, 0x0d, 0x80, 0x75, 0xc2, 0x8d, 0xf6,
|
0, // 7: go.api.Request.PostEntry.value:type_name -> go.api.Pair
|
||||||
0x8b, 0xae, 0x25, 0xaa, 0xa5, 0xfc, 0xd1, 0x09, 0xfd, 0xb8, 0x5b, 0x5c, 0x8c, 0xc9, 0x5e, 0xee,
|
0, // 8: go.api.Response.HeaderEntry.value:type_name -> go.api.Pair
|
||||||
0x93, 0x9d, 0x14, 0x8e, 0x6e, 0x6e, 0x1b, 0x2f, 0xd9, 0xc7, 0x7b, 0xb2, 0x30, 0x8b, 0xdf, 0x11,
|
0, // 9: go.api.Event.HeaderEntry.value:type_name -> go.api.Pair
|
||||||
0xa4, 0xcb, 0x07, 0xd9, 0xe3, 0x16, 0x7b, 0xd1, 0xc9, 0x20, 0x82, 0x98, 0x3e, 0x87, 0x58, 0xd5,
|
10, // [10:10] is the sub-list for method output_type
|
||||||
0xe1, 0xed, 0xc5, 0xaa, 0xa6, 0x97, 0x90, 0x3b, 0xd5, 0x49, 0xeb, 0x44, 0x67, 0xd0, 0x0f, 0xe1,
|
10, // [10:10] is the sub-list for method input_type
|
||||||
0xfb, 0x03, 0xfa, 0x61, 0x37, 0x5e, 0xf2, 0xf7, 0xc3, 0xc1, 0x06, 0xff, 0x9a, 0xad, 0x16, 0x4e,
|
10, // [10:10] is the sub-list for extension type_name
|
||||||
0xb0, 0x74, 0x6a, 0xea, 0xf1, 0xc9, 0x66, 0x5b, 0x65, 0xf8, 0x83, 0xde, 0xfc, 0x09, 0x00, 0x00,
|
10, // [10:10] is the sub-list for extension extendee
|
||||||
0xff, 0xff, 0x7a, 0xb4, 0xd4, 0x8f, 0xcb, 0x03, 0x00, 0x00,
|
0, // [0:10] is the sub-list for field type_name
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { file_api_proto_api_proto_init() }
|
||||||
|
func file_api_proto_api_proto_init() {
|
||||||
|
if File_api_proto_api_proto != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !protoimpl.UnsafeEnabled {
|
||||||
|
file_api_proto_api_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*Pair); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file_api_proto_api_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*Request); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file_api_proto_api_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*Response); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file_api_proto_api_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||||
|
switch v := v.(*Event); i {
|
||||||
|
case 0:
|
||||||
|
return &v.state
|
||||||
|
case 1:
|
||||||
|
return &v.sizeCache
|
||||||
|
case 2:
|
||||||
|
return &v.unknownFields
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type x struct{}
|
||||||
|
out := protoimpl.TypeBuilder{
|
||||||
|
File: protoimpl.DescBuilder{
|
||||||
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
|
RawDescriptor: file_api_proto_api_proto_rawDesc,
|
||||||
|
NumEnums: 0,
|
||||||
|
NumMessages: 9,
|
||||||
|
NumExtensions: 0,
|
||||||
|
NumServices: 0,
|
||||||
|
},
|
||||||
|
GoTypes: file_api_proto_api_proto_goTypes,
|
||||||
|
DependencyIndexes: file_api_proto_api_proto_depIdxs,
|
||||||
|
MessageInfos: file_api_proto_api_proto_msgTypes,
|
||||||
|
}.Build()
|
||||||
|
File_api_proto_api_proto = out.File
|
||||||
|
file_api_proto_api_proto_rawDesc = nil
|
||||||
|
file_api_proto_api_proto_goTypes = nil
|
||||||
|
file_api_proto_api_proto_depIdxs = nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,13 @@
|
|||||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||||
// source: github.com/micro/go-micro/api/proto/api.proto
|
// source: api/proto/api.proto
|
||||||
|
|
||||||
/*
|
|
||||||
Package go_api is a generated protocol buffer package.
|
|
||||||
|
|
||||||
It is generated from these files:
|
|
||||||
github.com/micro/go-micro/api/proto/api.proto
|
|
||||||
|
|
||||||
It has these top-level messages:
|
|
||||||
Pair
|
|
||||||
Request
|
|
||||||
Response
|
|
||||||
Event
|
|
||||||
*/
|
|
||||||
package go_api
|
package go_api
|
||||||
|
|
||||||
import proto "github.com/golang/protobuf/proto"
|
import (
|
||||||
import fmt "fmt"
|
fmt "fmt"
|
||||||
import math "math"
|
proto "github.com/golang/protobuf/proto"
|
||||||
|
math "math"
|
||||||
|
)
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
var _ = proto.Marshal
|
var _ = proto.Marshal
|
||||||
@@ -28,4 +18,4 @@ var _ = math.Inf
|
|||||||
// is compatible with the proto package it is being compiled against.
|
// is compatible with the proto package it is being compiled against.
|
||||||
// A compilation error at this line likely means your copy of the
|
// A compilation error at this line likely means your copy of the
|
||||||
// proto package needs to be updated.
|
// proto package needs to be updated.
|
||||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||||
@@ -6,12 +6,19 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/resolver"
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver struct{}
|
// Resolver struct
|
||||||
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve func to resolve enndpoint
|
||||||
|
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
|
||||||
|
// parse options
|
||||||
|
options := resolver.NewResolveOptions(opts...)
|
||||||
|
|
||||||
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|
||||||
// /foo.Bar/Service
|
// /foo.Bar/Service
|
||||||
if req.URL.Path == "/" {
|
if req.URL.Path == "/" {
|
||||||
return nil, errors.New("unknown name")
|
return nil, errors.New("unknown name")
|
||||||
@@ -26,6 +33,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
|
Domain: options.Domain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,6 +41,7 @@ func (r *Resolver) String() string {
|
|||||||
return "grpc"
|
return "grpc"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewResolver is used to create new Resolver
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
return &Resolver{}
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,17 +4,23 @@ package host
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/resolver"
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver struct{}
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
|
||||||
|
// parse options
|
||||||
|
options := resolver.NewResolveOptions(opts...)
|
||||||
|
|
||||||
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: req.Host,
|
Name: req.Host,
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
|
Domain: options.Domain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,5 +29,5 @@ func (r *Resolver) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
return &Resolver{}
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
// Package micro provides a micro rpc resolver which prefixes a namespace
|
|
||||||
package micro
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/resolver"
|
|
||||||
)
|
|
||||||
|
|
||||||
// default resolver for legacy purposes
|
|
||||||
// it uses proxy routing to resolve names
|
|
||||||
// /foo becomes namespace.foo
|
|
||||||
// /v1/foo becomes namespace.v1.foo
|
|
||||||
type Resolver struct {
|
|
||||||
Options resolver.Options
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|
||||||
var name, method string
|
|
||||||
|
|
||||||
switch r.Options.Handler {
|
|
||||||
// internal handlers
|
|
||||||
case "meta", "api", "rpc", "micro":
|
|
||||||
name, method = apiRoute(req.URL.Path)
|
|
||||||
default:
|
|
||||||
method = req.Method
|
|
||||||
name = proxyRoute(req.URL.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &resolver.Endpoint{
|
|
||||||
Name: name,
|
|
||||||
Method: method,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resolver) String() string {
|
|
||||||
return "micro"
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewResolver creates a new micro resolver
|
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
|
||||||
return &Resolver{
|
|
||||||
Options: resolver.NewOptions(opts...),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
package micro
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
proxyRe = regexp.MustCompile("^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$")
|
|
||||||
versionRe = regexp.MustCompilePOSIX("^v[0-9]+$")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Translates /foo/bar/zool into api service go.micro.api.foo method Bar.Zool
|
|
||||||
// Translates /foo/bar into api service go.micro.api.foo method Foo.Bar
|
|
||||||
func apiRoute(p string) (string, string) {
|
|
||||||
p = path.Clean(p)
|
|
||||||
p = strings.TrimPrefix(p, "/")
|
|
||||||
parts := strings.Split(p, "/")
|
|
||||||
|
|
||||||
// If we've got two or less parts
|
|
||||||
// Use first part as service
|
|
||||||
// Use all parts as method
|
|
||||||
if len(parts) <= 2 {
|
|
||||||
name := parts[0]
|
|
||||||
return name, methodName(parts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Treat /v[0-9]+ as versioning where we have 3 parts
|
|
||||||
// /v1/foo/bar => service: v1.foo method: Foo.bar
|
|
||||||
if len(parts) == 3 && versionRe.Match([]byte(parts[0])) {
|
|
||||||
name := strings.Join(parts[:len(parts)-1], ".")
|
|
||||||
return name, methodName(parts[len(parts)-2:])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Service is everything minus last two parts
|
|
||||||
// Method is the last two parts
|
|
||||||
name := strings.Join(parts[:len(parts)-2], ".")
|
|
||||||
return name, methodName(parts[len(parts)-2:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func proxyRoute(p string) string {
|
|
||||||
parts := strings.Split(p, "/")
|
|
||||||
if len(parts) < 2 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var service string
|
|
||||||
var alias string
|
|
||||||
|
|
||||||
// /[service]/methods
|
|
||||||
if len(parts) > 2 {
|
|
||||||
// /v1/[service]
|
|
||||||
if versionRe.MatchString(parts[1]) {
|
|
||||||
service = parts[1] + "." + parts[2]
|
|
||||||
alias = parts[2]
|
|
||||||
} else {
|
|
||||||
service = parts[1]
|
|
||||||
alias = parts[1]
|
|
||||||
}
|
|
||||||
// /[service]
|
|
||||||
} else {
|
|
||||||
service = parts[1]
|
|
||||||
alias = parts[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// check service name is valid
|
|
||||||
if !proxyRe.MatchString(alias) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return service
|
|
||||||
}
|
|
||||||
|
|
||||||
func methodName(parts []string) string {
|
|
||||||
for i, part := range parts {
|
|
||||||
parts[i] = toCamel(part)
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(parts, ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
func toCamel(s string) string {
|
|
||||||
words := strings.Split(s, "-")
|
|
||||||
var out string
|
|
||||||
for _, word := range words {
|
|
||||||
out += strings.Title(word)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
package micro
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestApiRoute(t *testing.T) {
|
|
||||||
testData := []struct {
|
|
||||||
path string
|
|
||||||
service string
|
|
||||||
method string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"/foo/bar",
|
|
||||||
"foo",
|
|
||||||
"Foo.Bar",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/foo/bar",
|
|
||||||
"foo",
|
|
||||||
"Foo.Bar",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/bar/baz",
|
|
||||||
"foo",
|
|
||||||
"Bar.Baz",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/bar/baz-xyz",
|
|
||||||
"foo",
|
|
||||||
"Bar.BazXyz",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/bar/baz/cat",
|
|
||||||
"foo.bar",
|
|
||||||
"Baz.Cat",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/bar/baz/cat/car",
|
|
||||||
"foo.bar.baz",
|
|
||||||
"Cat.Car",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/fooBar/bazCat",
|
|
||||||
"foo",
|
|
||||||
"FooBar.BazCat",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/v1/foo/bar",
|
|
||||||
"v1.foo",
|
|
||||||
"Foo.Bar",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/v1/foo/bar/baz",
|
|
||||||
"v1.foo",
|
|
||||||
"Bar.Baz",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/v1/foo/bar/baz/cat",
|
|
||||||
"v1.foo.bar",
|
|
||||||
"Baz.Cat",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, d := range testData {
|
|
||||||
s, m := apiRoute(d.path)
|
|
||||||
if d.service != s {
|
|
||||||
t.Fatalf("Expected service: %s for path: %s got: %s %s", d.service, d.path, s, m)
|
|
||||||
}
|
|
||||||
if d.method != m {
|
|
||||||
t.Fatalf("Expected service: %s for path: %s got: %s", d.method, d.path, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestProxyRoute(t *testing.T) {
|
|
||||||
testData := []struct {
|
|
||||||
path string
|
|
||||||
service string
|
|
||||||
}{
|
|
||||||
// no namespace
|
|
||||||
{
|
|
||||||
"/f",
|
|
||||||
"f",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/f",
|
|
||||||
"f",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/f-b",
|
|
||||||
"f-b",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/bar",
|
|
||||||
"foo",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo-bar",
|
|
||||||
"foo-bar",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo-bar-baz",
|
|
||||||
"foo-bar-baz",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/foo/bar/bar",
|
|
||||||
"foo",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/v1/foo/bar",
|
|
||||||
"v1.foo",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/v1/foo/bar/baz",
|
|
||||||
"v1.foo",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"/v1/foo/bar/baz/cat",
|
|
||||||
"v1.foo",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, d := range testData {
|
|
||||||
s := proxyRoute(d.path)
|
|
||||||
if d.service != s {
|
|
||||||
t.Fatalf("Expected service: %s for path: %s got: %s", d.service, d.path, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
package resolver
|
package resolver
|
||||||
|
|
||||||
// NewOptions returns new initialised options
|
import (
|
||||||
func NewOptions(opts ...Option) Options {
|
"github.com/unistack-org/micro/v3/registry"
|
||||||
var options Options
|
)
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
// Options struct
|
||||||
}
|
type Options struct {
|
||||||
return options
|
Handler string
|
||||||
|
ServicePrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Option func
|
||||||
|
type Option func(o *Options)
|
||||||
|
|
||||||
// WithHandler sets the handler being used
|
// WithHandler sets the handler being used
|
||||||
func WithHandler(h string) Option {
|
func WithHandler(h string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
@@ -16,9 +20,43 @@ func WithHandler(h string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithNamespace sets the namespace being used
|
// WithServicePrefix sets the ServicePrefix option
|
||||||
func WithNamespace(n string) Option {
|
func WithServicePrefix(p string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Namespace = n
|
o.ServicePrefix = p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewOptions returns new initialised options
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
options := Options{}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveOptions are used when resolving a request
|
||||||
|
type ResolveOptions struct {
|
||||||
|
Domain string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveOption sets an option
|
||||||
|
type ResolveOption func(*ResolveOptions)
|
||||||
|
|
||||||
|
// Domain sets the resolve Domain option
|
||||||
|
func Domain(n string) ResolveOption {
|
||||||
|
return func(o *ResolveOptions) {
|
||||||
|
o.Domain = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewResolveOptions returns new initialised resolve options
|
||||||
|
func NewResolveOptions(opts ...ResolveOption) ResolveOptions {
|
||||||
|
options := ResolveOptions{Domain: registry.DefaultDomain}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,25 +2,32 @@
|
|||||||
package path
|
package path
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/resolver"
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver struct{}
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
|
||||||
|
// parse options
|
||||||
|
options := resolver.NewResolveOptions(opts...)
|
||||||
|
|
||||||
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|
||||||
if req.URL.Path == "/" {
|
if req.URL.Path == "/" {
|
||||||
return nil, errors.New("unknown name")
|
return nil, resolver.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(req.URL.Path[1:], "/")
|
parts := strings.Split(req.URL.Path[1:], "/")
|
||||||
|
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[0],
|
Name: r.opts.ServicePrefix + "." + parts[0],
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
|
Domain: options.Domain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,5 +36,5 @@ func (r *Resolver) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
return &Resolver{}
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,18 +2,26 @@
|
|||||||
package resolver
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrNotFound returned when endpoint is not found
|
||||||
|
ErrNotFound = errors.New("not found")
|
||||||
|
// ErrInvalidPath returned on invalid path
|
||||||
|
ErrInvalidPath = errors.New("invalid path")
|
||||||
|
)
|
||||||
|
|
||||||
// Resolver resolves requests to endpoints
|
// Resolver resolves requests to endpoints
|
||||||
type Resolver interface {
|
type Resolver interface {
|
||||||
Resolve(r *http.Request) (*Endpoint, error)
|
Resolve(r *http.Request, opts ...ResolveOption) (*Endpoint, error)
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Endpoint is the endpoint for a http request
|
// Endpoint is the endpoint for a http request
|
||||||
type Endpoint struct {
|
type Endpoint struct {
|
||||||
// e.g greeter
|
// Endpoint name e.g greeter
|
||||||
Name string
|
Name string
|
||||||
// HTTP Host e.g example.com
|
// HTTP Host e.g example.com
|
||||||
Host string
|
Host string
|
||||||
@@ -21,11 +29,6 @@ type Endpoint struct {
|
|||||||
Method string
|
Method string
|
||||||
// HTTP Path e.g /greeter.
|
// HTTP Path e.g /greeter.
|
||||||
Path string
|
Path string
|
||||||
|
// Domain endpoint exists within
|
||||||
|
Domain string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
|
||||||
Handler string
|
|
||||||
Namespace string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Option func(o *Options)
|
|
||||||
|
|||||||
87
api/resolver/subdomain/subdomain.go
Normal file
87
api/resolver/subdomain/subdomain.go
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
// Package subdomain is a resolver which uses the subdomain to determine the domain to route to. It
|
||||||
|
// offloads the endpoint resolution to a child resolver which is provided in New.
|
||||||
|
package subdomain
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
|
"golang.org/x/net/publicsuffix"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewResolver(parent resolver.Resolver, opts ...resolver.Option) resolver.Resolver {
|
||||||
|
options := resolver.NewOptions(opts...)
|
||||||
|
return &Resolver{options, parent}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
resolver.Resolver
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
|
||||||
|
if dom := r.Domain(req); len(dom) > 0 {
|
||||||
|
opts = append(opts, resolver.Domain(dom))
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Resolver.Resolve(req, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) Domain(req *http.Request) string {
|
||||||
|
// determine the host, e.g. foobar.m3o.app
|
||||||
|
host := req.URL.Hostname()
|
||||||
|
if len(host) == 0 {
|
||||||
|
if h, _, err := net.SplitHostPort(req.Host); err == nil {
|
||||||
|
host = h // host does contain a port
|
||||||
|
} else if strings.Contains(err.Error(), "missing port in address") {
|
||||||
|
host = req.Host // host does not contain a port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for an ip address
|
||||||
|
if net.ParseIP(host) != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for dev environment
|
||||||
|
if host == "localhost" || host == "127.0.0.1" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// extract the top level domain plus one (e.g. 'myapp.com')
|
||||||
|
domain, err := publicsuffix.EffectiveTLDPlusOne(host)
|
||||||
|
if err != nil {
|
||||||
|
if logger.V(logger.DebugLevel) {
|
||||||
|
logger.Debug("Unable to extract domain from %v", host)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// there was no subdomain
|
||||||
|
if host == domain {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove the domain from the host, leaving the subdomain, e.g. "staging.foo.myapp.com" => "staging.foo"
|
||||||
|
subdomain := strings.TrimSuffix(host, "."+domain)
|
||||||
|
|
||||||
|
// ignore the API subdomain
|
||||||
|
if subdomain == "api" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the reversed subdomain as the namespace, e.g. "staging.foo" => "foo-staging"
|
||||||
|
comps := strings.Split(subdomain, ".")
|
||||||
|
for i := len(comps)/2 - 1; i >= 0; i-- {
|
||||||
|
opp := len(comps) - 1 - i
|
||||||
|
comps[i], comps[opp] = comps[opp], comps[i]
|
||||||
|
}
|
||||||
|
return strings.Join(comps, "-")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) String() string {
|
||||||
|
return "subdomain"
|
||||||
|
}
|
||||||
71
api/resolver/subdomain/subdomain_test.go
Normal file
71
api/resolver/subdomain/subdomain_test.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package subdomain
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/unistack-org/micro/v3/api/resolver/vpath"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolve(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
Name string
|
||||||
|
Host string
|
||||||
|
Result string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "Top level domain",
|
||||||
|
Host: "micro.mu",
|
||||||
|
Result: "micro",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Effective top level domain",
|
||||||
|
Host: "micro.com.au",
|
||||||
|
Result: "micro",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Subdomain dev",
|
||||||
|
Host: "dev.micro.mu",
|
||||||
|
Result: "dev",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Subdomain foo",
|
||||||
|
Host: "foo.micro.mu",
|
||||||
|
Result: "foo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Multi-level subdomain",
|
||||||
|
Host: "staging.myapp.m3o.app",
|
||||||
|
Result: "myapp-staging",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Dev host",
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Result: "micro",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Localhost",
|
||||||
|
Host: "localhost",
|
||||||
|
Result: "micro",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "IP host",
|
||||||
|
Host: "81.151.101.146",
|
||||||
|
Result: "micro",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
|
r := NewResolver(vpath.NewResolver())
|
||||||
|
result, err := r.Resolve(&http.Request{URL: &url.URL{Host: tc.Host, Path: "foo/bar"}})
|
||||||
|
assert.Nil(t, err, "Expecter err to be nil")
|
||||||
|
if result != nil {
|
||||||
|
assert.Equal(t, tc.Result, result.Domain, "Expected %v but got %v", tc.Result, result.Domain)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,46 +7,56 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/resolver"
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver struct{}
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
re = regexp.MustCompile("^v[0-9]+$")
|
re = regexp.MustCompile("^v[0-9]+$")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
|
||||||
if req.URL.Path == "/" {
|
if req.URL.Path == "/" {
|
||||||
return nil, errors.New("unknown name")
|
return nil, errors.New("unknown name")
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(req.URL.Path[1:], "/")
|
options := resolver.NewResolveOptions(opts...)
|
||||||
|
|
||||||
|
parts := strings.Split(req.URL.Path[1:], "/")
|
||||||
if len(parts) == 1 {
|
if len(parts) == 1 {
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[0],
|
Name: r.withPrefix(parts...),
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
|
Domain: options.Domain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// /v1/foo
|
// /v1/foo
|
||||||
if re.MatchString(parts[0]) {
|
if re.MatchString(parts[0]) {
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[1],
|
Name: r.withPrefix(parts[0:2]...),
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
|
Domain: options.Domain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[0],
|
Name: r.withPrefix(parts[0]),
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
|
Domain: options.Domain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +64,12 @@ func (r *Resolver) String() string {
|
|||||||
return "path"
|
return "path"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
// withPrefix transforms "foo" into "go.micro.api.foo"
|
||||||
return &Resolver{}
|
func (r *Resolver) withPrefix(parts ...string) string {
|
||||||
|
p := r.opts.ServicePrefix
|
||||||
|
if len(p) > 0 {
|
||||||
|
parts = append([]string{p}, parts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(parts, ".")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,26 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/micro/go-micro/api/resolver"
|
"context"
|
||||||
"github.com/micro/go-micro/api/resolver/micro"
|
|
||||||
"github.com/micro/go-micro/config/cmd"
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/unistack-org/micro/v3/api/resolver/vpath"
|
||||||
|
"github.com/unistack-org/micro/v3/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Namespace string
|
Handler string
|
||||||
Handler string
|
Registry registry.Registry
|
||||||
Registry registry.Registry
|
Resolver resolver.Resolver
|
||||||
Resolver resolver.Resolver
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(o *Options)
|
type Option func(o *Options)
|
||||||
|
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Handler: "meta",
|
Context: context.Background(),
|
||||||
Registry: *cmd.DefaultOptions().Registry,
|
Handler: "meta",
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
@@ -27,33 +28,36 @@ func NewOptions(opts ...Option) Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if options.Resolver == nil {
|
if options.Resolver == nil {
|
||||||
options.Resolver = micro.NewResolver(
|
options.Resolver = vpath.NewResolver(
|
||||||
resolver.WithHandler(options.Handler),
|
resolver.WithHandler(options.Handler),
|
||||||
resolver.WithNamespace(options.Namespace),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithContext sets the context
|
||||||
|
func WithContext(ctx context.Context) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Context = ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithHandler sets the handler
|
||||||
func WithHandler(h string) Option {
|
func WithHandler(h string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Handler = h
|
o.Handler = h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithNamespace(ns string) Option {
|
// WithRegistry sets the registry
|
||||||
return func(o *Options) {
|
|
||||||
o.Namespace = ns
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WithRegistry(r registry.Registry) Option {
|
func WithRegistry(r registry.Registry) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Registry = r
|
o.Registry = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithResolver sets the resolver
|
||||||
func WithResolver(r resolver.Resolver) Option {
|
func WithResolver(r resolver.Resolver) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Resolver = r
|
o.Resolver = r
|
||||||
|
|||||||
@@ -1,393 +0,0 @@
|
|||||||
// Package registry provides a dynamic api service router
|
|
||||||
package registry
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/api"
|
|
||||||
"github.com/micro/go-micro/api/router"
|
|
||||||
"github.com/micro/go-micro/registry"
|
|
||||||
"github.com/micro/go-micro/registry/cache"
|
|
||||||
)
|
|
||||||
|
|
||||||
// router is the default router
|
|
||||||
type registryRouter struct {
|
|
||||||
exit chan bool
|
|
||||||
opts router.Options
|
|
||||||
|
|
||||||
// registry cache
|
|
||||||
rc cache.Cache
|
|
||||||
|
|
||||||
sync.RWMutex
|
|
||||||
eps map[string]*api.Service
|
|
||||||
}
|
|
||||||
|
|
||||||
func setNamespace(ns, name string) string {
|
|
||||||
ns = strings.TrimSpace(ns)
|
|
||||||
name = strings.TrimSpace(name)
|
|
||||||
|
|
||||||
// no namespace
|
|
||||||
if len(ns) == 0 {
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
// has - suffix
|
|
||||||
case strings.HasSuffix(ns, "-"):
|
|
||||||
return strings.Replace(ns+name, ".", "-", -1)
|
|
||||||
// has . suffix
|
|
||||||
case strings.HasSuffix(ns, "."):
|
|
||||||
return ns + name
|
|
||||||
}
|
|
||||||
|
|
||||||
// default join .
|
|
||||||
return strings.Join([]string{ns, name}, ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *registryRouter) isClosed() bool {
|
|
||||||
select {
|
|
||||||
case <-r.exit:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// refresh list of api services
|
|
||||||
func (r *registryRouter) refresh() {
|
|
||||||
var attempts int
|
|
||||||
|
|
||||||
for {
|
|
||||||
services, err := r.opts.Registry.ListServices()
|
|
||||||
if err != nil {
|
|
||||||
attempts++
|
|
||||||
log.Println("Error listing endpoints", err)
|
|
||||||
time.Sleep(time.Duration(attempts) * time.Second)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
attempts = 0
|
|
||||||
|
|
||||||
// for each service, get service and store endpoints
|
|
||||||
for _, s := range services {
|
|
||||||
// only get services for this namespace
|
|
||||||
if !strings.HasPrefix(s.Name, r.opts.Namespace) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
service, err := r.rc.GetService(s.Name)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
r.store(service)
|
|
||||||
}
|
|
||||||
|
|
||||||
// refresh list in 10 minutes... cruft
|
|
||||||
select {
|
|
||||||
case <-time.After(time.Minute * 10):
|
|
||||||
case <-r.exit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// process watch event
|
|
||||||
func (r *registryRouter) process(res *registry.Result) {
|
|
||||||
// skip these things
|
|
||||||
if res == nil || res.Service == nil || !strings.HasPrefix(res.Service.Name, r.opts.Namespace) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// get entry from cache
|
|
||||||
service, err := r.rc.GetService(res.Service.Name)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// update our local endpoints
|
|
||||||
r.store(service)
|
|
||||||
}
|
|
||||||
|
|
||||||
// store local endpoint cache
|
|
||||||
func (r *registryRouter) store(services []*registry.Service) {
|
|
||||||
// endpoints
|
|
||||||
eps := map[string]*api.Service{}
|
|
||||||
|
|
||||||
// services
|
|
||||||
names := map[string]bool{}
|
|
||||||
|
|
||||||
// create a new endpoint mapping
|
|
||||||
for _, service := range services {
|
|
||||||
// set names we need later
|
|
||||||
names[service.Name] = true
|
|
||||||
|
|
||||||
// map per endpoint
|
|
||||||
for _, endpoint := range service.Endpoints {
|
|
||||||
// create a key service:endpoint_name
|
|
||||||
key := fmt.Sprintf("%s:%s", service.Name, endpoint.Name)
|
|
||||||
// decode endpoint
|
|
||||||
end := api.Decode(endpoint.Metadata)
|
|
||||||
|
|
||||||
// if we got nothing skip
|
|
||||||
if err := api.Validate(end); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// try get endpoint
|
|
||||||
ep, ok := eps[key]
|
|
||||||
if !ok {
|
|
||||||
ep = &api.Service{Name: service.Name}
|
|
||||||
}
|
|
||||||
|
|
||||||
// overwrite the endpoint
|
|
||||||
ep.Endpoint = end
|
|
||||||
// append services
|
|
||||||
ep.Services = append(ep.Services, service)
|
|
||||||
// store it
|
|
||||||
eps[key] = ep
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
r.Lock()
|
|
||||||
defer r.Unlock()
|
|
||||||
|
|
||||||
// delete any existing eps for services we know
|
|
||||||
for key, service := range r.eps {
|
|
||||||
// skip what we don't care about
|
|
||||||
if !names[service.Name] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// ok we know this thing
|
|
||||||
// delete delete delete
|
|
||||||
delete(r.eps, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// now set the eps we have
|
|
||||||
for name, endpoint := range eps {
|
|
||||||
r.eps[name] = endpoint
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// watch for endpoint changes
|
|
||||||
func (r *registryRouter) watch() {
|
|
||||||
var attempts int
|
|
||||||
|
|
||||||
for {
|
|
||||||
if r.isClosed() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// watch for changes
|
|
||||||
w, err := r.opts.Registry.Watch()
|
|
||||||
if err != nil {
|
|
||||||
attempts++
|
|
||||||
log.Println("Error watching endpoints", err)
|
|
||||||
time.Sleep(time.Duration(attempts) * time.Second)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ch := make(chan bool)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-ch:
|
|
||||||
w.Stop()
|
|
||||||
case <-r.exit:
|
|
||||||
w.Stop()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// reset if we get here
|
|
||||||
attempts = 0
|
|
||||||
|
|
||||||
for {
|
|
||||||
// process next event
|
|
||||||
res, err := w.Next()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error getting next endpoint", err)
|
|
||||||
close(ch)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
r.process(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *registryRouter) Options() router.Options {
|
|
||||||
return r.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *registryRouter) Close() error {
|
|
||||||
select {
|
|
||||||
case <-r.exit:
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
close(r.exit)
|
|
||||||
r.rc.Stop()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
|
|
||||||
if r.isClosed() {
|
|
||||||
return nil, errors.New("router closed")
|
|
||||||
}
|
|
||||||
|
|
||||||
r.RLock()
|
|
||||||
defer r.RUnlock()
|
|
||||||
|
|
||||||
// use the first match
|
|
||||||
// TODO: weighted matching
|
|
||||||
for _, e := range r.eps {
|
|
||||||
ep := e.Endpoint
|
|
||||||
|
|
||||||
// match
|
|
||||||
var pathMatch, hostMatch, methodMatch bool
|
|
||||||
|
|
||||||
// 1. try method GET, POST, PUT, etc
|
|
||||||
// 2. try host example.com, foobar.com, etc
|
|
||||||
// 3. try path /foo/bar, /bar/baz, etc
|
|
||||||
|
|
||||||
// 1. try match method
|
|
||||||
for _, m := range ep.Method {
|
|
||||||
if req.Method == m {
|
|
||||||
methodMatch = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no match on method pass
|
|
||||||
if len(ep.Method) > 0 && !methodMatch {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. try match host
|
|
||||||
for _, h := range ep.Host {
|
|
||||||
if req.Host == h {
|
|
||||||
hostMatch = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no match on host pass
|
|
||||||
if len(ep.Host) > 0 && !hostMatch {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. try match paths
|
|
||||||
for _, p := range ep.Path {
|
|
||||||
re, err := regexp.CompilePOSIX(p)
|
|
||||||
if err == nil && re.MatchString(req.URL.Path) {
|
|
||||||
pathMatch = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no match pass
|
|
||||||
if len(ep.Path) > 0 && !pathMatch {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Percentage traffic
|
|
||||||
|
|
||||||
// we got here, so its a match
|
|
||||||
return e, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// no match
|
|
||||||
return nil, errors.New("not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *registryRouter) Route(req *http.Request) (*api.Service, error) {
|
|
||||||
if r.isClosed() {
|
|
||||||
return nil, errors.New("router closed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// try get an endpoint
|
|
||||||
ep, err := r.Endpoint(req)
|
|
||||||
if err == nil {
|
|
||||||
return ep, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// error not nil
|
|
||||||
// ignore that shit
|
|
||||||
// TODO: don't ignore that shit
|
|
||||||
|
|
||||||
// get the service name
|
|
||||||
rp, err := r.opts.Resolver.Resolve(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// service name
|
|
||||||
name := setNamespace(r.opts.Namespace, rp.Name)
|
|
||||||
|
|
||||||
// get service
|
|
||||||
services, err := r.rc.GetService(name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// only use endpoint matching when the meta handler is set aka api.Default
|
|
||||||
switch r.opts.Handler {
|
|
||||||
// rpc handlers
|
|
||||||
case "meta", "api", "rpc":
|
|
||||||
handler := r.opts.Handler
|
|
||||||
|
|
||||||
// set default handler to api
|
|
||||||
if r.opts.Handler == "meta" {
|
|
||||||
handler = "rpc"
|
|
||||||
}
|
|
||||||
|
|
||||||
// construct api service
|
|
||||||
return &api.Service{
|
|
||||||
Name: name,
|
|
||||||
Endpoint: &api.Endpoint{
|
|
||||||
Name: rp.Method,
|
|
||||||
Handler: handler,
|
|
||||||
},
|
|
||||||
Services: services,
|
|
||||||
}, nil
|
|
||||||
// http handler
|
|
||||||
case "http", "proxy", "web":
|
|
||||||
// construct api service
|
|
||||||
return &api.Service{
|
|
||||||
Name: name,
|
|
||||||
Endpoint: &api.Endpoint{
|
|
||||||
Name: req.URL.String(),
|
|
||||||
Handler: r.opts.Handler,
|
|
||||||
Host: []string{req.Host},
|
|
||||||
Method: []string{req.Method},
|
|
||||||
Path: []string{req.URL.Path},
|
|
||||||
},
|
|
||||||
Services: services,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("unknown handler")
|
|
||||||
}
|
|
||||||
|
|
||||||
func newRouter(opts ...router.Option) *registryRouter {
|
|
||||||
options := router.NewOptions(opts...)
|
|
||||||
r := ®istryRouter{
|
|
||||||
exit: make(chan bool),
|
|
||||||
opts: options,
|
|
||||||
rc: cache.New(options.Registry),
|
|
||||||
eps: make(map[string]*api.Service),
|
|
||||||
}
|
|
||||||
go r.watch()
|
|
||||||
go r.refresh()
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRouter returns the default router
|
|
||||||
func NewRouter(opts ...router.Option) router.Router {
|
|
||||||
return newRouter(opts...)
|
|
||||||
}
|
|
||||||
@@ -1,181 +0,0 @@
|
|||||||
package registry
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSetNamespace(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
namespace string
|
|
||||||
name string
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
// default dotted path
|
|
||||||
{
|
|
||||||
"go.micro.api",
|
|
||||||
"foo",
|
|
||||||
"go.micro.api.foo",
|
|
||||||
},
|
|
||||||
// dotted end
|
|
||||||
{
|
|
||||||
"go.micro.api.",
|
|
||||||
"foo",
|
|
||||||
"go.micro.api.foo",
|
|
||||||
},
|
|
||||||
// dashed end
|
|
||||||
{
|
|
||||||
"go-micro-api-",
|
|
||||||
"foo",
|
|
||||||
"go-micro-api-foo",
|
|
||||||
},
|
|
||||||
// no namespace
|
|
||||||
{
|
|
||||||
"",
|
|
||||||
"foo",
|
|
||||||
"foo",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"go-micro-api-",
|
|
||||||
"v2.foo",
|
|
||||||
"go-micro-api-v2-foo",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range testCases {
|
|
||||||
name := setNamespace(test.namespace, test.name)
|
|
||||||
if name != test.expected {
|
|
||||||
t.Fatalf("expected name %s got %s", test.expected, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRouter(t *testing.T) {
|
|
||||||
r := newRouter()
|
|
||||||
|
|
||||||
compare := func(expect, got []string) bool {
|
|
||||||
// no data to compare, return true
|
|
||||||
if len(expect) == 0 && len(got) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// no data expected but got some return false
|
|
||||||
if len(expect) == 0 && len(got) > 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// compare expected with what we got
|
|
||||||
for _, e := range expect {
|
|
||||||
var seen bool
|
|
||||||
for _, g := range got {
|
|
||||||
if e == g {
|
|
||||||
seen = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !seen {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// we're done, return true
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
testData := []struct {
|
|
||||||
e *api.Endpoint
|
|
||||||
r *http.Request
|
|
||||||
m bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
e: &api.Endpoint{
|
|
||||||
Name: "Foo.Bar",
|
|
||||||
Host: []string{"example.com"},
|
|
||||||
Method: []string{"GET"},
|
|
||||||
Path: []string{"/foo"},
|
|
||||||
},
|
|
||||||
r: &http.Request{
|
|
||||||
Host: "example.com",
|
|
||||||
Method: "GET",
|
|
||||||
URL: &url.URL{
|
|
||||||
Path: "/foo",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
m: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
e: &api.Endpoint{
|
|
||||||
Name: "Bar.Baz",
|
|
||||||
Host: []string{"example.com", "foo.com"},
|
|
||||||
Method: []string{"GET", "POST"},
|
|
||||||
Path: []string{"/foo/bar"},
|
|
||||||
},
|
|
||||||
r: &http.Request{
|
|
||||||
Host: "foo.com",
|
|
||||||
Method: "POST",
|
|
||||||
URL: &url.URL{
|
|
||||||
Path: "/foo/bar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
m: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
e: &api.Endpoint{
|
|
||||||
Name: "Test.Cruft",
|
|
||||||
Host: []string{"example.com", "foo.com"},
|
|
||||||
Method: []string{"GET", "POST"},
|
|
||||||
Path: []string{"/xyz"},
|
|
||||||
},
|
|
||||||
r: &http.Request{
|
|
||||||
Host: "fail.com",
|
|
||||||
Method: "DELETE",
|
|
||||||
URL: &url.URL{
|
|
||||||
Path: "/test/fail",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
m: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, d := range testData {
|
|
||||||
key := fmt.Sprintf("%s:%s", "test.service", d.e.Name)
|
|
||||||
r.eps[key] = &api.Service{
|
|
||||||
Endpoint: d.e,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, d := range testData {
|
|
||||||
e, err := r.Endpoint(d.r)
|
|
||||||
if d.m && err != nil {
|
|
||||||
t.Fatalf("expected match, got %v", err)
|
|
||||||
}
|
|
||||||
if !d.m && err == nil {
|
|
||||||
t.Fatal("expected error got match")
|
|
||||||
}
|
|
||||||
// skip testing the non match
|
|
||||||
if !d.m {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ep := e.Endpoint
|
|
||||||
|
|
||||||
// test the match
|
|
||||||
if d.e.Name != ep.Name {
|
|
||||||
t.Fatalf("expected %v got %v", d.e.Name, ep.Name)
|
|
||||||
}
|
|
||||||
if ok := compare(d.e.Method, ep.Method); !ok {
|
|
||||||
t.Fatalf("expected %v got %v", d.e.Method, ep.Method)
|
|
||||||
}
|
|
||||||
if ok := compare(d.e.Path, ep.Path); !ok {
|
|
||||||
t.Fatalf("expected %v got %v", d.e.Path, ep.Path)
|
|
||||||
}
|
|
||||||
if ok := compare(d.e.Host, ep.Host); !ok {
|
|
||||||
t.Fatalf("expected %v got %v", d.e.Host, ep.Host)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,7 @@ package router
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api"
|
"github.com/unistack-org/micro/v3/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Router is used to determine an endpoint for a request
|
// Router is used to determine an endpoint for a request
|
||||||
@@ -15,6 +15,10 @@ type Router interface {
|
|||||||
Close() error
|
Close() error
|
||||||
// Endpoint returns an api.Service endpoint or an error if it does not exist
|
// Endpoint returns an api.Service endpoint or an error if it does not exist
|
||||||
Endpoint(r *http.Request) (*api.Service, error)
|
Endpoint(r *http.Request) (*api.Service, error)
|
||||||
|
// Register endpoint in router
|
||||||
|
Register(ep *api.Endpoint) error
|
||||||
|
// Deregister endpoint from router
|
||||||
|
Deregister(ep *api.Endpoint) error
|
||||||
// Route returns an api.Service route
|
// Route returns an api.Service route
|
||||||
Route(r *http.Request) (*api.Service, error)
|
Route(r *http.Request) (*api.Service, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package acme
|
package acme
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
@@ -14,7 +15,11 @@ var (
|
|||||||
|
|
||||||
// Provider is a ACME provider interface
|
// Provider is a ACME provider interface
|
||||||
type Provider interface {
|
type Provider interface {
|
||||||
NewListener(...string) (net.Listener, error)
|
Init(...Option) error
|
||||||
|
// Listen returns a new listener
|
||||||
|
Listen(...string) (net.Listener, error)
|
||||||
|
// TLSConfig returns a tls config
|
||||||
|
TLSConfig(...string) (*tls.Config, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Let's Encrypt ACME endpoints
|
// The Let's Encrypt ACME endpoints
|
||||||
|
|||||||
@@ -3,21 +3,48 @@
|
|||||||
package autocert
|
package autocert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/server/acme"
|
"github.com/unistack-org/micro/v3/api/server/acme"
|
||||||
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert
|
// autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert
|
||||||
type autocertProvider struct{}
|
type autocertProvider struct{}
|
||||||
|
|
||||||
// NewListener implements acme.Provider
|
func (a *autocertProvider) Init(opts ...acme.Option) error {
|
||||||
func (a *autocertProvider) NewListener(ACMEHosts ...string) (net.Listener, error) {
|
return nil
|
||||||
return autocert.NewListener(ACMEHosts...), nil
|
}
|
||||||
|
|
||||||
|
// Listen implements acme.Provider
|
||||||
|
func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) {
|
||||||
|
return autocert.NewListener(hosts...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TLSConfig returns a new tls config
|
||||||
|
func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
|
||||||
|
// create a new manager
|
||||||
|
m := &autocert.Manager{
|
||||||
|
Prompt: autocert.AcceptTOS,
|
||||||
|
}
|
||||||
|
if len(hosts) > 0 {
|
||||||
|
m.HostPolicy = autocert.HostWhitelist(hosts...)
|
||||||
|
}
|
||||||
|
dir := cacheDir()
|
||||||
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||||
|
if logger.V(logger.InfoLevel) {
|
||||||
|
logger.Info("warning: autocert not using a cache: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m.Cache = autocert.DirCache(dir)
|
||||||
|
}
|
||||||
|
return m.TLSConfig(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns an autocert acme.Provider
|
// New returns an autocert acme.Provider
|
||||||
func New() acme.Provider {
|
func NewProvider() acme.Provider {
|
||||||
return &autocertProvider{}
|
return &autocertProvider{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestAutocert(t *testing.T) {
|
func TestAutocert(t *testing.T) {
|
||||||
l := New()
|
l := NewProvider()
|
||||||
if _, ok := l.(*autocertProvider); !ok {
|
if _, ok := l.(*autocertProvider); !ok {
|
||||||
t.Error("New() didn't return an autocertProvider")
|
t.Error("NewProvider() didn't return an autocertProvider")
|
||||||
}
|
}
|
||||||
// TODO: Travis CI doesn't let us bind :443
|
// TODO: Travis CI doesn't let us bind :443
|
||||||
// if _, err := l.NewListener(); err != nil {
|
// if _, err := l.NewListener(); err != nil {
|
||||||
|
|||||||
37
api/server/acme/autocert/cache.go
Normal file
37
api/server/acme/autocert/cache.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package autocert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func homeDir() string {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
||||||
|
}
|
||||||
|
if h := os.Getenv("HOME"); h != "" {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
func cacheDir() string {
|
||||||
|
const base = "golang-autocert"
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "darwin":
|
||||||
|
return filepath.Join(homeDir(), "Library", "Caches", base)
|
||||||
|
case "windows":
|
||||||
|
for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
|
||||||
|
if v := os.Getenv(ev); v != "" {
|
||||||
|
return filepath.Join(v, base)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Worst case:
|
||||||
|
return filepath.Join(homeDir(), base)
|
||||||
|
}
|
||||||
|
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
|
||||||
|
return filepath.Join(xdg, base)
|
||||||
|
}
|
||||||
|
return filepath.Join(homeDir(), ".cache", base)
|
||||||
|
}
|
||||||
@@ -1,26 +1,27 @@
|
|||||||
// Package certmagic is the ACME provider from github.com/mholt/certmagic
|
// Package certmagic is the ACME provider from github.com/caddyserver/certmagic
|
||||||
package certmagic
|
package certmagic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mholt/certmagic"
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/unistack-org/micro/v3/api/server/acme"
|
||||||
"github.com/micro/go-micro/api/server/acme"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type certmagicProvider struct {
|
type certmagicProvider struct {
|
||||||
opts acme.Options
|
opts acme.Options
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *certmagicProvider) NewListener(ACMEHosts ...string) (net.Listener, error) {
|
// TODO: set self-contained options
|
||||||
certmagic.Default.CA = c.opts.CA
|
func (c *certmagicProvider) setup() {
|
||||||
|
certmagic.DefaultACME.CA = c.opts.CA
|
||||||
if c.opts.ChallengeProvider != nil {
|
if c.opts.ChallengeProvider != nil {
|
||||||
// Enabling DNS Challenge disables the other challenges
|
// Enabling DNS Challenge disables the other challenges
|
||||||
certmagic.Default.DNSProvider = c.opts.ChallengeProvider
|
certmagic.DefaultACME.DNSProvider = c.opts.ChallengeProvider
|
||||||
}
|
}
|
||||||
if c.opts.OnDemand {
|
if c.opts.OnDemand {
|
||||||
certmagic.Default.OnDemand = new(certmagic.OnDemandConfig)
|
certmagic.Default.OnDemand = new(certmagic.OnDemandConfig)
|
||||||
@@ -31,27 +32,39 @@ func (c *certmagicProvider) NewListener(ACMEHosts ...string) (net.Listener, erro
|
|||||||
}
|
}
|
||||||
// If multiple instances of the provider are running, inject some
|
// If multiple instances of the provider are running, inject some
|
||||||
// randomness so they don't collide
|
// randomness so they don't collide
|
||||||
|
// RenewalWindowRatio [0.33 - 0.50)
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
randomDuration := (7 * 24 * time.Hour) + (time.Duration(rand.Intn(504)) * time.Hour)
|
randomRatio := float64(rand.Intn(17)+33) * 0.01
|
||||||
certmagic.Default.RenewDurationBefore = randomDuration
|
certmagic.Default.RenewalWindowRatio = randomRatio
|
||||||
|
|
||||||
return certmagic.Listen(ACMEHosts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a certmagic provider
|
func (c *certmagicProvider) Listen(hosts ...string) (net.Listener, error) {
|
||||||
func New(options ...acme.Option) acme.Provider {
|
c.setup()
|
||||||
|
return certmagic.Listen(hosts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certmagicProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
|
||||||
|
c.setup()
|
||||||
|
return certmagic.TLS(hosts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *certmagicProvider) Init(opts ...acme.Option) error {
|
||||||
|
if p.opts.Cache != nil {
|
||||||
|
if _, ok := p.opts.Cache.(certmagic.Storage); !ok {
|
||||||
|
return fmt.Errorf("ACME: cache provided doesn't implement certmagic's Storage interface")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewProvider returns a certmagic provider
|
||||||
|
func NewProvider(options ...acme.Option) acme.Provider {
|
||||||
opts := acme.DefaultOptions()
|
opts := acme.DefaultOptions()
|
||||||
|
|
||||||
for _, o := range options {
|
for _, o := range options {
|
||||||
o(&opts)
|
o(&opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Cache != nil {
|
|
||||||
if _, ok := opts.Cache.(certmagic.Storage); !ok {
|
|
||||||
log.Fatal("ACME: cache provided doesn't implement certmagic's Storage interface")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &certmagicProvider{
|
return &certmagicProvider{
|
||||||
opts: opts,
|
opts: opts,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,224 +0,0 @@
|
|||||||
package certmagic
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-acme/lego/v3/providers/dns/cloudflare"
|
|
||||||
"github.com/mholt/certmagic"
|
|
||||||
"github.com/micro/go-micro/api/server/acme"
|
|
||||||
cfstore "github.com/micro/go-micro/store/cloudflare"
|
|
||||||
"github.com/micro/go-micro/sync/lock/memory"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCertMagic(t *testing.T) {
|
|
||||||
if len(os.Getenv("IN_TRAVIS_CI")) != 0 {
|
|
||||||
t.Skip("Travis doesn't let us bind :443")
|
|
||||||
}
|
|
||||||
l, err := New().NewListener()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
l.Close()
|
|
||||||
|
|
||||||
c := cloudflare.NewDefaultConfig()
|
|
||||||
c.AuthEmail = ""
|
|
||||||
c.AuthKey = ""
|
|
||||||
c.AuthToken = "test"
|
|
||||||
c.ZoneToken = "test"
|
|
||||||
|
|
||||||
p, err := cloudflare.NewDNSProviderConfig(c)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err = New(acme.AcceptToS(true),
|
|
||||||
acme.CA(acme.LetsEncryptStagingCA),
|
|
||||||
acme.ChallengeProvider(p),
|
|
||||||
).NewListener()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
l.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStorageImplementation(t *testing.T) {
|
|
||||||
apiToken, accountID := os.Getenv("CF_API_TOKEN"), os.Getenv("CF_ACCOUNT_ID")
|
|
||||||
kvID := os.Getenv("KV_NAMESPACE_ID")
|
|
||||||
if len(apiToken) == 0 || len(accountID) == 0 || len(kvID) == 0 {
|
|
||||||
t.Skip("No Cloudflare API keys available, skipping test")
|
|
||||||
}
|
|
||||||
|
|
||||||
var s certmagic.Storage
|
|
||||||
st := cfstore.NewStore(
|
|
||||||
cfstore.Token(apiToken),
|
|
||||||
cfstore.Account(accountID),
|
|
||||||
cfstore.Namespace(kvID),
|
|
||||||
)
|
|
||||||
s = &storage{
|
|
||||||
lock: memory.NewLock(),
|
|
||||||
store: st,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Lock
|
|
||||||
if err := s.Lock("test"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Unlock
|
|
||||||
if err := s.Unlock("test"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test data
|
|
||||||
testdata := []struct {
|
|
||||||
key string
|
|
||||||
value []byte
|
|
||||||
}{
|
|
||||||
{key: "/foo/a", value: []byte("lorem")},
|
|
||||||
{key: "/foo/b", value: []byte("ipsum")},
|
|
||||||
{key: "/foo/c", value: []byte("dolor")},
|
|
||||||
{key: "/foo/d", value: []byte("sit")},
|
|
||||||
{key: "/bar/a", value: []byte("amet")},
|
|
||||||
{key: "/bar/b", value: []byte("consectetur")},
|
|
||||||
{key: "/bar/c", value: []byte("adipiscing")},
|
|
||||||
{key: "/bar/d", value: []byte("elit")},
|
|
||||||
{key: "/foo/bar/a", value: []byte("sed")},
|
|
||||||
{key: "/foo/bar/b", value: []byte("do")},
|
|
||||||
{key: "/foo/bar/c", value: []byte("eiusmod")},
|
|
||||||
{key: "/foo/bar/d", value: []byte("tempor")},
|
|
||||||
{key: "/foo/bar/baz/a", value: []byte("incididunt")},
|
|
||||||
{key: "/foo/bar/baz/b", value: []byte("ut")},
|
|
||||||
{key: "/foo/bar/baz/c", value: []byte("labore")},
|
|
||||||
{key: "/foo/bar/baz/d", value: []byte("et")},
|
|
||||||
// a duplicate just in case there's any edge cases
|
|
||||||
{key: "/foo/a", value: []byte("lorem")},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Store
|
|
||||||
for _, d := range testdata {
|
|
||||||
if err := s.Store(d.key, d.value); err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Load
|
|
||||||
for _, d := range testdata {
|
|
||||||
if value, err := s.Load(d.key); err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
} else {
|
|
||||||
if !reflect.DeepEqual(value, d.value) {
|
|
||||||
t.Fatalf("Load %s: expected %v, got %v", d.key, d.value, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Exists
|
|
||||||
for _, d := range testdata {
|
|
||||||
if !s.Exists(d.key) {
|
|
||||||
t.Fatalf("%s should exist, but doesn't\n", d.key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test List
|
|
||||||
if list, err := s.List("/", true); err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
} else {
|
|
||||||
var expected []string
|
|
||||||
for i, d := range testdata {
|
|
||||||
if i != len(testdata)-1 {
|
|
||||||
// Don't store the intentionally duplicated key
|
|
||||||
expected = append(expected, d.key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Strings(expected)
|
|
||||||
sort.Strings(list)
|
|
||||||
if !reflect.DeepEqual(expected, list) {
|
|
||||||
t.Fatalf("List: Expected %v, got %v\n", expected, list)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if list, err := s.List("/foo", false); err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
} else {
|
|
||||||
sort.Strings(list)
|
|
||||||
expected := []string{"/foo/a", "/foo/b", "/foo/bar", "/foo/c", "/foo/d"}
|
|
||||||
if !reflect.DeepEqual(expected, list) {
|
|
||||||
t.Fatalf("List: expected %s, got %s\n", expected, list)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Stat
|
|
||||||
for _, d := range testdata {
|
|
||||||
info, err := s.Stat(d.key)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
} else {
|
|
||||||
if info.Key != d.key {
|
|
||||||
t.Fatalf("Stat().Key: expected %s, got %s\n", d.key, info.Key)
|
|
||||||
}
|
|
||||||
if info.Size != int64(len(d.value)) {
|
|
||||||
t.Fatalf("Stat().Size: expected %d, got %d\n", len(d.value), info.Size)
|
|
||||||
}
|
|
||||||
if time.Since(info.Modified) > time.Minute {
|
|
||||||
t.Fatalf("Stat().Modified: expected time since last modified to be < 1 minute, got %v\n", time.Since(info.Modified))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test Delete
|
|
||||||
for _, d := range testdata {
|
|
||||||
if err := s.Delete(d.key); err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// New interface doesn't return an error, so call it in case any log.Fatal
|
|
||||||
// happens
|
|
||||||
New(acme.Cache(s))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Full test with a real zone, with against LE staging
|
|
||||||
func TestE2e(t *testing.T) {
|
|
||||||
apiToken, accountID := os.Getenv("CF_API_TOKEN"), os.Getenv("CF_ACCOUNT_ID")
|
|
||||||
kvID := os.Getenv("KV_NAMESPACE_ID")
|
|
||||||
if len(apiToken) == 0 || len(accountID) == 0 || len(kvID) == 0 {
|
|
||||||
t.Skip("No Cloudflare API keys available, skipping test")
|
|
||||||
}
|
|
||||||
|
|
||||||
testLock := memory.NewLock()
|
|
||||||
testStore := cfstore.NewStore(
|
|
||||||
cfstore.Token(apiToken),
|
|
||||||
cfstore.Account(accountID),
|
|
||||||
cfstore.Namespace(kvID),
|
|
||||||
)
|
|
||||||
testStorage := NewStorage(testLock, testStore)
|
|
||||||
|
|
||||||
conf := cloudflare.NewDefaultConfig()
|
|
||||||
conf.AuthToken = apiToken
|
|
||||||
conf.ZoneToken = apiToken
|
|
||||||
testChallengeProvider, err := cloudflare.NewDNSProviderConfig(conf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
testProvider := New(
|
|
||||||
acme.AcceptToS(true),
|
|
||||||
acme.Cache(testStorage),
|
|
||||||
acme.CA(acme.LetsEncryptStagingCA),
|
|
||||||
acme.ChallengeProvider(testChallengeProvider),
|
|
||||||
acme.OnDemand(false),
|
|
||||||
)
|
|
||||||
|
|
||||||
listener, err := testProvider.NewListener("*.micro.mu", "micro.mu")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
go http.Serve(listener, http.NotFoundHandler())
|
|
||||||
time.Sleep(10 * time.Minute)
|
|
||||||
}
|
|
||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mholt/certmagic"
|
"github.com/caddyserver/certmagic"
|
||||||
"github.com/micro/go-micro/store"
|
"github.com/unistack-org/micro/v3/store"
|
||||||
"github.com/micro/go-micro/sync/lock"
|
"github.com/unistack-org/micro/v3/sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// File represents a "File" that will be stored in store.Store - the contents and last modified time
|
// File represents a "File" that will be stored in store.Store - the contents and last modified time
|
||||||
@@ -26,16 +26,16 @@ type File struct {
|
|||||||
// As certmagic storage expects a filesystem (with stat() abilities) we have to implement
|
// As certmagic storage expects a filesystem (with stat() abilities) we have to implement
|
||||||
// the bare minimum of metadata.
|
// the bare minimum of metadata.
|
||||||
type storage struct {
|
type storage struct {
|
||||||
lock lock.Lock
|
lock sync.Sync
|
||||||
store store.Store
|
store store.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Lock(key string) error {
|
func (s *storage) Lock(key string) error {
|
||||||
return s.lock.Acquire(key, lock.TTL(10*time.Minute))
|
return s.lock.Lock(key, sync.LockTTL(10*time.Minute))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Unlock(key string) error {
|
func (s *storage) Unlock(key string) error {
|
||||||
return s.lock.Release(key)
|
return s.lock.Unlock(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Store(key string, value []byte) error {
|
func (s *storage) Store(key string, value []byte) error {
|
||||||
@@ -52,14 +52,14 @@ func (s *storage) Store(key string, value []byte) error {
|
|||||||
Key: key,
|
Key: key,
|
||||||
Value: buf.Bytes(),
|
Value: buf.Bytes(),
|
||||||
}
|
}
|
||||||
return s.store.Write(r)
|
return s.store.Write(s.store.Options().Context, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Load(key string) ([]byte, error) {
|
func (s *storage) Load(key string) ([]byte, error) {
|
||||||
if !s.Exists(key) {
|
if !s.Exists(key) {
|
||||||
return nil, certmagic.ErrNotExist(errors.New(key + " doesn't exist"))
|
return nil, certmagic.ErrNotExist(errors.New(key + " doesn't exist"))
|
||||||
}
|
}
|
||||||
records, err := s.store.Read(key)
|
records, err := s.store.Read(s.store.Options().Context, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -77,26 +77,27 @@ func (s *storage) Load(key string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Delete(key string) error {
|
func (s *storage) Delete(key string) error {
|
||||||
return s.store.Delete(key)
|
return s.store.Delete(s.store.Options().Context, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Exists(key string) bool {
|
func (s *storage) Exists(key string) bool {
|
||||||
_, err := s.store.Read(key)
|
if _, err := s.store.Read(s.store.Options().Context, key); err != nil {
|
||||||
if err != nil {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) List(prefix string, recursive bool) ([]string, error) {
|
func (s *storage) List(prefix string, recursive bool) ([]string, error) {
|
||||||
records, err := s.store.List()
|
keys, err := s.store.List(s.store.Options().Context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//nolint:prealloc
|
||||||
var results []string
|
var results []string
|
||||||
for _, r := range records {
|
for _, k := range keys {
|
||||||
if strings.HasPrefix(r.Key, prefix) {
|
if strings.HasPrefix(k, prefix) {
|
||||||
results = append(results, r.Key)
|
results = append(results, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if recursive {
|
if recursive {
|
||||||
@@ -115,7 +116,7 @@ func (s *storage) List(prefix string, recursive bool) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Stat(key string) (certmagic.KeyInfo, error) {
|
func (s *storage) Stat(key string) (certmagic.KeyInfo, error) {
|
||||||
records, err := s.store.Read(key)
|
records, err := s.store.Read(s.store.Options().Context, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return certmagic.KeyInfo{}, err
|
return certmagic.KeyInfo{}, err
|
||||||
}
|
}
|
||||||
@@ -138,7 +139,7 @@ func (s *storage) Stat(key string) (certmagic.KeyInfo, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewStorage returns a certmagic.Storage backed by a go-micro/lock and go-micro/store
|
// NewStorage returns a certmagic.Storage backed by a go-micro/lock and go-micro/store
|
||||||
func NewStorage(lock lock.Lock, store store.Store) certmagic.Storage {
|
func NewStorage(lock sync.Sync, store store.Store) certmagic.Storage {
|
||||||
return &storage{
|
return &storage{
|
||||||
lock: lock,
|
lock: lock,
|
||||||
store: store,
|
store: store,
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func OnDemand(b bool) Option {
|
|||||||
|
|
||||||
// Cache provides a cache / storage interface to the underlying ACME library
|
// Cache provides a cache / storage interface to the underlying ACME library
|
||||||
// as there is no standard, this needs to be validated by the underlying
|
// as there is no standard, this needs to be validated by the underlying
|
||||||
// implentation.
|
// implementation
|
||||||
func Cache(c interface{}) Option {
|
func Cache(c interface{}) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Cache = c
|
o.Cache = c
|
||||||
|
|||||||
44
api/server/cors/cors.go
Normal file
44
api/server/cors/cors.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package cors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CombinedCORSHandler wraps a server and provides CORS headers
|
||||||
|
func CombinedCORSHandler(h http.Handler) http.Handler {
|
||||||
|
return corsHandler{h}
|
||||||
|
}
|
||||||
|
|
||||||
|
type corsHandler struct {
|
||||||
|
handler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c corsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
SetHeaders(w, r)
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.handler.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeaders sets the CORS headers
|
||||||
|
func SetHeaders(w http.ResponseWriter, r *http.Request) {
|
||||||
|
set := func(w http.ResponseWriter, k, v string) {
|
||||||
|
if v := w.Header().Get(k); len(v) > 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if origin := r.Header.Get("Origin"); len(origin) > 0 {
|
||||||
|
set(w, "Access-Control-Allow-Origin", origin)
|
||||||
|
} else {
|
||||||
|
set(w, "Access-Control-Allow-Origin", "*")
|
||||||
|
}
|
||||||
|
|
||||||
|
set(w, "Access-Control-Allow-Credentials", "true")
|
||||||
|
set(w, "Access-Control-Allow-Methods", "POST, PATCH, GET, OPTIONS, PUT, DELETE")
|
||||||
|
set(w, "Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||||
|
}
|
||||||
@@ -5,26 +5,24 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/handlers"
|
"github.com/unistack-org/micro/v3/api/server"
|
||||||
"github.com/micro/go-micro/api/server"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/micro/go-micro/util/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpServer struct {
|
type httpServer struct {
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
opts server.Options
|
opts server.Options
|
||||||
|
|
||||||
mtx sync.RWMutex
|
sync.RWMutex
|
||||||
address string
|
address string
|
||||||
exit chan chan error
|
exit chan chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(address string) server.Server {
|
func NewServer(address string, opts ...server.Option) server.Server {
|
||||||
return &httpServer{
|
return &httpServer{
|
||||||
opts: server.Options{},
|
opts: server.NewOptions(opts...),
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
address: address,
|
address: address,
|
||||||
exit: make(chan chan error),
|
exit: make(chan chan error),
|
||||||
@@ -32,8 +30,8 @@ func NewServer(address string) server.Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *httpServer) Address() string {
|
func (s *httpServer) Address() string {
|
||||||
s.mtx.RLock()
|
s.RLock()
|
||||||
defer s.mtx.RUnlock()
|
defer s.RUnlock()
|
||||||
return s.address
|
return s.address
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,16 +43,26 @@ func (s *httpServer) Init(opts ...server.Option) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *httpServer) Handle(path string, handler http.Handler) {
|
func (s *httpServer) Handle(path string, handler http.Handler) {
|
||||||
s.mux.Handle(path, handlers.CombinedLoggingHandler(os.Stdout, handler))
|
// TODO: move this stuff out to one place with ServeHTTP
|
||||||
|
|
||||||
|
// apply the wrappers, e.g. auth
|
||||||
|
for _, wrapper := range s.opts.Wrappers {
|
||||||
|
handler = wrapper(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mux.Handle(path, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *httpServer) Start() error {
|
func (s *httpServer) Start() error {
|
||||||
var l net.Listener
|
var l net.Listener
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
s.RLock()
|
||||||
|
config := s.opts
|
||||||
|
s.RUnlock()
|
||||||
if s.opts.EnableACME && s.opts.ACMEProvider != nil {
|
if s.opts.EnableACME && s.opts.ACMEProvider != nil {
|
||||||
// should we check the address to make sure its using :443?
|
// should we check the address to make sure its using :443?
|
||||||
l, err = s.opts.ACMEProvider.NewListener(s.opts.ACMEHosts...)
|
l, err = s.opts.ACMEProvider.Listen(s.opts.ACMEHosts...)
|
||||||
} else if s.opts.EnableTLS && s.opts.TLSConfig != nil {
|
} else if s.opts.EnableTLS && s.opts.TLSConfig != nil {
|
||||||
l, err = tls.Listen("tcp", s.address, s.opts.TLSConfig)
|
l, err = tls.Listen("tcp", s.address, s.opts.TLSConfig)
|
||||||
} else {
|
} else {
|
||||||
@@ -65,16 +73,21 @@ func (s *httpServer) Start() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Logf("HTTP API Listening on %s", l.Addr().String())
|
if config.Logger.V(logger.InfoLevel) {
|
||||||
|
config.Logger.Info("HTTP API Listening on %s", l.Addr().String())
|
||||||
|
}
|
||||||
|
|
||||||
s.mtx.Lock()
|
s.Lock()
|
||||||
s.address = l.Addr().String()
|
s.address = l.Addr().String()
|
||||||
s.mtx.Unlock()
|
s.Unlock()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := http.Serve(l, s.mux); err != nil {
|
if err := http.Serve(l, s.mux); err != nil {
|
||||||
// temporary fix
|
// temporary fix
|
||||||
//log.Fatal(err)
|
if config.Logger.V(logger.ErrorLevel) {
|
||||||
|
config.Logger.Error("serve err: %v", err)
|
||||||
|
}
|
||||||
|
s.Stop()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -2,18 +2,52 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/micro/go-micro/api/server/acme"
|
"github.com/unistack-org/micro/v3/api/resolver"
|
||||||
|
"github.com/unistack-org/micro/v3/api/server/acme"
|
||||||
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Option func
|
||||||
type Option func(o *Options)
|
type Option func(o *Options)
|
||||||
|
|
||||||
|
// Options for api server
|
||||||
type Options struct {
|
type Options struct {
|
||||||
EnableACME bool
|
EnableACME bool
|
||||||
|
EnableCORS bool
|
||||||
ACMEProvider acme.Provider
|
ACMEProvider acme.Provider
|
||||||
EnableTLS bool
|
EnableTLS bool
|
||||||
ACMEHosts []string
|
ACMEHosts []string
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
|
Resolver resolver.Resolver
|
||||||
|
Wrappers []Wrapper
|
||||||
|
Logger logger.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOptions returns new Options
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
options := Options{
|
||||||
|
Logger: logger.DefaultLogger,
|
||||||
|
}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
type Wrapper func(h http.Handler) http.Handler
|
||||||
|
|
||||||
|
func WrapHandler(w ...Wrapper) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Wrappers = append(o.Wrappers, w...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func EnableCORS(b bool) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.EnableCORS = b
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnableACME(b bool) Option {
|
func EnableACME(b bool) Option {
|
||||||
@@ -45,3 +79,15 @@ func TLSConfig(t *tls.Config) Option {
|
|||||||
o.TLSConfig = t
|
o.TLSConfig = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Resolver(r resolver.Resolver) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Resolver = r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Logger(l logger.Logger) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Logger = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
134
auth/auth.go
Normal file
134
auth/auth.go
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
// Package auth provides authentication and authorization capability
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// BearerScheme used for Authorization header
|
||||||
|
BearerScheme = "Bearer "
|
||||||
|
// ScopePublic is the scope applied to a rule to allow access to the public
|
||||||
|
ScopePublic = ""
|
||||||
|
// ScopeAccount is the scope applied to a rule to limit to users with any valid account
|
||||||
|
ScopeAccount = "*"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultAuth Auth = &NoopAuth{opts: NewOptions()}
|
||||||
|
// ErrInvalidToken is when the token provided is not valid
|
||||||
|
ErrInvalidToken = errors.New("invalid token provided")
|
||||||
|
// ErrForbidden is when a user does not have the necessary scope to access a resource
|
||||||
|
ErrForbidden = errors.New("resource forbidden")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Auth provides authentication and authorization
|
||||||
|
type Auth interface {
|
||||||
|
// Init the auth
|
||||||
|
Init(opts ...Option)
|
||||||
|
// Options set for auth
|
||||||
|
Options() Options
|
||||||
|
// Generate a new account
|
||||||
|
Generate(id string, opts ...GenerateOption) (*Account, error)
|
||||||
|
// Verify an account has access to a resource using the rules
|
||||||
|
Verify(acc *Account, res *Resource, opts ...VerifyOption) error
|
||||||
|
// Inspect a token
|
||||||
|
Inspect(token string) (*Account, error)
|
||||||
|
// Token generated using refresh token or credentials
|
||||||
|
Token(opts ...TokenOption) (*Token, error)
|
||||||
|
// Grant access to a resource
|
||||||
|
Grant(rule *Rule) error
|
||||||
|
// Revoke access to a resource
|
||||||
|
Revoke(rule *Rule) error
|
||||||
|
// Rules returns all the rules used to verify requests
|
||||||
|
Rules(...RulesOption) ([]*Rule, error)
|
||||||
|
// String returns the name of the implementation
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Account provided by an auth provider
|
||||||
|
type Account struct {
|
||||||
|
// ID of the account e.g. email
|
||||||
|
ID string `json:"id"`
|
||||||
|
// Type of the account, e.g. service
|
||||||
|
Type string `json:"type"`
|
||||||
|
// Issuer of the account
|
||||||
|
Issuer string `json:"issuer"`
|
||||||
|
// Any other associated metadata
|
||||||
|
Metadata map[string]string `json:"metadata"`
|
||||||
|
// Scopes the account has access to
|
||||||
|
Scopes []string `json:"scopes"`
|
||||||
|
// Secret for the account, e.g. the password
|
||||||
|
Secret string `json:"secret"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token can be short or long lived
|
||||||
|
type Token struct {
|
||||||
|
// The token to be used for accessing resources
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
// RefreshToken to be used to generate a new token
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
// Time of token creation
|
||||||
|
Created time.Time `json:"created"`
|
||||||
|
// Time of token expiry
|
||||||
|
Expiry time.Time `json:"expiry"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expired returns a boolean indicating if the token needs to be refreshed
|
||||||
|
func (t *Token) Expired() bool {
|
||||||
|
return t.Expiry.Unix() < time.Now().Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resource is an entity such as a user or
|
||||||
|
type Resource struct {
|
||||||
|
// Name of the resource, e.g. go.micro.service.notes
|
||||||
|
Name string `json:"name"`
|
||||||
|
// Type of resource, e.g. service
|
||||||
|
Type string `json:"type"`
|
||||||
|
// Endpoint resource e.g NotesService.Create
|
||||||
|
Endpoint string `json:"endpoint"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Access defines the type of access a rule grants
|
||||||
|
type Access int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// AccessGranted to a resource
|
||||||
|
AccessGranted Access = iota
|
||||||
|
// AccessDenied to a resource
|
||||||
|
AccessDenied
|
||||||
|
)
|
||||||
|
|
||||||
|
// Rule is used to verify access to a resource
|
||||||
|
type Rule struct {
|
||||||
|
// ID of the rule, e.g. "public"
|
||||||
|
ID string
|
||||||
|
// Scope the rule requires, a blank scope indicates open to the public and * indicates the rule
|
||||||
|
// applies to any valid account
|
||||||
|
Scope string
|
||||||
|
// Resource the rule applies to
|
||||||
|
Resource *Resource
|
||||||
|
// Access determines if the rule grants or denies access to the resource
|
||||||
|
Access Access
|
||||||
|
// Priority the rule should take when verifying a request, the higher the value the sooner the
|
||||||
|
// rule will be applied
|
||||||
|
Priority int32
|
||||||
|
}
|
||||||
|
|
||||||
|
type accountKey struct{}
|
||||||
|
|
||||||
|
// AccountFromContext gets the account from the context, which
|
||||||
|
// is set by the auth wrapper at the start of a call. If the account
|
||||||
|
// is not set, a nil account will be returned. The error is only returned
|
||||||
|
// when there was a problem retrieving an account
|
||||||
|
func AccountFromContext(ctx context.Context) (*Account, bool) {
|
||||||
|
acc, ok := ctx.Value(accountKey{}).(*Account)
|
||||||
|
return acc, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContextWithAccount sets the account in the context
|
||||||
|
func ContextWithAccount(ctx context.Context, account *Account) context.Context {
|
||||||
|
return context.WithValue(ctx, accountKey{}, account)
|
||||||
|
}
|
||||||
73
auth/noop.go
Normal file
73
auth/noop.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NoopAuth struct {
|
||||||
|
opts Options
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the name of the implementation
|
||||||
|
func (n *NoopAuth) String() string {
|
||||||
|
return "noop"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init the auth
|
||||||
|
func (n *NoopAuth) Init(opts ...Option) {
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&n.opts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options set for auth
|
||||||
|
func (n *NoopAuth) Options() Options {
|
||||||
|
return n.opts
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a new account
|
||||||
|
func (n *NoopAuth) Generate(id string, opts ...GenerateOption) (*Account, error) {
|
||||||
|
options := NewGenerateOptions(opts...)
|
||||||
|
|
||||||
|
return &Account{
|
||||||
|
ID: id,
|
||||||
|
Secret: options.Secret,
|
||||||
|
Metadata: options.Metadata,
|
||||||
|
Scopes: options.Scopes,
|
||||||
|
Issuer: n.Options().Issuer,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grant access to a resource
|
||||||
|
func (n *NoopAuth) Grant(rule *Rule) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Revoke access to a resource
|
||||||
|
func (n *NoopAuth) Revoke(rule *Rule) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rules used to verify requests
|
||||||
|
func (n *NoopAuth) Rules(opts ...RulesOption) ([]*Rule, error) {
|
||||||
|
return []*Rule{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify an account has access to a resource
|
||||||
|
func (n *NoopAuth) Verify(acc *Account, res *Resource, opts ...VerifyOption) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inspect a token
|
||||||
|
func (n *NoopAuth) Inspect(token string) (*Account, error) {
|
||||||
|
uid, err := uuid.NewRandom()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Account{ID: uid.String(), Issuer: n.Options().Issuer}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token generation using an account id and secret
|
||||||
|
func (n *NoopAuth) Token(opts ...TokenOption) (*Token, error) {
|
||||||
|
return &Token{}, nil
|
||||||
|
}
|
||||||
262
auth/options.go
Normal file
262
auth/options.go
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
|
"github.com/unistack-org/micro/v3/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
var options Options
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
// Issuer of the service's account
|
||||||
|
Issuer string
|
||||||
|
// ID is the services auth ID
|
||||||
|
ID string
|
||||||
|
// Secret is used to authenticate the service
|
||||||
|
Secret string
|
||||||
|
// Token is the services token used to authenticate itself
|
||||||
|
Token *Token
|
||||||
|
// PublicKey for decoding JWTs
|
||||||
|
PublicKey string
|
||||||
|
// PrivateKey for encoding JWTs
|
||||||
|
PrivateKey string
|
||||||
|
// LoginURL is the relative url path where a user can login
|
||||||
|
LoginURL string
|
||||||
|
// Store to back auth
|
||||||
|
Store store.Store
|
||||||
|
// Addrs sets the addresses of auth
|
||||||
|
Addrs []string
|
||||||
|
// Logger sets the logger
|
||||||
|
Logger logger.Logger
|
||||||
|
// Context to store other options
|
||||||
|
Context context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(o *Options)
|
||||||
|
|
||||||
|
// Addrs is the auth addresses to use
|
||||||
|
func Addrs(addrs ...string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Addrs = addrs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Issuer of the services account
|
||||||
|
func Issuer(i string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Issuer = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store to back auth
|
||||||
|
func Store(s store.Store) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Store = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicKey is the JWT public key
|
||||||
|
func PublicKey(key string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.PublicKey = key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivateKey is the JWT private key
|
||||||
|
func PrivateKey(key string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.PrivateKey = key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Credentials sets the auth credentials
|
||||||
|
func Credentials(id, secret string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.ID = id
|
||||||
|
o.Secret = secret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClientToken sets the auth token to use when making requests
|
||||||
|
func ClientToken(token *Token) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Token = token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoginURL sets the auth LoginURL
|
||||||
|
func LoginURL(url string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.LoginURL = url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type GenerateOptions struct {
|
||||||
|
// Metadata associated with the account
|
||||||
|
Metadata map[string]string
|
||||||
|
// Scopes the account has access too
|
||||||
|
Scopes []string
|
||||||
|
// Provider of the account, e.g. oauth
|
||||||
|
Provider string
|
||||||
|
// Type of the account, e.g. user
|
||||||
|
Type string
|
||||||
|
// Secret used to authenticate the account
|
||||||
|
Secret string
|
||||||
|
// Issuer of the account, e.g. micro
|
||||||
|
Issuer string
|
||||||
|
}
|
||||||
|
|
||||||
|
type GenerateOption func(o *GenerateOptions)
|
||||||
|
|
||||||
|
// WithSecret for the generated account
|
||||||
|
func WithSecret(s string) GenerateOption {
|
||||||
|
return func(o *GenerateOptions) {
|
||||||
|
o.Secret = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithType for the generated account
|
||||||
|
func WithType(t string) GenerateOption {
|
||||||
|
return func(o *GenerateOptions) {
|
||||||
|
o.Type = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMetadata for the generated account
|
||||||
|
func WithMetadata(md map[string]string) GenerateOption {
|
||||||
|
return func(o *GenerateOptions) {
|
||||||
|
o.Metadata = md
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithProvider for the generated account
|
||||||
|
func WithProvider(p string) GenerateOption {
|
||||||
|
return func(o *GenerateOptions) {
|
||||||
|
o.Provider = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithScopes for the generated account
|
||||||
|
func WithScopes(s ...string) GenerateOption {
|
||||||
|
return func(o *GenerateOptions) {
|
||||||
|
o.Scopes = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithIssuer for the generated account
|
||||||
|
func WithIssuer(i string) GenerateOption {
|
||||||
|
return func(o *GenerateOptions) {
|
||||||
|
o.Issuer = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGenerateOptions from a slice of options
|
||||||
|
func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
|
||||||
|
var options GenerateOptions
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
type TokenOptions struct {
|
||||||
|
// ID for the account
|
||||||
|
ID string
|
||||||
|
// Secret for the account
|
||||||
|
Secret string
|
||||||
|
// RefreshToken is used to refesh a token
|
||||||
|
RefreshToken string
|
||||||
|
// Expiry is the time the token should live for
|
||||||
|
Expiry time.Duration
|
||||||
|
// Issuer of the account
|
||||||
|
Issuer string
|
||||||
|
}
|
||||||
|
|
||||||
|
type TokenOption func(o *TokenOptions)
|
||||||
|
|
||||||
|
// WithExpiry for the token
|
||||||
|
func WithExpiry(ex time.Duration) TokenOption {
|
||||||
|
return func(o *TokenOptions) {
|
||||||
|
o.Expiry = ex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCredentials(id, secret string) TokenOption {
|
||||||
|
return func(o *TokenOptions) {
|
||||||
|
o.ID = id
|
||||||
|
o.Secret = secret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithToken(rt string) TokenOption {
|
||||||
|
return func(o *TokenOptions) {
|
||||||
|
o.RefreshToken = rt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithTokenIssuer(iss string) TokenOption {
|
||||||
|
return func(o *TokenOptions) {
|
||||||
|
o.Issuer = iss
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTokenOptions from a slice of options
|
||||||
|
func NewTokenOptions(opts ...TokenOption) TokenOptions {
|
||||||
|
var options TokenOptions
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set default expiry of token
|
||||||
|
if options.Expiry == 0 {
|
||||||
|
options.Expiry = time.Minute
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
type VerifyOptions struct {
|
||||||
|
Context context.Context
|
||||||
|
Namespace string
|
||||||
|
}
|
||||||
|
|
||||||
|
type VerifyOption func(o *VerifyOptions)
|
||||||
|
|
||||||
|
func VerifyContext(ctx context.Context) VerifyOption {
|
||||||
|
return func(o *VerifyOptions) {
|
||||||
|
o.Context = ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func VerifyNamespace(ns string) VerifyOption {
|
||||||
|
return func(o *VerifyOptions) {
|
||||||
|
o.Namespace = ns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type RulesOptions struct {
|
||||||
|
Context context.Context
|
||||||
|
Namespace string
|
||||||
|
}
|
||||||
|
|
||||||
|
type RulesOption func(o *RulesOptions)
|
||||||
|
|
||||||
|
func RulesContext(ctx context.Context) RulesOption {
|
||||||
|
return func(o *RulesOptions) {
|
||||||
|
o.Context = ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RulesNamespace(ns string) RulesOption {
|
||||||
|
return func(o *RulesOptions) {
|
||||||
|
o.Namespace = ns
|
||||||
|
}
|
||||||
|
}
|
||||||
91
auth/rules.go
Normal file
91
auth/rules.go
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// VerifyAccess an account has access to a resource using the rules provided. If the account does not have
|
||||||
|
// access an error will be returned. If there are no rules provided which match the resource, an error
|
||||||
|
// will be returned
|
||||||
|
func VerifyAccess(rules []*Rule, acc *Account, res *Resource) error {
|
||||||
|
// the rule is only to be applied if the type matches the resource or is catch-all (*)
|
||||||
|
validTypes := []string{"*", res.Type}
|
||||||
|
|
||||||
|
// the rule is only to be applied if the name matches the resource or is catch-all (*)
|
||||||
|
validNames := []string{"*", res.Name}
|
||||||
|
|
||||||
|
// rules can have wildcard excludes on endpoints since this can also be a path for web services,
|
||||||
|
// e.g. /foo/* would include /foo/bar. We also want to check for wildcards and the exact endpoint
|
||||||
|
validEndpoints := []string{"*", res.Endpoint}
|
||||||
|
if comps := strings.Split(res.Endpoint, "/"); len(comps) > 1 {
|
||||||
|
for i := 1; i < len(comps)+1; i++ {
|
||||||
|
wildcard := fmt.Sprintf("%v/*", strings.Join(comps[0:i], "/"))
|
||||||
|
validEndpoints = append(validEndpoints, wildcard)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter the rules to the ones which match the criteria above
|
||||||
|
filteredRules := make([]*Rule, 0)
|
||||||
|
for _, rule := range rules {
|
||||||
|
if !include(validTypes, rule.Resource.Type) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !include(validNames, rule.Resource.Name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !include(validEndpoints, rule.Resource.Endpoint) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
filteredRules = append(filteredRules, rule)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort the filtered rules by priority, highest to lowest
|
||||||
|
sort.SliceStable(filteredRules, func(i, j int) bool {
|
||||||
|
return filteredRules[i].Priority > filteredRules[j].Priority
|
||||||
|
})
|
||||||
|
|
||||||
|
// loop through the rules and check for a rule which applies to this account
|
||||||
|
for _, rule := range filteredRules {
|
||||||
|
// a blank scope indicates the rule applies to everyone, even nil accounts
|
||||||
|
if rule.Scope == ScopePublic && rule.Access == AccessDenied {
|
||||||
|
return ErrForbidden
|
||||||
|
} else if rule.Scope == ScopePublic && rule.Access == AccessGranted {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// all further checks require an account
|
||||||
|
if acc == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// this rule applies to any account
|
||||||
|
if rule.Scope == ScopeAccount && rule.Access == AccessDenied {
|
||||||
|
return ErrForbidden
|
||||||
|
} else if rule.Scope == ScopeAccount && rule.Access == AccessGranted {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the account has the necessary scope
|
||||||
|
if include(acc.Scopes, rule.Scope) && rule.Access == AccessDenied {
|
||||||
|
return ErrForbidden
|
||||||
|
} else if include(acc.Scopes, rule.Scope) && rule.Access == AccessGranted {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if no rules matched then return forbidden
|
||||||
|
return ErrForbidden
|
||||||
|
}
|
||||||
|
|
||||||
|
// include is a helper function which checks to see if the slice contains the value. includes is
|
||||||
|
// not case sensitive.
|
||||||
|
func include(slice []string, val string) bool {
|
||||||
|
for _, s := range slice {
|
||||||
|
if strings.EqualFold(s, val) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
288
auth/rules_test.go
Normal file
288
auth/rules_test.go
Normal file
@@ -0,0 +1,288 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestVerify(t *testing.T) {
|
||||||
|
srvResource := &Resource{
|
||||||
|
Type: "service",
|
||||||
|
Name: "go.micro.service.foo",
|
||||||
|
Endpoint: "Foo.Bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
webResource := &Resource{
|
||||||
|
Type: "service",
|
||||||
|
Name: "go.micro.web.foo",
|
||||||
|
Endpoint: "/foo/bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
catchallResource := &Resource{
|
||||||
|
Type: "*",
|
||||||
|
Name: "*",
|
||||||
|
Endpoint: "*",
|
||||||
|
}
|
||||||
|
|
||||||
|
tt := []struct {
|
||||||
|
Name string
|
||||||
|
Rules []*Rule
|
||||||
|
Account *Account
|
||||||
|
Resource *Resource
|
||||||
|
Error error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "NoRules",
|
||||||
|
Rules: []*Rule{},
|
||||||
|
Account: nil,
|
||||||
|
Resource: srvResource,
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallPublicAccount",
|
||||||
|
Account: &Account{},
|
||||||
|
Resource: srvResource,
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "",
|
||||||
|
Resource: catchallResource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallPublicNoAccount",
|
||||||
|
Resource: srvResource,
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "",
|
||||||
|
Resource: catchallResource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallPrivateAccount",
|
||||||
|
Account: &Account{},
|
||||||
|
Resource: srvResource,
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallPrivateNoAccount",
|
||||||
|
Resource: srvResource,
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallServiceRuleMatch",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: &Resource{
|
||||||
|
Type: srvResource.Type,
|
||||||
|
Name: srvResource.Name,
|
||||||
|
Endpoint: "*",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallServiceRuleNoMatch",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: &Resource{
|
||||||
|
Type: srvResource.Type,
|
||||||
|
Name: "wrongname",
|
||||||
|
Endpoint: "*",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "ExactRuleValidScope",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{
|
||||||
|
Scopes: []string{"neededscope"},
|
||||||
|
},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "neededscope",
|
||||||
|
Resource: srvResource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "ExactRuleInvalidScope",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{
|
||||||
|
Scopes: []string{"neededscope"},
|
||||||
|
},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "invalidscope",
|
||||||
|
Resource: srvResource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallDenyWithAccount",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
Access: AccessDenied,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "CatchallDenyWithNoAccount",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
Access: AccessDenied,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "RulePriorityGrantFirst",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
Access: AccessGranted,
|
||||||
|
Priority: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
Access: AccessDenied,
|
||||||
|
Priority: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "RulePriorityDenyFirst",
|
||||||
|
Resource: srvResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
Access: AccessGranted,
|
||||||
|
Priority: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: catchallResource,
|
||||||
|
Access: AccessDenied,
|
||||||
|
Priority: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "WebExactEndpointValid",
|
||||||
|
Resource: webResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: webResource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "WebExactEndpointInalid",
|
||||||
|
Resource: webResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: &Resource{
|
||||||
|
Type: webResource.Type,
|
||||||
|
Name: webResource.Name,
|
||||||
|
Endpoint: "invalidendpoint",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "WebWildcardEndpoint",
|
||||||
|
Resource: webResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: &Resource{
|
||||||
|
Type: webResource.Type,
|
||||||
|
Name: webResource.Name,
|
||||||
|
Endpoint: "*",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "WebWildcardPathEndpointValid",
|
||||||
|
Resource: webResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: &Resource{
|
||||||
|
Type: webResource.Type,
|
||||||
|
Name: webResource.Name,
|
||||||
|
Endpoint: "/foo/*",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "WebWildcardPathEndpointInvalid",
|
||||||
|
Resource: webResource,
|
||||||
|
Account: &Account{},
|
||||||
|
Rules: []*Rule{
|
||||||
|
{
|
||||||
|
Scope: "*",
|
||||||
|
Resource: &Resource{
|
||||||
|
Type: webResource.Type,
|
||||||
|
Name: webResource.Name,
|
||||||
|
Endpoint: "/bar/*",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Error: ErrForbidden,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
|
if err := VerifyAccess(tc.Rules, tc.Account, tc.Resource); err != tc.Error {
|
||||||
|
t.Errorf("Expected %v but got %v", tc.Error, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,70 +1,44 @@
|
|||||||
// Package broker is an interface used for asynchronous messaging
|
// Package broker is an interface used for asynchronous messaging
|
||||||
package broker
|
package broker
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultBroker Broker = NewBroker()
|
||||||
|
)
|
||||||
|
|
||||||
// Broker is an interface used for asynchronous messaging.
|
// Broker is an interface used for asynchronous messaging.
|
||||||
type Broker interface {
|
type Broker interface {
|
||||||
Init(...Option) error
|
Init(...Option) error
|
||||||
Options() Options
|
Options() Options
|
||||||
Address() string
|
Address() string
|
||||||
Connect() error
|
Connect(context.Context) error
|
||||||
Disconnect() error
|
Disconnect(context.Context) error
|
||||||
Publish(topic string, m *Message, opts ...PublishOption) error
|
Publish(context.Context, string, *Message, ...PublishOption) error
|
||||||
Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error)
|
Subscribe(context.Context, string, Handler, ...SubscribeOption) (Subscriber, error)
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler is used to process messages via a subscription of a topic.
|
// Handler is used to process messages via a subscription of a topic.
|
||||||
// The handler is passed a publication interface which contains the
|
|
||||||
// message and optional Ack method to acknowledge receipt of the message.
|
|
||||||
type Handler func(Event) error
|
type Handler func(Event) error
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
Header map[string]string
|
|
||||||
Body []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// Event is given to a subscription handler for processing
|
// Event is given to a subscription handler for processing
|
||||||
type Event interface {
|
type Event interface {
|
||||||
Topic() string
|
Topic() string
|
||||||
Message() *Message
|
Message() *Message
|
||||||
Ack() error
|
Ack() error
|
||||||
|
Error() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Message is used to transfer data
|
||||||
|
type Message struct {
|
||||||
|
Header map[string]string // contains message metadata
|
||||||
|
Body []byte // contains message body
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscriber is a convenience return type for the Subscribe method
|
// Subscriber is a convenience return type for the Subscribe method
|
||||||
type Subscriber interface {
|
type Subscriber interface {
|
||||||
Options() SubscribeOptions
|
Options() SubscribeOptions
|
||||||
Topic() string
|
Topic() string
|
||||||
Unsubscribe() error
|
Unsubscribe(context.Context) error
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
DefaultBroker Broker = newHttpBroker()
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewBroker(opts ...Option) Broker {
|
|
||||||
return newHttpBroker(opts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Init(opts ...Option) error {
|
|
||||||
return DefaultBroker.Init(opts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Connect() error {
|
|
||||||
return DefaultBroker.Connect()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Disconnect() error {
|
|
||||||
return DefaultBroker.Disconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Publish(topic string, msg *Message, opts ...PublishOption) error {
|
|
||||||
return DefaultBroker.Publish(topic, msg, opts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
|
|
||||||
return DefaultBroker.Subscribe(topic, handler, opts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func String() string {
|
|
||||||
return DefaultBroker.String()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
package broker
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/micro/go-micro/registry"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// mock data
|
|
||||||
testData = map[string][]*registry.Service{
|
|
||||||
"foo": {
|
|
||||||
{
|
|
||||||
Name: "foo",
|
|
||||||
Version: "1.0.0",
|
|
||||||
Nodes: []*registry.Node{
|
|
||||||
{
|
|
||||||
Id: "foo-1.0.0-123",
|
|
||||||
Address: "localhost:9999",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Id: "foo-1.0.0-321",
|
|
||||||
Address: "localhost:9999",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "foo",
|
|
||||||
Version: "1.0.1",
|
|
||||||
Nodes: []*registry.Node{
|
|
||||||
{
|
|
||||||
Id: "foo-1.0.1-321",
|
|
||||||
Address: "localhost:6666",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "foo",
|
|
||||||
Version: "1.0.3",
|
|
||||||
Nodes: []*registry.Node{
|
|
||||||
{
|
|
||||||
Id: "foo-1.0.3-345",
|
|
||||||
Address: "localhost:8888",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
46
broker/context.go
Normal file
46
broker/context.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package broker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type brokerKey struct{}
|
||||||
|
|
||||||
|
func FromContext(ctx context.Context) (Broker, bool) {
|
||||||
|
c, ok := ctx.Value(brokerKey{}).(Broker)
|
||||||
|
return c, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewContext(ctx context.Context, s Broker) context.Context {
|
||||||
|
return context.WithValue(ctx, brokerKey{}, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSubscribeOption returns a function to setup a context with given value
|
||||||
|
func SetSubscribeOption(k, v interface{}) SubscribeOption {
|
||||||
|
return func(o *SubscribeOptions) {
|
||||||
|
if o.Context == nil {
|
||||||
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOption returns a function to setup a context with given value
|
||||||
|
func SetOption(k, v interface{}) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
if o.Context == nil {
|
||||||
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPublishOption returns a function to setup a context with given value
|
||||||
|
func SetPublishOption(k, v interface{}) PublishOption {
|
||||||
|
return func(o *PublishOptions) {
|
||||||
|
if o.Context == nil {
|
||||||
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
// Package http provides a http based message broker
|
|
||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewBroker returns a new http broker
|
|
||||||
func NewBroker(opts ...broker.Option) broker.Broker {
|
|
||||||
return broker.NewBroker(opts...)
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Handle registers the handler for the given pattern.
|
|
||||||
func Handle(pattern string, handler http.Handler) broker.Option {
|
|
||||||
return func(o *broker.Options) {
|
|
||||||
if o.Context == nil {
|
|
||||||
o.Context = context.Background()
|
|
||||||
}
|
|
||||||
handlers, ok := o.Context.Value("http_handlers").(map[string]http.Handler)
|
|
||||||
if !ok {
|
|
||||||
handlers = make(map[string]http.Handler)
|
|
||||||
}
|
|
||||||
handlers[pattern] = handler
|
|
||||||
o.Context = context.WithValue(o.Context, "http_handlers", handlers)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,704 +0,0 @@
|
|||||||
package broker
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"math/rand"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"runtime"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/micro/go-micro/codec/json"
|
|
||||||
merr "github.com/micro/go-micro/errors"
|
|
||||||
"github.com/micro/go-micro/registry"
|
|
||||||
"github.com/micro/go-micro/registry/cache"
|
|
||||||
maddr "github.com/micro/go-micro/util/addr"
|
|
||||||
mnet "github.com/micro/go-micro/util/net"
|
|
||||||
mls "github.com/micro/go-micro/util/tls"
|
|
||||||
"golang.org/x/net/http2"
|
|
||||||
)
|
|
||||||
|
|
||||||
// HTTP Broker is a point to point async broker
|
|
||||||
type httpBroker struct {
|
|
||||||
id string
|
|
||||||
address string
|
|
||||||
opts Options
|
|
||||||
|
|
||||||
mux *http.ServeMux
|
|
||||||
|
|
||||||
c *http.Client
|
|
||||||
r registry.Registry
|
|
||||||
|
|
||||||
sync.RWMutex
|
|
||||||
subscribers map[string][]*httpSubscriber
|
|
||||||
running bool
|
|
||||||
exit chan chan error
|
|
||||||
|
|
||||||
// offline message inbox
|
|
||||||
mtx sync.RWMutex
|
|
||||||
inbox map[string][][]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type httpSubscriber struct {
|
|
||||||
opts SubscribeOptions
|
|
||||||
id string
|
|
||||||
topic string
|
|
||||||
fn Handler
|
|
||||||
svc *registry.Service
|
|
||||||
hb *httpBroker
|
|
||||||
}
|
|
||||||
|
|
||||||
type httpEvent struct {
|
|
||||||
m *Message
|
|
||||||
t string
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
DefaultSubPath = "/_sub"
|
|
||||||
broadcastVersion = "ff.http.broadcast"
|
|
||||||
registerTTL = time.Minute
|
|
||||||
registerInterval = time.Second * 30
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rand.Seed(time.Now().Unix())
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTransport(config *tls.Config) *http.Transport {
|
|
||||||
if config == nil {
|
|
||||||
config = &tls.Config{
|
|
||||||
InsecureSkipVerify: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dialTLS := func(network string, addr string) (net.Conn, error) {
|
|
||||||
return tls.Dial(network, addr, config)
|
|
||||||
}
|
|
||||||
|
|
||||||
t := &http.Transport{
|
|
||||||
Proxy: http.ProxyFromEnvironment,
|
|
||||||
Dial: (&net.Dialer{
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
KeepAlive: 30 * time.Second,
|
|
||||||
}).Dial,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
DialTLS: dialTLS,
|
|
||||||
}
|
|
||||||
runtime.SetFinalizer(&t, func(tr **http.Transport) {
|
|
||||||
(*tr).CloseIdleConnections()
|
|
||||||
})
|
|
||||||
|
|
||||||
// setup http2
|
|
||||||
http2.ConfigureTransport(t)
|
|
||||||
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
func newHttpBroker(opts ...Option) Broker {
|
|
||||||
options := Options{
|
|
||||||
Codec: json.Marshaler{},
|
|
||||||
Context: context.TODO(),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
// set address
|
|
||||||
addr := ":0"
|
|
||||||
if len(options.Addrs) > 0 && len(options.Addrs[0]) > 0 {
|
|
||||||
addr = options.Addrs[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
// get registry
|
|
||||||
reg, ok := options.Context.Value(registryKey).(registry.Registry)
|
|
||||||
if !ok {
|
|
||||||
reg = registry.DefaultRegistry
|
|
||||||
}
|
|
||||||
|
|
||||||
h := &httpBroker{
|
|
||||||
id: "broker-" + uuid.New().String(),
|
|
||||||
address: addr,
|
|
||||||
opts: options,
|
|
||||||
r: reg,
|
|
||||||
c: &http.Client{Transport: newTransport(options.TLSConfig)},
|
|
||||||
subscribers: make(map[string][]*httpSubscriber),
|
|
||||||
exit: make(chan chan error),
|
|
||||||
mux: http.NewServeMux(),
|
|
||||||
inbox: make(map[string][][]byte),
|
|
||||||
}
|
|
||||||
|
|
||||||
// specify the message handler
|
|
||||||
h.mux.Handle(DefaultSubPath, h)
|
|
||||||
|
|
||||||
// get optional handlers
|
|
||||||
if h.opts.Context != nil {
|
|
||||||
handlers, ok := h.opts.Context.Value("http_handlers").(map[string]http.Handler)
|
|
||||||
if ok {
|
|
||||||
for pattern, handler := range handlers {
|
|
||||||
h.mux.Handle(pattern, handler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpEvent) Ack() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpEvent) Message() *Message {
|
|
||||||
return h.m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpEvent) Topic() string {
|
|
||||||
return h.t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpSubscriber) Options() SubscribeOptions {
|
|
||||||
return h.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpSubscriber) Topic() string {
|
|
||||||
return h.topic
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpSubscriber) Unsubscribe() error {
|
|
||||||
return h.hb.unsubscribe(h)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) saveMessage(topic string, msg []byte) {
|
|
||||||
h.mtx.Lock()
|
|
||||||
defer h.mtx.Unlock()
|
|
||||||
|
|
||||||
// get messages
|
|
||||||
c := h.inbox[topic]
|
|
||||||
|
|
||||||
// save message
|
|
||||||
c = append(c, msg)
|
|
||||||
|
|
||||||
// max length 64
|
|
||||||
if len(c) > 64 {
|
|
||||||
c = c[:64]
|
|
||||||
}
|
|
||||||
|
|
||||||
// save inbox
|
|
||||||
h.inbox[topic] = c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) getMessage(topic string, num int) [][]byte {
|
|
||||||
h.mtx.Lock()
|
|
||||||
defer h.mtx.Unlock()
|
|
||||||
|
|
||||||
// get messages
|
|
||||||
c, ok := h.inbox[topic]
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// more message than requests
|
|
||||||
if len(c) >= num {
|
|
||||||
msg := c[:num]
|
|
||||||
h.inbox[topic] = c[num:]
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset inbox
|
|
||||||
h.inbox[topic] = nil
|
|
||||||
|
|
||||||
// return all messages
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) subscribe(s *httpSubscriber) error {
|
|
||||||
h.Lock()
|
|
||||||
defer h.Unlock()
|
|
||||||
|
|
||||||
if err := h.r.Register(s.svc, registry.RegisterTTL(registerTTL)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
h.subscribers[s.topic] = append(h.subscribers[s.topic], s)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) unsubscribe(s *httpSubscriber) error {
|
|
||||||
h.Lock()
|
|
||||||
defer h.Unlock()
|
|
||||||
|
|
||||||
var subscribers []*httpSubscriber
|
|
||||||
|
|
||||||
// look for subscriber
|
|
||||||
for _, sub := range h.subscribers[s.topic] {
|
|
||||||
// deregister and skip forward
|
|
||||||
if sub.id == s.id {
|
|
||||||
_ = h.r.Deregister(sub.svc)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// keep subscriber
|
|
||||||
subscribers = append(subscribers, sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
// set subscribers
|
|
||||||
h.subscribers[s.topic] = subscribers
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) run(l net.Listener) {
|
|
||||||
t := time.NewTicker(registerInterval)
|
|
||||||
defer t.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
// heartbeat for each subscriber
|
|
||||||
case <-t.C:
|
|
||||||
h.RLock()
|
|
||||||
for _, subs := range h.subscribers {
|
|
||||||
for _, sub := range subs {
|
|
||||||
_ = h.r.Register(sub.svc, registry.RegisterTTL(registerTTL))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.RUnlock()
|
|
||||||
// received exit signal
|
|
||||||
case ch := <-h.exit:
|
|
||||||
ch <- l.Close()
|
|
||||||
h.RLock()
|
|
||||||
for _, subs := range h.subscribers {
|
|
||||||
for _, sub := range subs {
|
|
||||||
_ = h.r.Deregister(sub.svc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.RUnlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
||||||
if req.Method != "POST" {
|
|
||||||
err := merr.BadRequest("go.micro.broker", "Method not allowed")
|
|
||||||
http.Error(w, err.Error(), http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer req.Body.Close()
|
|
||||||
|
|
||||||
req.ParseForm()
|
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(req.Body)
|
|
||||||
if err != nil {
|
|
||||||
errr := merr.InternalServerError("go.micro.broker", "Error reading request body: %v", err)
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(errr.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var m *Message
|
|
||||||
if err = h.opts.Codec.Unmarshal(b, &m); err != nil {
|
|
||||||
errr := merr.InternalServerError("go.micro.broker", "Error parsing request body: %v", err)
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(errr.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
topic := m.Header[":topic"]
|
|
||||||
delete(m.Header, ":topic")
|
|
||||||
|
|
||||||
if len(topic) == 0 {
|
|
||||||
errr := merr.InternalServerError("go.micro.broker", "Topic not found")
|
|
||||||
w.WriteHeader(500)
|
|
||||||
w.Write([]byte(errr.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
p := &httpEvent{m: m, t: topic}
|
|
||||||
id := req.Form.Get("id")
|
|
||||||
|
|
||||||
var subs []Handler
|
|
||||||
|
|
||||||
h.RLock()
|
|
||||||
for _, subscriber := range h.subscribers[topic] {
|
|
||||||
if id != subscriber.id {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
subs = append(subs, subscriber.fn)
|
|
||||||
}
|
|
||||||
h.RUnlock()
|
|
||||||
|
|
||||||
// execute the handler
|
|
||||||
for _, fn := range subs {
|
|
||||||
fn(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) Address() string {
|
|
||||||
h.RLock()
|
|
||||||
defer h.RUnlock()
|
|
||||||
return h.address
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) Connect() error {
|
|
||||||
h.RLock()
|
|
||||||
if h.running {
|
|
||||||
h.RUnlock()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
h.RUnlock()
|
|
||||||
|
|
||||||
h.Lock()
|
|
||||||
defer h.Unlock()
|
|
||||||
|
|
||||||
var l net.Listener
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if h.opts.Secure || h.opts.TLSConfig != nil {
|
|
||||||
config := h.opts.TLSConfig
|
|
||||||
|
|
||||||
fn := func(addr string) (net.Listener, error) {
|
|
||||||
if config == nil {
|
|
||||||
hosts := []string{addr}
|
|
||||||
|
|
||||||
// check if its a valid host:port
|
|
||||||
if host, _, err := net.SplitHostPort(addr); err == nil {
|
|
||||||
if len(host) == 0 {
|
|
||||||
hosts = maddr.IPs()
|
|
||||||
} else {
|
|
||||||
hosts = []string{host}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// generate a certificate
|
|
||||||
cert, err := mls.Certificate(hosts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
config = &tls.Config{Certificates: []tls.Certificate{cert}}
|
|
||||||
}
|
|
||||||
return tls.Listen("tcp", addr, config)
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err = mnet.Listen(h.address, fn)
|
|
||||||
} else {
|
|
||||||
fn := func(addr string) (net.Listener, error) {
|
|
||||||
return net.Listen("tcp", addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err = mnet.Listen(h.address, fn)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := h.address
|
|
||||||
h.address = l.Addr().String()
|
|
||||||
|
|
||||||
go http.Serve(l, h.mux)
|
|
||||||
go func() {
|
|
||||||
h.run(l)
|
|
||||||
h.Lock()
|
|
||||||
h.opts.Addrs = []string{addr}
|
|
||||||
h.address = addr
|
|
||||||
h.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// get registry
|
|
||||||
reg, ok := h.opts.Context.Value(registryKey).(registry.Registry)
|
|
||||||
if !ok {
|
|
||||||
reg = registry.DefaultRegistry
|
|
||||||
}
|
|
||||||
// set cache
|
|
||||||
h.r = cache.New(reg)
|
|
||||||
|
|
||||||
// set running
|
|
||||||
h.running = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) Disconnect() error {
|
|
||||||
h.RLock()
|
|
||||||
if !h.running {
|
|
||||||
h.RUnlock()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
h.RUnlock()
|
|
||||||
|
|
||||||
h.Lock()
|
|
||||||
defer h.Unlock()
|
|
||||||
|
|
||||||
// stop cache
|
|
||||||
rc, ok := h.r.(cache.Cache)
|
|
||||||
if ok {
|
|
||||||
rc.Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// exit and return err
|
|
||||||
ch := make(chan error)
|
|
||||||
h.exit <- ch
|
|
||||||
err := <-ch
|
|
||||||
|
|
||||||
// set not running
|
|
||||||
h.running = false
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) Init(opts ...Option) error {
|
|
||||||
h.RLock()
|
|
||||||
if h.running {
|
|
||||||
h.RUnlock()
|
|
||||||
return errors.New("cannot init while connected")
|
|
||||||
}
|
|
||||||
h.RUnlock()
|
|
||||||
|
|
||||||
h.Lock()
|
|
||||||
defer h.Unlock()
|
|
||||||
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&h.opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(h.opts.Addrs) > 0 && len(h.opts.Addrs[0]) > 0 {
|
|
||||||
h.address = h.opts.Addrs[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(h.id) == 0 {
|
|
||||||
h.id = "broker-" + uuid.New().String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// get registry
|
|
||||||
reg, ok := h.opts.Context.Value(registryKey).(registry.Registry)
|
|
||||||
if !ok {
|
|
||||||
reg = registry.DefaultRegistry
|
|
||||||
}
|
|
||||||
|
|
||||||
// get cache
|
|
||||||
if rc, ok := h.r.(cache.Cache); ok {
|
|
||||||
rc.Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// set registry
|
|
||||||
h.r = cache.New(reg)
|
|
||||||
|
|
||||||
// reconfigure tls config
|
|
||||||
if c := h.opts.TLSConfig; c != nil {
|
|
||||||
h.c = &http.Client{
|
|
||||||
Transport: newTransport(c),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) Options() Options {
|
|
||||||
return h.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption) error {
|
|
||||||
// create the message first
|
|
||||||
m := &Message{
|
|
||||||
Header: make(map[string]string),
|
|
||||||
Body: msg.Body,
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range msg.Header {
|
|
||||||
m.Header[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Header[":topic"] = topic
|
|
||||||
|
|
||||||
// encode the message
|
|
||||||
b, err := h.opts.Codec.Marshal(m)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// save the message
|
|
||||||
h.saveMessage(topic, b)
|
|
||||||
|
|
||||||
// now attempt to get the service
|
|
||||||
h.RLock()
|
|
||||||
s, err := h.r.GetService(topic)
|
|
||||||
if err != nil {
|
|
||||||
h.RUnlock()
|
|
||||||
// ignore error
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
h.RUnlock()
|
|
||||||
|
|
||||||
pub := func(node *registry.Node, t string, b []byte) error {
|
|
||||||
scheme := "http"
|
|
||||||
|
|
||||||
// check if secure is added in metadata
|
|
||||||
if node.Metadata["secure"] == "true" {
|
|
||||||
scheme = "https"
|
|
||||||
}
|
|
||||||
|
|
||||||
vals := url.Values{}
|
|
||||||
vals.Add("id", node.Id)
|
|
||||||
|
|
||||||
uri := fmt.Sprintf("%s://%s%s?%s", scheme, node.Address, DefaultSubPath, vals.Encode())
|
|
||||||
r, err := h.c.Post(uri, "application/json", bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// discard response body
|
|
||||||
io.Copy(ioutil.Discard, r.Body)
|
|
||||||
r.Body.Close()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
srv := func(s []*registry.Service, b []byte) {
|
|
||||||
for _, service := range s {
|
|
||||||
var nodes []*registry.Node
|
|
||||||
|
|
||||||
for _, node := range service.Nodes {
|
|
||||||
// only use nodes tagged with broker http
|
|
||||||
if node.Metadata["broker"] != "http" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// look for nodes for the topic
|
|
||||||
if node.Metadata["topic"] != topic {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nodes = append(nodes, node)
|
|
||||||
}
|
|
||||||
|
|
||||||
// only process if we have nodes
|
|
||||||
if len(nodes) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
switch service.Version {
|
|
||||||
// broadcast version means broadcast to all nodes
|
|
||||||
case broadcastVersion:
|
|
||||||
var success bool
|
|
||||||
|
|
||||||
// publish to all nodes
|
|
||||||
for _, node := range nodes {
|
|
||||||
// publish async
|
|
||||||
if err := pub(node, topic, b); err == nil {
|
|
||||||
success = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// save if it failed to publish at least once
|
|
||||||
if !success {
|
|
||||||
h.saveMessage(topic, b)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// select node to publish to
|
|
||||||
node := nodes[rand.Int()%len(nodes)]
|
|
||||||
|
|
||||||
// publish async to one node
|
|
||||||
if err := pub(node, topic, b); err != nil {
|
|
||||||
// if failed save it
|
|
||||||
h.saveMessage(topic, b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// do the rest async
|
|
||||||
go func() {
|
|
||||||
// get a third of the backlog
|
|
||||||
messages := h.getMessage(topic, 8)
|
|
||||||
delay := (len(messages) > 1)
|
|
||||||
|
|
||||||
// publish all the messages
|
|
||||||
for _, msg := range messages {
|
|
||||||
// serialize here
|
|
||||||
srv(s, msg)
|
|
||||||
|
|
||||||
// sending a backlog of messages
|
|
||||||
if delay {
|
|
||||||
time.Sleep(time.Millisecond * 100)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
|
|
||||||
var err error
|
|
||||||
var host, port string
|
|
||||||
options := NewSubscribeOptions(opts...)
|
|
||||||
|
|
||||||
// parse address for host, port
|
|
||||||
host, port, err = net.SplitHostPort(h.Address())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
addr, err := maddr.Extract(host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// create unique id
|
|
||||||
id := h.id + "." + uuid.New().String()
|
|
||||||
|
|
||||||
var secure bool
|
|
||||||
|
|
||||||
if h.opts.Secure || h.opts.TLSConfig != nil {
|
|
||||||
secure = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// register service
|
|
||||||
node := ®istry.Node{
|
|
||||||
Id: id,
|
|
||||||
Address: mnet.HostPort(addr, port),
|
|
||||||
Metadata: map[string]string{
|
|
||||||
"secure": fmt.Sprintf("%t", secure),
|
|
||||||
"broker": "http",
|
|
||||||
"topic": topic,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// check for queue group or broadcast queue
|
|
||||||
version := options.Queue
|
|
||||||
if len(version) == 0 {
|
|
||||||
version = broadcastVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
service := ®istry.Service{
|
|
||||||
Name: topic,
|
|
||||||
Version: version,
|
|
||||||
Nodes: []*registry.Node{node},
|
|
||||||
}
|
|
||||||
|
|
||||||
// generate subscriber
|
|
||||||
subscriber := &httpSubscriber{
|
|
||||||
opts: options,
|
|
||||||
hb: h,
|
|
||||||
id: id,
|
|
||||||
topic: topic,
|
|
||||||
fn: handler,
|
|
||||||
svc: service,
|
|
||||||
}
|
|
||||||
|
|
||||||
// subscribe now
|
|
||||||
if err := h.subscribe(subscriber); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the subscriber
|
|
||||||
return subscriber, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *httpBroker) String() string {
|
|
||||||
return "http"
|
|
||||||
}
|
|
||||||
@@ -1,351 +0,0 @@
|
|||||||
package broker
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
glog "github.com/go-log/log"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/micro/go-micro/registry/memory"
|
|
||||||
"github.com/micro/go-micro/util/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func newTestRegistry() *memory.Registry {
|
|
||||||
r := memory.NewRegistry()
|
|
||||||
m := r.(*memory.Registry)
|
|
||||||
m.Services = testData
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func sub(be *testing.B, c int) {
|
|
||||||
// set no op logger
|
|
||||||
log.SetLogger(glog.DefaultLogger)
|
|
||||||
|
|
||||||
be.StopTimer()
|
|
||||||
m := newTestRegistry()
|
|
||||||
|
|
||||||
b := NewBroker(Registry(m))
|
|
||||||
topic := uuid.New().String()
|
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
|
||||||
be.Fatalf("Unexpected init error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Connect(); err != nil {
|
|
||||||
be.Fatalf("Unexpected connect error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &Message{
|
|
||||||
Header: map[string]string{
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
Body: []byte(`{"message": "Hello World"}`),
|
|
||||||
}
|
|
||||||
|
|
||||||
var subs []Subscriber
|
|
||||||
done := make(chan bool, c)
|
|
||||||
|
|
||||||
for i := 0; i < c; i++ {
|
|
||||||
sub, err := b.Subscribe(topic, func(p Event) error {
|
|
||||||
done <- true
|
|
||||||
m := p.Message()
|
|
||||||
|
|
||||||
if string(m.Body) != string(msg.Body) {
|
|
||||||
be.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}, Queue("shared"))
|
|
||||||
if err != nil {
|
|
||||||
be.Fatalf("Unexpected subscribe error: %v", err)
|
|
||||||
}
|
|
||||||
subs = append(subs, sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < be.N; i++ {
|
|
||||||
be.StartTimer()
|
|
||||||
if err := b.Publish(topic, msg); err != nil {
|
|
||||||
be.Fatalf("Unexpected publish error: %v", err)
|
|
||||||
}
|
|
||||||
<-done
|
|
||||||
be.StopTimer()
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sub := range subs {
|
|
||||||
sub.Unsubscribe()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Disconnect(); err != nil {
|
|
||||||
be.Fatalf("Unexpected disconnect error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func pub(be *testing.B, c int) {
|
|
||||||
// set no op logger
|
|
||||||
log.SetLogger(glog.DefaultLogger)
|
|
||||||
|
|
||||||
be.StopTimer()
|
|
||||||
m := newTestRegistry()
|
|
||||||
b := NewBroker(Registry(m))
|
|
||||||
topic := uuid.New().String()
|
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
|
||||||
be.Fatalf("Unexpected init error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Connect(); err != nil {
|
|
||||||
be.Fatalf("Unexpected connect error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &Message{
|
|
||||||
Header: map[string]string{
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
Body: []byte(`{"message": "Hello World"}`),
|
|
||||||
}
|
|
||||||
|
|
||||||
done := make(chan bool, c*4)
|
|
||||||
|
|
||||||
sub, err := b.Subscribe(topic, func(p Event) error {
|
|
||||||
done <- true
|
|
||||||
m := p.Message()
|
|
||||||
if string(m.Body) != string(msg.Body) {
|
|
||||||
be.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}, Queue("shared"))
|
|
||||||
if err != nil {
|
|
||||||
be.Fatalf("Unexpected subscribe error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
ch := make(chan int, c*4)
|
|
||||||
be.StartTimer()
|
|
||||||
|
|
||||||
for i := 0; i < c; i++ {
|
|
||||||
go func() {
|
|
||||||
for range ch {
|
|
||||||
if err := b.Publish(topic, msg); err != nil {
|
|
||||||
be.Fatalf("Unexpected publish error: %v", err)
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(time.Second):
|
|
||||||
}
|
|
||||||
wg.Done()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < be.N; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
ch <- i
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
be.StopTimer()
|
|
||||||
sub.Unsubscribe()
|
|
||||||
close(ch)
|
|
||||||
close(done)
|
|
||||||
|
|
||||||
if err := b.Disconnect(); err != nil {
|
|
||||||
be.Fatalf("Unexpected disconnect error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBroker(t *testing.T) {
|
|
||||||
m := newTestRegistry()
|
|
||||||
b := NewBroker(Registry(m))
|
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
|
||||||
t.Fatalf("Unexpected init error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Connect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected connect error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &Message{
|
|
||||||
Header: map[string]string{
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
Body: []byte(`{"message": "Hello World"}`),
|
|
||||||
}
|
|
||||||
|
|
||||||
done := make(chan bool)
|
|
||||||
|
|
||||||
sub, err := b.Subscribe("test", func(p Event) error {
|
|
||||||
m := p.Message()
|
|
||||||
|
|
||||||
if string(m.Body) != string(msg.Body) {
|
|
||||||
t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
|
||||||
}
|
|
||||||
|
|
||||||
close(done)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected subscribe error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Publish("test", msg); err != nil {
|
|
||||||
t.Fatalf("Unexpected publish error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
<-done
|
|
||||||
sub.Unsubscribe()
|
|
||||||
|
|
||||||
if err := b.Disconnect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected disconnect error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConcurrentSubBroker(t *testing.T) {
|
|
||||||
m := newTestRegistry()
|
|
||||||
b := NewBroker(Registry(m))
|
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
|
||||||
t.Fatalf("Unexpected init error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Connect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected connect error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &Message{
|
|
||||||
Header: map[string]string{
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
Body: []byte(`{"message": "Hello World"}`),
|
|
||||||
}
|
|
||||||
|
|
||||||
var subs []Subscriber
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
sub, err := b.Subscribe("test", func(p Event) error {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
m := p.Message()
|
|
||||||
|
|
||||||
if string(m.Body) != string(msg.Body) {
|
|
||||||
t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected subscribe error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Add(1)
|
|
||||||
subs = append(subs, sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Publish("test", msg); err != nil {
|
|
||||||
t.Fatalf("Unexpected publish error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
for _, sub := range subs {
|
|
||||||
sub.Unsubscribe()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Disconnect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected disconnect error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConcurrentPubBroker(t *testing.T) {
|
|
||||||
m := newTestRegistry()
|
|
||||||
b := NewBroker(Registry(m))
|
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
|
||||||
t.Fatalf("Unexpected init error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Connect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected connect error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &Message{
|
|
||||||
Header: map[string]string{
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
Body: []byte(`{"message": "Hello World"}`),
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
sub, err := b.Subscribe("test", func(p Event) error {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
m := p.Message()
|
|
||||||
|
|
||||||
if string(m.Body) != string(msg.Body) {
|
|
||||||
t.Fatalf("Unexpected msg %s, expected %s", string(m.Body), string(msg.Body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected subscribe error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
|
|
||||||
if err := b.Publish("test", msg); err != nil {
|
|
||||||
t.Fatalf("Unexpected publish error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
sub.Unsubscribe()
|
|
||||||
|
|
||||||
if err := b.Disconnect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected disconnect error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkSub1(b *testing.B) {
|
|
||||||
sub(b, 1)
|
|
||||||
}
|
|
||||||
func BenchmarkSub8(b *testing.B) {
|
|
||||||
sub(b, 8)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkSub32(b *testing.B) {
|
|
||||||
sub(b, 32)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkSub64(b *testing.B) {
|
|
||||||
sub(b, 64)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkSub128(b *testing.B) {
|
|
||||||
sub(b, 128)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkPub1(b *testing.B) {
|
|
||||||
pub(b, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkPub8(b *testing.B) {
|
|
||||||
pub(b, 8)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkPub32(b *testing.B) {
|
|
||||||
pub(b, 32)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkPub64(b *testing.B) {
|
|
||||||
pub(b, 64)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkPub128(b *testing.B) {
|
|
||||||
pub(b, 128)
|
|
||||||
}
|
|
||||||
@@ -1,197 +0,0 @@
|
|||||||
// Package memory provides a memory broker
|
|
||||||
package memory
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"math/rand"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
maddr "github.com/micro/go-micro/util/addr"
|
|
||||||
mnet "github.com/micro/go-micro/util/net"
|
|
||||||
)
|
|
||||||
|
|
||||||
type memoryBroker struct {
|
|
||||||
opts broker.Options
|
|
||||||
|
|
||||||
addr string
|
|
||||||
sync.RWMutex
|
|
||||||
connected bool
|
|
||||||
Subscribers map[string][]*memorySubscriber
|
|
||||||
}
|
|
||||||
|
|
||||||
type memoryEvent struct {
|
|
||||||
topic string
|
|
||||||
message *broker.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
type memorySubscriber struct {
|
|
||||||
id string
|
|
||||||
topic string
|
|
||||||
exit chan bool
|
|
||||||
handler broker.Handler
|
|
||||||
opts broker.SubscribeOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) Options() broker.Options {
|
|
||||||
return m.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) Address() string {
|
|
||||||
return m.addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) Connect() error {
|
|
||||||
m.Lock()
|
|
||||||
defer m.Unlock()
|
|
||||||
|
|
||||||
if m.connected {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
addr, err := maddr.Extract("::")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
i := rand.Intn(20000)
|
|
||||||
// set addr with port
|
|
||||||
addr = mnet.HostPort(addr, 10000+i)
|
|
||||||
|
|
||||||
m.addr = addr
|
|
||||||
m.connected = true
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) Disconnect() error {
|
|
||||||
m.Lock()
|
|
||||||
defer m.Unlock()
|
|
||||||
|
|
||||||
if !m.connected {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
m.connected = false
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) Init(opts ...broker.Option) error {
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&m.opts)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) Publish(topic string, message *broker.Message, opts ...broker.PublishOption) error {
|
|
||||||
m.RLock()
|
|
||||||
if !m.connected {
|
|
||||||
m.RUnlock()
|
|
||||||
return errors.New("not connected")
|
|
||||||
}
|
|
||||||
|
|
||||||
subs, ok := m.Subscribers[topic]
|
|
||||||
m.RUnlock()
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
p := &memoryEvent{
|
|
||||||
topic: topic,
|
|
||||||
message: message,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sub := range subs {
|
|
||||||
if err := sub.handler(p); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
||||||
m.RLock()
|
|
||||||
if !m.connected {
|
|
||||||
m.RUnlock()
|
|
||||||
return nil, errors.New("not connected")
|
|
||||||
}
|
|
||||||
m.RUnlock()
|
|
||||||
|
|
||||||
var options broker.SubscribeOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
sub := &memorySubscriber{
|
|
||||||
exit: make(chan bool, 1),
|
|
||||||
id: uuid.New().String(),
|
|
||||||
topic: topic,
|
|
||||||
handler: handler,
|
|
||||||
opts: options,
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Lock()
|
|
||||||
m.Subscribers[topic] = append(m.Subscribers[topic], sub)
|
|
||||||
m.Unlock()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-sub.exit
|
|
||||||
m.Lock()
|
|
||||||
var newSubscribers []*memorySubscriber
|
|
||||||
for _, sb := range m.Subscribers[topic] {
|
|
||||||
if sb.id == sub.id {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
newSubscribers = append(newSubscribers, sb)
|
|
||||||
}
|
|
||||||
m.Subscribers[topic] = newSubscribers
|
|
||||||
m.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
return sub, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryBroker) String() string {
|
|
||||||
return "memory"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryEvent) Topic() string {
|
|
||||||
return m.topic
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryEvent) Message() *broker.Message {
|
|
||||||
return m.message
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memoryEvent) Ack() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memorySubscriber) Options() broker.SubscribeOptions {
|
|
||||||
return m.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memorySubscriber) Topic() string {
|
|
||||||
return m.topic
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *memorySubscriber) Unsubscribe() error {
|
|
||||||
m.exit <- true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBroker(opts ...broker.Option) broker.Broker {
|
|
||||||
var options broker.Options
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &memoryBroker{
|
|
||||||
opts: options,
|
|
||||||
Subscribers: make(map[string][]*memorySubscriber),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package memory
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMemoryBroker(t *testing.T) {
|
|
||||||
b := NewBroker()
|
|
||||||
|
|
||||||
if err := b.Connect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected connect error %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
topic := "test"
|
|
||||||
count := 10
|
|
||||||
|
|
||||||
fn := func(p broker.Event) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
sub, err := b.Subscribe(topic, fn)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected error subscribing %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
message := &broker.Message{
|
|
||||||
Header: map[string]string{
|
|
||||||
"foo": "bar",
|
|
||||||
"id": fmt.Sprintf("%d", i),
|
|
||||||
},
|
|
||||||
Body: []byte(`hello world`),
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Publish(topic, message); err != nil {
|
|
||||||
t.Fatalf("Unexpected error publishing %d", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := sub.Unsubscribe(); err != nil {
|
|
||||||
t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := b.Disconnect(); err != nil {
|
|
||||||
t.Fatalf("Unexpected connect error %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package nats
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
)
|
|
||||||
|
|
||||||
// setSubscribeOption returns a function to setup a context with given value
|
|
||||||
func setSubscribeOption(k, v interface{}) broker.SubscribeOption {
|
|
||||||
return func(o *broker.SubscribeOptions) {
|
|
||||||
if o.Context == nil {
|
|
||||||
o.Context = context.Background()
|
|
||||||
}
|
|
||||||
o.Context = context.WithValue(o.Context, k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// setBrokerOption returns a function to setup a context with given value
|
|
||||||
func setBrokerOption(k, v interface{}) broker.Option {
|
|
||||||
return func(o *broker.Options) {
|
|
||||||
if o.Context == nil {
|
|
||||||
o.Context = context.Background()
|
|
||||||
}
|
|
||||||
o.Context = context.WithValue(o.Context, k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// setPublishOption returns a function to setup a context with given value
|
|
||||||
func setPublishOption(k, v interface{}) broker.PublishOption {
|
|
||||||
return func(o *broker.PublishOptions) {
|
|
||||||
if o.Context == nil {
|
|
||||||
o.Context = context.Background()
|
|
||||||
}
|
|
||||||
o.Context = context.WithValue(o.Context, k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,255 +0,0 @@
|
|||||||
// Package nats provides a NATS broker
|
|
||||||
package nats
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
"github.com/micro/go-micro/codec/json"
|
|
||||||
nats "github.com/nats-io/nats.go"
|
|
||||||
)
|
|
||||||
|
|
||||||
type natsBroker struct {
|
|
||||||
sync.Once
|
|
||||||
sync.RWMutex
|
|
||||||
addrs []string
|
|
||||||
conn *nats.Conn
|
|
||||||
opts broker.Options
|
|
||||||
nopts nats.Options
|
|
||||||
drain bool
|
|
||||||
closeCh chan (error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type subscriber struct {
|
|
||||||
s *nats.Subscription
|
|
||||||
opts broker.SubscribeOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
type publication struct {
|
|
||||||
t string
|
|
||||||
m *broker.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *publication) Topic() string {
|
|
||||||
return p.t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *publication) Message() *broker.Message {
|
|
||||||
return p.m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *publication) Ack() error {
|
|
||||||
// nats does not support acking
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) Options() broker.SubscribeOptions {
|
|
||||||
return s.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) Topic() string {
|
|
||||||
return s.s.Subject
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) Unsubscribe() error {
|
|
||||||
return s.s.Unsubscribe()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) Address() string {
|
|
||||||
if n.conn != nil && n.conn.IsConnected() {
|
|
||||||
return n.conn.ConnectedUrl()
|
|
||||||
}
|
|
||||||
if len(n.addrs) > 0 {
|
|
||||||
return n.addrs[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func setAddrs(addrs []string) []string {
|
|
||||||
var cAddrs []string
|
|
||||||
for _, addr := range addrs {
|
|
||||||
if len(addr) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !strings.HasPrefix(addr, "nats://") {
|
|
||||||
addr = "nats://" + addr
|
|
||||||
}
|
|
||||||
cAddrs = append(cAddrs, addr)
|
|
||||||
}
|
|
||||||
if len(cAddrs) == 0 {
|
|
||||||
cAddrs = []string{nats.DefaultURL}
|
|
||||||
}
|
|
||||||
return cAddrs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) Connect() error {
|
|
||||||
n.Lock()
|
|
||||||
defer n.Unlock()
|
|
||||||
|
|
||||||
status := nats.CLOSED
|
|
||||||
if n.conn != nil {
|
|
||||||
status = n.conn.Status()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch status {
|
|
||||||
case nats.CONNECTED, nats.RECONNECTING, nats.CONNECTING:
|
|
||||||
return nil
|
|
||||||
default: // DISCONNECTED or CLOSED or DRAINING
|
|
||||||
opts := n.nopts
|
|
||||||
opts.Servers = n.addrs
|
|
||||||
opts.Secure = n.opts.Secure
|
|
||||||
opts.TLSConfig = n.opts.TLSConfig
|
|
||||||
|
|
||||||
// secure might not be set
|
|
||||||
if n.opts.TLSConfig != nil {
|
|
||||||
opts.Secure = true
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := opts.Connect()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
n.conn = c
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) Disconnect() error {
|
|
||||||
n.RLock()
|
|
||||||
defer n.RUnlock()
|
|
||||||
if n.drain {
|
|
||||||
n.conn.Drain()
|
|
||||||
return <-n.closeCh
|
|
||||||
}
|
|
||||||
n.conn.Close()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) Init(opts ...broker.Option) error {
|
|
||||||
n.setOption(opts...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) Options() broker.Options {
|
|
||||||
return n.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
|
||||||
b, err := n.opts.Codec.Marshal(msg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
n.RLock()
|
|
||||||
defer n.RUnlock()
|
|
||||||
return n.conn.Publish(topic, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
||||||
if n.conn == nil {
|
|
||||||
return nil, errors.New("not connected")
|
|
||||||
}
|
|
||||||
|
|
||||||
opt := broker.SubscribeOptions{
|
|
||||||
AutoAck: true,
|
|
||||||
Context: context.Background(),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&opt)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn := func(msg *nats.Msg) {
|
|
||||||
var m broker.Message
|
|
||||||
if err := n.opts.Codec.Unmarshal(msg.Data, &m); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
handler(&publication{m: &m, t: msg.Subject})
|
|
||||||
}
|
|
||||||
|
|
||||||
var sub *nats.Subscription
|
|
||||||
var err error
|
|
||||||
|
|
||||||
n.RLock()
|
|
||||||
if len(opt.Queue) > 0 {
|
|
||||||
sub, err = n.conn.QueueSubscribe(topic, opt.Queue, fn)
|
|
||||||
} else {
|
|
||||||
sub, err = n.conn.Subscribe(topic, fn)
|
|
||||||
}
|
|
||||||
n.RUnlock()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &subscriber{s: sub, opts: opt}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) String() string {
|
|
||||||
return "nats"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBroker(opts ...broker.Option) broker.Broker {
|
|
||||||
options := broker.Options{
|
|
||||||
// Default codec
|
|
||||||
Codec: json.Marshaler{},
|
|
||||||
Context: context.Background(),
|
|
||||||
}
|
|
||||||
|
|
||||||
n := &natsBroker{
|
|
||||||
opts: options,
|
|
||||||
}
|
|
||||||
n.setOption(opts...)
|
|
||||||
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) setOption(opts ...broker.Option) {
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&n.opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
n.Once.Do(func() {
|
|
||||||
n.nopts = nats.GetDefaultOptions()
|
|
||||||
})
|
|
||||||
|
|
||||||
if nopts, ok := n.opts.Context.Value(optionsKey{}).(nats.Options); ok {
|
|
||||||
n.nopts = nopts
|
|
||||||
}
|
|
||||||
|
|
||||||
// broker.Options have higher priority than nats.Options
|
|
||||||
// only if Addrs, Secure or TLSConfig were not set through a broker.Option
|
|
||||||
// we read them from nats.Option
|
|
||||||
if len(n.opts.Addrs) == 0 {
|
|
||||||
n.opts.Addrs = n.nopts.Servers
|
|
||||||
}
|
|
||||||
|
|
||||||
if !n.opts.Secure {
|
|
||||||
n.opts.Secure = n.nopts.Secure
|
|
||||||
}
|
|
||||||
|
|
||||||
if n.opts.TLSConfig == nil {
|
|
||||||
n.opts.TLSConfig = n.nopts.TLSConfig
|
|
||||||
}
|
|
||||||
n.addrs = setAddrs(n.opts.Addrs)
|
|
||||||
|
|
||||||
if n.opts.Context.Value(drainConnectionKey{}) != nil {
|
|
||||||
n.drain = true
|
|
||||||
n.closeCh = make(chan error)
|
|
||||||
n.nopts.ClosedCB = n.onClose
|
|
||||||
n.nopts.AsyncErrorCB = n.onAsyncError
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) onClose(conn *nats.Conn) {
|
|
||||||
n.closeCh <- nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *natsBroker) onAsyncError(conn *nats.Conn, sub *nats.Subscription, err error) {
|
|
||||||
// There are kinds of different async error nats might callback, but we are interested
|
|
||||||
// in ErrDrainTimeout only here.
|
|
||||||
if err == nats.ErrDrainTimeout {
|
|
||||||
n.closeCh <- err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
package nats
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
nats "github.com/nats-io/nats.go"
|
|
||||||
)
|
|
||||||
|
|
||||||
var addrTestCases = []struct {
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
addrs map[string]string // expected address : set address
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"brokerOpts",
|
|
||||||
"set broker addresses through a broker.Option in constructor",
|
|
||||||
map[string]string{
|
|
||||||
"nats://192.168.10.1:5222": "192.168.10.1:5222",
|
|
||||||
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"brokerInit",
|
|
||||||
"set broker addresses through a broker.Option in broker.Init()",
|
|
||||||
map[string]string{
|
|
||||||
"nats://192.168.10.1:5222": "192.168.10.1:5222",
|
|
||||||
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"natsOpts",
|
|
||||||
"set broker addresses through the nats.Option in constructor",
|
|
||||||
map[string]string{
|
|
||||||
"nats://192.168.10.1:5222": "192.168.10.1:5222",
|
|
||||||
"nats://10.20.10.0:4222": "10.20.10.0:4222"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"default",
|
|
||||||
"check if default Address is set correctly",
|
|
||||||
map[string]string{
|
|
||||||
"nats://127.0.0.1:4222": "",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestInitAddrs tests issue #100. Ensures that if the addrs is set by an option in init it will be used.
|
|
||||||
func TestInitAddrs(t *testing.T) {
|
|
||||||
|
|
||||||
for _, tc := range addrTestCases {
|
|
||||||
t.Run(fmt.Sprintf("%s: %s", tc.name, tc.description), func(t *testing.T) {
|
|
||||||
|
|
||||||
var br broker.Broker
|
|
||||||
var addrs []string
|
|
||||||
|
|
||||||
for _, addr := range tc.addrs {
|
|
||||||
addrs = append(addrs, addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch tc.name {
|
|
||||||
case "brokerOpts":
|
|
||||||
// we know that there are just two addrs in the dict
|
|
||||||
br = NewBroker(broker.Addrs(addrs[0], addrs[1]))
|
|
||||||
br.Init()
|
|
||||||
case "brokerInit":
|
|
||||||
br = NewBroker()
|
|
||||||
// we know that there are just two addrs in the dict
|
|
||||||
br.Init(broker.Addrs(addrs[0], addrs[1]))
|
|
||||||
case "natsOpts":
|
|
||||||
nopts := nats.GetDefaultOptions()
|
|
||||||
nopts.Servers = addrs
|
|
||||||
br = NewBroker(Options(nopts))
|
|
||||||
br.Init()
|
|
||||||
case "default":
|
|
||||||
br = NewBroker()
|
|
||||||
br.Init()
|
|
||||||
}
|
|
||||||
|
|
||||||
natsBroker, ok := br.(*natsBroker)
|
|
||||||
if !ok {
|
|
||||||
t.Fatal("Expected broker to be of types *natsBroker")
|
|
||||||
}
|
|
||||||
// check if the same amount of addrs we set has actually been set, default
|
|
||||||
// have only 1 address nats://127.0.0.1:4222 (current nats code) or
|
|
||||||
// nats://localhost:4222 (older code version)
|
|
||||||
if len(natsBroker.addrs) != len(tc.addrs) && tc.name != "default" {
|
|
||||||
t.Errorf("Expected Addr count = %d, Actual Addr count = %d",
|
|
||||||
len(natsBroker.addrs), len(tc.addrs))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, addr := range natsBroker.addrs {
|
|
||||||
_, ok := tc.addrs[addr]
|
|
||||||
if !ok {
|
|
||||||
t.Errorf("Expected '%s' has not been set", addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package nats
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
nats "github.com/nats-io/nats.go"
|
|
||||||
)
|
|
||||||
|
|
||||||
type optionsKey struct{}
|
|
||||||
type drainConnectionKey struct{}
|
|
||||||
|
|
||||||
// Options accepts nats.Options
|
|
||||||
func Options(opts nats.Options) broker.Option {
|
|
||||||
return setBrokerOption(optionsKey{}, opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DrainConnection will drain subscription on close
|
|
||||||
func DrainConnection() broker.Option {
|
|
||||||
return setBrokerOption(drainConnectionKey{}, struct{}{})
|
|
||||||
}
|
|
||||||
77
broker/noop.go
Normal file
77
broker/noop.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package broker
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type noopBroker struct {
|
||||||
|
opts Options
|
||||||
|
}
|
||||||
|
|
||||||
|
type noopSubscriber struct {
|
||||||
|
topic string
|
||||||
|
opts SubscribeOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBroker returns new noop broker
|
||||||
|
func NewBroker(opts ...Option) Broker {
|
||||||
|
return &noopBroker{opts: NewOptions(opts...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initialize broker
|
||||||
|
func (n *noopBroker) Init(opts ...Option) error {
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&n.opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options returns broker Options
|
||||||
|
func (n *noopBroker) Options() Options {
|
||||||
|
return n.opts
|
||||||
|
}
|
||||||
|
|
||||||
|
// Address returns broker address
|
||||||
|
func (n *noopBroker) Address() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect connects to broker
|
||||||
|
func (n *noopBroker) Connect(ctx context.Context) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disconnect disconnects from broker
|
||||||
|
func (n *noopBroker) Disconnect(ctx context.Context) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish publishes message to broker
|
||||||
|
func (n *noopBroker) Publish(ctx context.Context, topic string, m *Message, opts ...PublishOption) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe subscribes to broker topic
|
||||||
|
func (n *noopBroker) Subscribe(ctx context.Context, topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) {
|
||||||
|
options := NewSubscribeOptions(opts...)
|
||||||
|
return &noopSubscriber{topic: topic, opts: options}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String return broker string representation
|
||||||
|
func (n *noopBroker) String() string {
|
||||||
|
return "noop"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options returns subscriber options
|
||||||
|
func (n *noopSubscriber) Options() SubscribeOptions {
|
||||||
|
return n.opts
|
||||||
|
}
|
||||||
|
|
||||||
|
// TOpic returns subscriber topic
|
||||||
|
func (n *noopSubscriber) Topic() string {
|
||||||
|
return n.topic
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsubscribe unsbscribes from broker topic
|
||||||
|
func (n *noopSubscriber) Unsubscribe(ctx context.Context) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -4,60 +4,116 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
|
||||||
"github.com/micro/go-micro/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
|
"github.com/unistack-org/micro/v3/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Options struct
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Addrs []string
|
Addrs []string
|
||||||
Secure bool
|
Secure bool
|
||||||
Codec codec.Marshaler
|
Codec codec.Marshaler
|
||||||
|
|
||||||
|
// Logger
|
||||||
|
Logger logger.Logger
|
||||||
|
// Handler executed when errors occur processing messages
|
||||||
|
ErrorHandler Handler
|
||||||
|
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
|
// Registry used for clustering
|
||||||
|
Registry registry.Registry
|
||||||
// Other options for implementations of the interface
|
// Other options for implementations of the interface
|
||||||
// can be stored in a context
|
// can be stored in a context
|
||||||
Context context.Context
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewOptions create new Options
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
options := Options{
|
||||||
|
Registry: registry.DefaultRegistry,
|
||||||
|
Logger: logger.DefaultLogger,
|
||||||
|
Context: context.Background(),
|
||||||
|
}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
// Context sets the context option
|
||||||
|
func Context(ctx context.Context) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Context = ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublishOptions struct
|
||||||
type PublishOptions struct {
|
type PublishOptions struct {
|
||||||
// Other options for implementations of the interface
|
// Other options for implementations of the interface
|
||||||
// can be stored in a context
|
// can be stored in a context
|
||||||
Context context.Context
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPublishOptions creates PublishOptions struct
|
||||||
|
func NewPublishOptions(opts ...PublishOption) PublishOptions {
|
||||||
|
options := PublishOptions{
|
||||||
|
Context: context.Background(),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubscribeOptions struct
|
||||||
type SubscribeOptions struct {
|
type SubscribeOptions struct {
|
||||||
// AutoAck defaults to true. When a handler returns
|
// AutoAck ack messages if handler returns nil err
|
||||||
// with a nil error the message is acked.
|
|
||||||
AutoAck bool
|
AutoAck bool
|
||||||
// Subscribers with the same queue name
|
|
||||||
|
// Handler executed when errors occur processing messages
|
||||||
|
ErrorHandler Handler
|
||||||
|
|
||||||
|
// Subscribers with the same group name
|
||||||
// will create a shared subscription where each
|
// will create a shared subscription where each
|
||||||
// receives a subset of messages.
|
// receives a subset of messages.
|
||||||
Queue string
|
Group string
|
||||||
|
|
||||||
// Other options for implementations of the interface
|
// Other options for implementations of the interface
|
||||||
// can be stored in a context
|
// can be stored in a context
|
||||||
Context context.Context
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Option func
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
|
// PublishOption func
|
||||||
type PublishOption func(*PublishOptions)
|
type PublishOption func(*PublishOptions)
|
||||||
|
|
||||||
|
// PublishContext sets the context
|
||||||
|
func PublishContext(ctx context.Context) PublishOption {
|
||||||
|
return func(o *PublishOptions) {
|
||||||
|
o.Context = ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubscribeOption func
|
||||||
type SubscribeOption func(*SubscribeOptions)
|
type SubscribeOption func(*SubscribeOptions)
|
||||||
|
|
||||||
var (
|
// NewSubscribeOptions creates new SubscribeOptions
|
||||||
registryKey = "github.com/micro/go-micro/registry"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
|
func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
|
||||||
opt := SubscribeOptions{
|
options := SubscribeOptions{
|
||||||
AutoAck: true,
|
AutoAck: true,
|
||||||
|
Context: context.Background(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&opt)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
return opt
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addrs sets the host addresses to be used by the broker
|
// Addrs sets the host addresses to be used by the broker
|
||||||
@@ -75,24 +131,55 @@ func Codec(c codec.Marshaler) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableAutoAck will disable auto acking of messages
|
// DisableAutoAck disables auto ack
|
||||||
// after they have been handled.
|
|
||||||
func DisableAutoAck() SubscribeOption {
|
func DisableAutoAck() SubscribeOption {
|
||||||
return func(o *SubscribeOptions) {
|
return func(o *SubscribeOptions) {
|
||||||
o.AutoAck = false
|
o.AutoAck = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queue sets the name of the queue to share messages on
|
// SubscribeAutoAck will disable auto acking of messages
|
||||||
func Queue(name string) SubscribeOption {
|
// after they have been handled.
|
||||||
|
func SubscribeAutoAck(b bool) SubscribeOption {
|
||||||
return func(o *SubscribeOptions) {
|
return func(o *SubscribeOptions) {
|
||||||
o.Queue = name
|
o.AutoAck = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrorHandler will catch all broker errors that cant be handled
|
||||||
|
// in normal way, for example Codec errors
|
||||||
|
func ErrorHandler(h Handler) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.ErrorHandler = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubscribeErrorHandler will catch all broker errors that cant be handled
|
||||||
|
// in normal way, for example Codec errors
|
||||||
|
func SubscribeErrorHandler(h Handler) SubscribeOption {
|
||||||
|
return func(o *SubscribeOptions) {
|
||||||
|
o.ErrorHandler = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Queue sets the subscribers sueue
|
||||||
|
func Queue(name string) SubscribeOption {
|
||||||
|
return func(o *SubscribeOptions) {
|
||||||
|
o.Group = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubscribeGroup sets the name of the queue to share messages on
|
||||||
|
func SubscribeGroup(name string) SubscribeOption {
|
||||||
|
return func(o *SubscribeOptions) {
|
||||||
|
o.Group = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Registry sets registry option
|
||||||
func Registry(r registry.Registry) Option {
|
func Registry(r registry.Registry) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.Context = context.WithValue(o.Context, registryKey, r)
|
o.Registry = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,13 +190,20 @@ func Secure(b bool) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specify TLS Config
|
// TLSConfig sets the TLS Config
|
||||||
func TLSConfig(t *tls.Config) Option {
|
func TLSConfig(t *tls.Config) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.TLSConfig = t
|
o.TLSConfig = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger sets the logger
|
||||||
|
func Logger(l logger.Logger) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Logger = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SubscribeContext set context
|
// SubscribeContext set context
|
||||||
func SubscribeContext(ctx context.Context) SubscribeOption {
|
func SubscribeContext(ctx context.Context) SubscribeOption {
|
||||||
return func(o *SubscribeOptions) {
|
return func(o *SubscribeOptions) {
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
pb "github.com/micro/go-micro/broker/service/proto"
|
|
||||||
"github.com/micro/go-micro/errors"
|
|
||||||
"github.com/micro/go-micro/util/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Broker struct {
|
|
||||||
Broker broker.Broker
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Broker) Publish(ctx context.Context, req *pb.PublishRequest, rsp *pb.Empty) error {
|
|
||||||
log.Debugf("Publishing message to %s topic", req.Topic)
|
|
||||||
err := b.Broker.Publish(req.Topic, &broker.Message{
|
|
||||||
Header: req.Message.Header,
|
|
||||||
Body: req.Message.Body,
|
|
||||||
})
|
|
||||||
log.Debugf("Published message to %s topic", req.Topic)
|
|
||||||
if err != nil {
|
|
||||||
return errors.InternalServerError("go.micro.broker", err.Error())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Broker) Subscribe(ctx context.Context, req *pb.SubscribeRequest, stream pb.Broker_SubscribeStream) error {
|
|
||||||
errChan := make(chan error, 1)
|
|
||||||
|
|
||||||
// message handler to stream back messages from broker
|
|
||||||
handler := func(p broker.Event) error {
|
|
||||||
if err := stream.Send(&pb.Message{
|
|
||||||
Header: p.Message().Header,
|
|
||||||
Body: p.Message().Body,
|
|
||||||
}); err != nil {
|
|
||||||
select {
|
|
||||||
case errChan <- err:
|
|
||||||
return err
|
|
||||||
default:
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("Subscribing to %s topic", req.Topic)
|
|
||||||
sub, err := b.Broker.Subscribe(req.Topic, handler, broker.Queue(req.Queue))
|
|
||||||
if err != nil {
|
|
||||||
return errors.InternalServerError("go.micro.broker", err.Error())
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
log.Debugf("Unsubscribing from topic %s", req.Topic)
|
|
||||||
sub.Unsubscribe()
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Debugf("Context done for subscription to topic %s", req.Topic)
|
|
||||||
return nil
|
|
||||||
case err := <-errChan:
|
|
||||||
log.Debugf("Subscription error for topic %s: %v", req.Topic, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
|
||||||
// source: github.com/micro/go-micro/broker/proto/broker.proto
|
|
||||||
|
|
||||||
package go_micro_broker
|
|
||||||
|
|
||||||
import (
|
|
||||||
fmt "fmt"
|
|
||||||
proto "github.com/golang/protobuf/proto"
|
|
||||||
math "math"
|
|
||||||
)
|
|
||||||
|
|
||||||
import (
|
|
||||||
context "context"
|
|
||||||
client "github.com/micro/go-micro/client"
|
|
||||||
server "github.com/micro/go-micro/server"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ = proto.Marshal
|
|
||||||
var _ = fmt.Errorf
|
|
||||||
var _ = math.Inf
|
|
||||||
|
|
||||||
// This is a compile-time assertion to ensure that this generated file
|
|
||||||
// is compatible with the proto package it is being compiled against.
|
|
||||||
// A compilation error at this line likely means your copy of the
|
|
||||||
// proto package needs to be updated.
|
|
||||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ context.Context
|
|
||||||
var _ client.Option
|
|
||||||
var _ server.Option
|
|
||||||
|
|
||||||
// Client API for Broker service
|
|
||||||
|
|
||||||
type BrokerService interface {
|
|
||||||
Publish(ctx context.Context, in *PublishRequest, opts ...client.CallOption) (*Empty, error)
|
|
||||||
Subscribe(ctx context.Context, in *SubscribeRequest, opts ...client.CallOption) (Broker_SubscribeService, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokerService struct {
|
|
||||||
c client.Client
|
|
||||||
name string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBrokerService(name string, c client.Client) BrokerService {
|
|
||||||
if c == nil {
|
|
||||||
c = client.NewClient()
|
|
||||||
}
|
|
||||||
if len(name) == 0 {
|
|
||||||
name = "go.micro.broker"
|
|
||||||
}
|
|
||||||
return &brokerService{
|
|
||||||
c: c,
|
|
||||||
name: name,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *brokerService) Publish(ctx context.Context, in *PublishRequest, opts ...client.CallOption) (*Empty, error) {
|
|
||||||
req := c.c.NewRequest(c.name, "Broker.Publish", in)
|
|
||||||
out := new(Empty)
|
|
||||||
err := c.c.Call(ctx, req, out, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *brokerService) Subscribe(ctx context.Context, in *SubscribeRequest, opts ...client.CallOption) (Broker_SubscribeService, error) {
|
|
||||||
req := c.c.NewRequest(c.name, "Broker.Subscribe", &SubscribeRequest{})
|
|
||||||
stream, err := c.c.Stream(ctx, req, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := stream.Send(in); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &brokerServiceSubscribe{stream}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Broker_SubscribeService interface {
|
|
||||||
SendMsg(interface{}) error
|
|
||||||
RecvMsg(interface{}) error
|
|
||||||
Close() error
|
|
||||||
Recv() (*Message, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokerServiceSubscribe struct {
|
|
||||||
stream client.Stream
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerServiceSubscribe) Close() error {
|
|
||||||
return x.stream.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerServiceSubscribe) SendMsg(m interface{}) error {
|
|
||||||
return x.stream.Send(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerServiceSubscribe) RecvMsg(m interface{}) error {
|
|
||||||
return x.stream.Recv(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerServiceSubscribe) Recv() (*Message, error) {
|
|
||||||
m := new(Message)
|
|
||||||
err := x.stream.Recv(m)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server API for Broker service
|
|
||||||
|
|
||||||
type BrokerHandler interface {
|
|
||||||
Publish(context.Context, *PublishRequest, *Empty) error
|
|
||||||
Subscribe(context.Context, *SubscribeRequest, Broker_SubscribeStream) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterBrokerHandler(s server.Server, hdlr BrokerHandler, opts ...server.HandlerOption) error {
|
|
||||||
type broker interface {
|
|
||||||
Publish(ctx context.Context, in *PublishRequest, out *Empty) error
|
|
||||||
Subscribe(ctx context.Context, stream server.Stream) error
|
|
||||||
}
|
|
||||||
type Broker struct {
|
|
||||||
broker
|
|
||||||
}
|
|
||||||
h := &brokerHandler{hdlr}
|
|
||||||
return s.Handle(s.NewHandler(&Broker{h}, opts...))
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokerHandler struct {
|
|
||||||
BrokerHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *brokerHandler) Publish(ctx context.Context, in *PublishRequest, out *Empty) error {
|
|
||||||
return h.BrokerHandler.Publish(ctx, in, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *brokerHandler) Subscribe(ctx context.Context, stream server.Stream) error {
|
|
||||||
m := new(SubscribeRequest)
|
|
||||||
if err := stream.Recv(m); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return h.BrokerHandler.Subscribe(ctx, m, &brokerSubscribeStream{stream})
|
|
||||||
}
|
|
||||||
|
|
||||||
type Broker_SubscribeStream interface {
|
|
||||||
SendMsg(interface{}) error
|
|
||||||
RecvMsg(interface{}) error
|
|
||||||
Close() error
|
|
||||||
Send(*Message) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokerSubscribeStream struct {
|
|
||||||
stream server.Stream
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerSubscribeStream) Close() error {
|
|
||||||
return x.stream.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerSubscribeStream) SendMsg(m interface{}) error {
|
|
||||||
return x.stream.Send(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerSubscribeStream) RecvMsg(m interface{}) error {
|
|
||||||
return x.stream.Recv(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerSubscribeStream) Send(m *Message) error {
|
|
||||||
return x.stream.Send(m)
|
|
||||||
}
|
|
||||||
@@ -1,364 +0,0 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
|
||||||
// source: github.com/micro/go-micro/broker/proto/broker.proto
|
|
||||||
|
|
||||||
package go_micro_broker
|
|
||||||
|
|
||||||
import (
|
|
||||||
context "context"
|
|
||||||
fmt "fmt"
|
|
||||||
proto "github.com/golang/protobuf/proto"
|
|
||||||
grpc "google.golang.org/grpc"
|
|
||||||
math "math"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ = proto.Marshal
|
|
||||||
var _ = fmt.Errorf
|
|
||||||
var _ = math.Inf
|
|
||||||
|
|
||||||
// This is a compile-time assertion to ensure that this generated file
|
|
||||||
// is compatible with the proto package it is being compiled against.
|
|
||||||
// A compilation error at this line likely means your copy of the
|
|
||||||
// proto package needs to be updated.
|
|
||||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
|
||||||
|
|
||||||
type Empty struct {
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Empty) Reset() { *m = Empty{} }
|
|
||||||
func (m *Empty) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*Empty) ProtoMessage() {}
|
|
||||||
func (*Empty) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_5edf81766900dd99, []int{0}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Empty) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_Empty.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *Empty) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_Empty.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (m *Empty) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_Empty.Merge(m, src)
|
|
||||||
}
|
|
||||||
func (m *Empty) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_Empty.Size(m)
|
|
||||||
}
|
|
||||||
func (m *Empty) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_Empty.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_Empty proto.InternalMessageInfo
|
|
||||||
|
|
||||||
type PublishRequest struct {
|
|
||||||
Topic string `protobuf:"bytes,1,opt,name=topic,proto3" json:"topic,omitempty"`
|
|
||||||
Message *Message `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *PublishRequest) Reset() { *m = PublishRequest{} }
|
|
||||||
func (m *PublishRequest) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*PublishRequest) ProtoMessage() {}
|
|
||||||
func (*PublishRequest) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_5edf81766900dd99, []int{1}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *PublishRequest) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_PublishRequest.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *PublishRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_PublishRequest.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (m *PublishRequest) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_PublishRequest.Merge(m, src)
|
|
||||||
}
|
|
||||||
func (m *PublishRequest) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_PublishRequest.Size(m)
|
|
||||||
}
|
|
||||||
func (m *PublishRequest) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_PublishRequest.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_PublishRequest proto.InternalMessageInfo
|
|
||||||
|
|
||||||
func (m *PublishRequest) GetTopic() string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Topic
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *PublishRequest) GetMessage() *Message {
|
|
||||||
if m != nil {
|
|
||||||
return m.Message
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type SubscribeRequest struct {
|
|
||||||
Topic string `protobuf:"bytes,1,opt,name=topic,proto3" json:"topic,omitempty"`
|
|
||||||
Queue string `protobuf:"bytes,2,opt,name=queue,proto3" json:"queue,omitempty"`
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *SubscribeRequest) Reset() { *m = SubscribeRequest{} }
|
|
||||||
func (m *SubscribeRequest) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*SubscribeRequest) ProtoMessage() {}
|
|
||||||
func (*SubscribeRequest) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_5edf81766900dd99, []int{2}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *SubscribeRequest) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_SubscribeRequest.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *SubscribeRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_SubscribeRequest.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (m *SubscribeRequest) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_SubscribeRequest.Merge(m, src)
|
|
||||||
}
|
|
||||||
func (m *SubscribeRequest) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_SubscribeRequest.Size(m)
|
|
||||||
}
|
|
||||||
func (m *SubscribeRequest) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_SubscribeRequest.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_SubscribeRequest proto.InternalMessageInfo
|
|
||||||
|
|
||||||
func (m *SubscribeRequest) GetTopic() string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Topic
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *SubscribeRequest) GetQueue() string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Queue
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
Header map[string]string `protobuf:"bytes,1,rep,name=header,proto3" json:"header,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
|
||||||
Body []byte `protobuf:"bytes,2,opt,name=body,proto3" json:"body,omitempty"`
|
|
||||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
|
||||||
XXX_unrecognized []byte `json:"-"`
|
|
||||||
XXX_sizecache int32 `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Message) Reset() { *m = Message{} }
|
|
||||||
func (m *Message) String() string { return proto.CompactTextString(m) }
|
|
||||||
func (*Message) ProtoMessage() {}
|
|
||||||
func (*Message) Descriptor() ([]byte, []int) {
|
|
||||||
return fileDescriptor_5edf81766900dd99, []int{3}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Message) XXX_Unmarshal(b []byte) error {
|
|
||||||
return xxx_messageInfo_Message.Unmarshal(m, b)
|
|
||||||
}
|
|
||||||
func (m *Message) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
|
||||||
return xxx_messageInfo_Message.Marshal(b, m, deterministic)
|
|
||||||
}
|
|
||||||
func (m *Message) XXX_Merge(src proto.Message) {
|
|
||||||
xxx_messageInfo_Message.Merge(m, src)
|
|
||||||
}
|
|
||||||
func (m *Message) XXX_Size() int {
|
|
||||||
return xxx_messageInfo_Message.Size(m)
|
|
||||||
}
|
|
||||||
func (m *Message) XXX_DiscardUnknown() {
|
|
||||||
xxx_messageInfo_Message.DiscardUnknown(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var xxx_messageInfo_Message proto.InternalMessageInfo
|
|
||||||
|
|
||||||
func (m *Message) GetHeader() map[string]string {
|
|
||||||
if m != nil {
|
|
||||||
return m.Header
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Message) GetBody() []byte {
|
|
||||||
if m != nil {
|
|
||||||
return m.Body
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
proto.RegisterType((*Empty)(nil), "go.micro.broker.Empty")
|
|
||||||
proto.RegisterType((*PublishRequest)(nil), "go.micro.broker.PublishRequest")
|
|
||||||
proto.RegisterType((*SubscribeRequest)(nil), "go.micro.broker.SubscribeRequest")
|
|
||||||
proto.RegisterType((*Message)(nil), "go.micro.broker.Message")
|
|
||||||
proto.RegisterMapType((map[string]string)(nil), "go.micro.broker.Message.HeaderEntry")
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
proto.RegisterFile("github.com/micro/go-micro/broker/proto/broker.proto", fileDescriptor_5edf81766900dd99)
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileDescriptor_5edf81766900dd99 = []byte{
|
|
||||||
// 309 bytes of a gzipped FileDescriptorProto
|
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x91, 0xcf, 0x4a, 0xf3, 0x40,
|
|
||||||
0x14, 0xc5, 0x3b, 0xed, 0xd7, 0x86, 0xde, 0x7e, 0x68, 0x19, 0x8a, 0x84, 0x6e, 0x8c, 0xc1, 0x45,
|
|
||||||
0x36, 0x4e, 0x24, 0xdd, 0xa8, 0x88, 0x0b, 0xb1, 0xe0, 0x42, 0x41, 0xc6, 0x9d, 0xbb, 0x4c, 0x3a,
|
|
||||||
0x24, 0xa1, 0x8d, 0x93, 0x4e, 0x66, 0x84, 0xbc, 0x88, 0x2b, 0x1f, 0x56, 0x3a, 0x93, 0xfa, 0xa7,
|
|
||||||
0xa1, 0xee, 0xee, 0x49, 0x7e, 0x73, 0xee, 0xe1, 0x5c, 0x98, 0xa5, 0xb9, 0xca, 0x34, 0x23, 0x89,
|
|
||||||
0x28, 0xc2, 0x22, 0x4f, 0xa4, 0x08, 0x53, 0x71, 0x66, 0x07, 0x26, 0xc5, 0x92, 0xcb, 0xb0, 0x94,
|
|
||||||
0x42, 0x6d, 0x05, 0x31, 0x02, 0x1f, 0xa6, 0x82, 0x18, 0x86, 0xd8, 0xcf, 0xbe, 0x03, 0xfd, 0x79,
|
|
||||||
0x51, 0xaa, 0xda, 0x7f, 0x81, 0x83, 0x27, 0xcd, 0x56, 0x79, 0x95, 0x51, 0xbe, 0xd6, 0xbc, 0x52,
|
|
||||||
0x78, 0x02, 0x7d, 0x25, 0xca, 0x3c, 0x71, 0x91, 0x87, 0x82, 0x21, 0xb5, 0x02, 0x47, 0xe0, 0x14,
|
|
||||||
0xbc, 0xaa, 0xe2, 0x94, 0xbb, 0x5d, 0x0f, 0x05, 0xa3, 0xc8, 0x25, 0x3b, 0x9e, 0xe4, 0xd1, 0xfe,
|
|
||||||
0xa7, 0x5b, 0xd0, 0xbf, 0x81, 0xf1, 0xb3, 0x66, 0x55, 0x22, 0x73, 0xc6, 0xff, 0x76, 0x9f, 0x40,
|
|
||||||
0x7f, 0xad, 0xb9, 0xb6, 0xde, 0x43, 0x6a, 0x85, 0xff, 0x8e, 0xc0, 0x69, 0x4c, 0xf1, 0x35, 0x0c,
|
|
||||||
0x32, 0x1e, 0x2f, 0xb8, 0x74, 0x91, 0xd7, 0x0b, 0x46, 0xd1, 0xe9, 0xbe, 0xf5, 0xe4, 0xde, 0x60,
|
|
||||||
0xf3, 0x57, 0x25, 0x6b, 0xda, 0xbc, 0xc1, 0x18, 0xfe, 0x31, 0xb1, 0xa8, 0x8d, 0xfd, 0x7f, 0x6a,
|
|
||||||
0xe6, 0xe9, 0x25, 0x8c, 0x7e, 0xa0, 0x78, 0x0c, 0xbd, 0x25, 0xaf, 0x9b, 0x58, 0x9b, 0x71, 0x13,
|
|
||||||
0xea, 0x2d, 0x5e, 0x7d, 0x87, 0x32, 0xe2, 0xaa, 0x7b, 0x81, 0xa2, 0x0f, 0x04, 0x83, 0x5b, 0xb3,
|
|
||||||
0x15, 0xdf, 0x81, 0xd3, 0xf4, 0x87, 0x8f, 0x5b, 0x91, 0x7e, 0x37, 0x3b, 0x3d, 0x6a, 0x01, 0xf6,
|
|
||||||
0x06, 0x1d, 0xfc, 0x00, 0xc3, 0xaf, 0xa6, 0xf0, 0x49, 0x0b, 0xdb, 0x6d, 0x71, 0xba, 0xb7, 0x7c,
|
|
||||||
0xbf, 0x73, 0x8e, 0xd8, 0xc0, 0x1c, 0x7d, 0xf6, 0x19, 0x00, 0x00, 0xff, 0xff, 0x25, 0x38, 0xfa,
|
|
||||||
0x02, 0x2b, 0x02, 0x00, 0x00,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reference imports to suppress errors if they are not otherwise used.
|
|
||||||
var _ context.Context
|
|
||||||
var _ grpc.ClientConn
|
|
||||||
|
|
||||||
// This is a compile-time assertion to ensure that this generated file
|
|
||||||
// is compatible with the grpc package it is being compiled against.
|
|
||||||
const _ = grpc.SupportPackageIsVersion4
|
|
||||||
|
|
||||||
// BrokerClient is the client API for Broker service.
|
|
||||||
//
|
|
||||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
|
||||||
type BrokerClient interface {
|
|
||||||
Publish(ctx context.Context, in *PublishRequest, opts ...grpc.CallOption) (*Empty, error)
|
|
||||||
Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (Broker_SubscribeClient, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokerClient struct {
|
|
||||||
cc *grpc.ClientConn
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBrokerClient(cc *grpc.ClientConn) BrokerClient {
|
|
||||||
return &brokerClient{cc}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *brokerClient) Publish(ctx context.Context, in *PublishRequest, opts ...grpc.CallOption) (*Empty, error) {
|
|
||||||
out := new(Empty)
|
|
||||||
err := c.cc.Invoke(ctx, "/go.micro.broker.Broker/Publish", in, out, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *brokerClient) Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (Broker_SubscribeClient, error) {
|
|
||||||
stream, err := c.cc.NewStream(ctx, &_Broker_serviceDesc.Streams[0], "/go.micro.broker.Broker/Subscribe", opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
x := &brokerSubscribeClient{stream}
|
|
||||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := x.ClientStream.CloseSend(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return x, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Broker_SubscribeClient interface {
|
|
||||||
Recv() (*Message, error)
|
|
||||||
grpc.ClientStream
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokerSubscribeClient struct {
|
|
||||||
grpc.ClientStream
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerSubscribeClient) Recv() (*Message, error) {
|
|
||||||
m := new(Message)
|
|
||||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BrokerServer is the server API for Broker service.
|
|
||||||
type BrokerServer interface {
|
|
||||||
Publish(context.Context, *PublishRequest) (*Empty, error)
|
|
||||||
Subscribe(*SubscribeRequest, Broker_SubscribeServer) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterBrokerServer(s *grpc.Server, srv BrokerServer) {
|
|
||||||
s.RegisterService(&_Broker_serviceDesc, srv)
|
|
||||||
}
|
|
||||||
|
|
||||||
func _Broker_Publish_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
|
||||||
in := new(PublishRequest)
|
|
||||||
if err := dec(in); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if interceptor == nil {
|
|
||||||
return srv.(BrokerServer).Publish(ctx, in)
|
|
||||||
}
|
|
||||||
info := &grpc.UnaryServerInfo{
|
|
||||||
Server: srv,
|
|
||||||
FullMethod: "/go.micro.broker.Broker/Publish",
|
|
||||||
}
|
|
||||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
||||||
return srv.(BrokerServer).Publish(ctx, req.(*PublishRequest))
|
|
||||||
}
|
|
||||||
return interceptor(ctx, in, info, handler)
|
|
||||||
}
|
|
||||||
|
|
||||||
func _Broker_Subscribe_Handler(srv interface{}, stream grpc.ServerStream) error {
|
|
||||||
m := new(SubscribeRequest)
|
|
||||||
if err := stream.RecvMsg(m); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return srv.(BrokerServer).Subscribe(m, &brokerSubscribeServer{stream})
|
|
||||||
}
|
|
||||||
|
|
||||||
type Broker_SubscribeServer interface {
|
|
||||||
Send(*Message) error
|
|
||||||
grpc.ServerStream
|
|
||||||
}
|
|
||||||
|
|
||||||
type brokerSubscribeServer struct {
|
|
||||||
grpc.ServerStream
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *brokerSubscribeServer) Send(m *Message) error {
|
|
||||||
return x.ServerStream.SendMsg(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
var _Broker_serviceDesc = grpc.ServiceDesc{
|
|
||||||
ServiceName: "go.micro.broker.Broker",
|
|
||||||
HandlerType: (*BrokerServer)(nil),
|
|
||||||
Methods: []grpc.MethodDesc{
|
|
||||||
{
|
|
||||||
MethodName: "Publish",
|
|
||||||
Handler: _Broker_Publish_Handler,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Streams: []grpc.StreamDesc{
|
|
||||||
{
|
|
||||||
StreamName: "Subscribe",
|
|
||||||
Handler: _Broker_Subscribe_Handler,
|
|
||||||
ServerStreams: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Metadata: "github.com/micro/go-micro/broker/proto/broker.proto",
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
syntax = "proto3";
|
|
||||||
|
|
||||||
package go.micro.broker;
|
|
||||||
|
|
||||||
service Broker {
|
|
||||||
rpc Publish(PublishRequest) returns (Empty) {};
|
|
||||||
rpc Subscribe(SubscribeRequest) returns (stream Message) {};
|
|
||||||
}
|
|
||||||
|
|
||||||
message Empty {}
|
|
||||||
|
|
||||||
message PublishRequest {
|
|
||||||
string topic = 1;
|
|
||||||
Message message = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
message SubscribeRequest {
|
|
||||||
string topic = 1;
|
|
||||||
string queue = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
message Message {
|
|
||||||
map<string,string> header = 1;
|
|
||||||
bytes body = 2;
|
|
||||||
}
|
|
||||||
@@ -1,132 +0,0 @@
|
|||||||
// Package service provides the broker service client
|
|
||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
|
||||||
pb "github.com/micro/go-micro/broker/service/proto"
|
|
||||||
"github.com/micro/go-micro/client"
|
|
||||||
"github.com/micro/go-micro/util/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type serviceBroker struct {
|
|
||||||
Addrs []string
|
|
||||||
Client pb.BrokerService
|
|
||||||
options broker.Options
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
DefaultName = "go.micro.broker"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (b *serviceBroker) Address() string {
|
|
||||||
return b.Addrs[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *serviceBroker) Connect() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *serviceBroker) Disconnect() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *serviceBroker) Init(opts ...broker.Option) error {
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&b.options)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *serviceBroker) Options() broker.Options {
|
|
||||||
return b.options
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *serviceBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
|
||||||
log.Debugf("Publishing to topic %s broker %v", topic, b.Addrs)
|
|
||||||
_, err := b.Client.Publish(context.TODO(), &pb.PublishRequest{
|
|
||||||
Topic: topic,
|
|
||||||
Message: &pb.Message{
|
|
||||||
Header: msg.Header,
|
|
||||||
Body: msg.Body,
|
|
||||||
},
|
|
||||||
}, client.WithAddress(b.Addrs...))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *serviceBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
||||||
var options broker.SubscribeOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
log.Debugf("Subscribing to topic %s queue %s broker %v", topic, options.Queue, b.Addrs)
|
|
||||||
stream, err := b.Client.Subscribe(context.TODO(), &pb.SubscribeRequest{
|
|
||||||
Topic: topic,
|
|
||||||
Queue: options.Queue,
|
|
||||||
}, client.WithAddress(b.Addrs...), client.WithRequestTimeout(time.Hour))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sub := &serviceSub{
|
|
||||||
topic: topic,
|
|
||||||
queue: options.Queue,
|
|
||||||
handler: handler,
|
|
||||||
stream: stream,
|
|
||||||
closed: make(chan bool),
|
|
||||||
options: options,
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-sub.closed:
|
|
||||||
log.Debugf("Unsubscribed from topic %s", topic)
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
// run the subscriber
|
|
||||||
log.Debugf("Streaming from broker %v to topic [%s] queue [%s]", b.Addrs, topic, options.Queue)
|
|
||||||
if err := sub.run(); err != nil {
|
|
||||||
log.Debugf("Resubscribing to topic %s broker %v", topic, b.Addrs)
|
|
||||||
stream, err := b.Client.Subscribe(context.TODO(), &pb.SubscribeRequest{
|
|
||||||
Topic: topic,
|
|
||||||
Queue: options.Queue,
|
|
||||||
}, client.WithAddress(b.Addrs...), client.WithRequestTimeout(time.Hour))
|
|
||||||
if err != nil {
|
|
||||||
log.Debugf("Failed to resubscribe to topic %s: %v", topic, err)
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// new stream
|
|
||||||
sub.stream = stream
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return sub, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *serviceBroker) String() string {
|
|
||||||
return "service"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBroker(opts ...broker.Option) broker.Broker {
|
|
||||||
var options broker.Options
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
addrs := options.Addrs
|
|
||||||
if len(addrs) == 0 {
|
|
||||||
addrs = []string{"127.0.0.1:8001"}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &serviceBroker{
|
|
||||||
Addrs: addrs,
|
|
||||||
Client: pb.NewBrokerService(DefaultName, client.DefaultClient),
|
|
||||||
options: options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user