Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add mongo_client package * feat: add mongo_client package * test: add mongo_client package tests * remove binary * style fix * remove mongo entrypoint * add build script * refactor config * refactor config mechanism * change default port * delete go sum * add go doc * fix bad naming * remove useless bin * fix flag * change config priority * delay flag parsing * delay flag parsing * reflect only print to stdout
- Loading branch information
Showing
32 changed files
with
1,406 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,7 @@ composer.lock | |
*.log | ||
.idea/ | ||
/app | ||
/mongo | ||
/example/app | ||
/example/go2php/app | ||
/example/config/app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env bash | ||
package=../example/mongo_client/sidecar.go | ||
package_name=mongo-proxy | ||
|
||
#the full list of the platforms: https://golang.org/doc/install/source#environment | ||
platforms=( | ||
"darwin/amd64" | ||
"linux/amd64" | ||
) | ||
|
||
for platform in "${platforms[@]}" | ||
do | ||
platform_split=(${platform//\// }) | ||
GOOS=${platform_split[0]} | ||
GOARCH=${platform_split[1]} | ||
output_name=$package_name'-'$GOOS'-'$GOARCH | ||
if [ $GOOS = "windows" ]; then | ||
output_name+='.exe' | ||
fi | ||
echo GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package | ||
GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package | ||
if [ $? -ne 0 ]; then | ||
echo 'An error has occurred! Aborting the script execution...' | ||
exit 1 | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf/GoTask. | ||
* | ||
* @link https://www.github.com/hyperf/gotask | ||
* @document https://www.github.com/hyperf/gotask | ||
* @contact guxi99@gmail.com | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
use Reasno\GoTask\GoTask; | ||
use Reasno\GoTask\IPC\SocketIPCSender; | ||
use Swoole\Process; | ||
use function Swoole\Coroutine\run; | ||
|
||
require __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
const ADDR = '127.0.0.1:6001'; | ||
|
||
exec('go build -o ' . __DIR__ . '/app ' . __DIR__ . '/sidecar.go'); | ||
$process = new Process(function (Process $process) { | ||
$process->exec(__DIR__ . '/app', ['-address', ADDR]); | ||
}); | ||
$process->start(); | ||
|
||
sleep(1); | ||
|
||
run(function () { | ||
$task = new SocketIPCSender(ADDR); | ||
for ($i = 0; $i < 5; ++$i) { | ||
$task->call('MongoProxy.InsertOne', ['Database' => 'testing', 'Collection' => 'colors', 'Record' => ['Blue' => 'Red', 'number' => $i]]); | ||
} | ||
var_dump($task->call('MongoProxy.Find', ['Database' => 'testing', 'Collection' => 'colors', 'Filter' => ['Blue' => 'Red'], 'Opts' => [['Skip' => 1, 'Limit' => 2]]])); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/hyperf/gotask/v2/pkg/gotask" | ||
"github.com/hyperf/gotask/v2/pkg/mongo_client" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func main() { | ||
mongoConfig := mongo_client.LoadConfig() | ||
ctx, cancel := context.WithTimeout(context.Background(), mongoConfig.ConnectTimeout) | ||
defer cancel() | ||
|
||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoConfig.Uri)) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
if err := gotask.Register(mongo_client.NewMongoProxyWithTimeout(client, mongoConfig.ReadWriteTimeout)); err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
if err := gotask.Run(); err != nil { | ||
log.Fatalln(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package mongo_client | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"time" | ||
) | ||
|
||
type Config struct { | ||
Uri string | ||
ConnectTimeout time.Duration | ||
ReadWriteTimeout time.Duration | ||
} | ||
|
||
var ( | ||
globalMongoUri *string | ||
globalMongoConnectTimeout *time.Duration | ||
globalMongoReadWriteTimeout *time.Duration | ||
) | ||
|
||
func init() { | ||
uri, ok := os.LookupEnv("MONGODB_URI") | ||
if !ok { | ||
uri = "mongodb://127.0.0.1:27017" | ||
} | ||
ct := getTimeout("MONGODB_CONNECT_TIMEOUT", 3*time.Second) | ||
rwt := getTimeout("MONGODB_READ_WRITE_TIMEOUT", time.Minute) | ||
|
||
globalMongoUri = flag.String("mongodb-uri", uri, "the default mongodb uri") | ||
globalMongoConnectTimeout = flag.Duration("mongodb-connect-timeout", ct, "mongodb connect timeout") | ||
globalMongoReadWriteTimeout = flag.Duration("mongodb-read-write-timeout", rwt, "mongodb read write timeout") | ||
flag.Parse() | ||
} | ||
|
||
func getTimeout(env string, fallback time.Duration) (result time.Duration) { | ||
env, ok := os.LookupEnv(env) | ||
if !ok { | ||
return fallback | ||
} | ||
result, err := time.ParseDuration(env) | ||
if err != nil { | ||
return fallback | ||
} | ||
return result | ||
} | ||
|
||
// LoadConfig loads Configurations from environmental variables or config file in PHP. | ||
// Environmental variables takes priority. | ||
func LoadConfig() Config { | ||
return Config{ | ||
*globalMongoUri, | ||
*globalMongoConnectTimeout, | ||
*globalMongoReadWriteTimeout, | ||
} | ||
} |
Oops, something went wrong.