-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add proxy support #67
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,110 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
require "vendor/autoload.php"; | ||
|
||
use Momento\Auth\EnvMomentoTokenProvider; | ||
use Momento\Auth\EnvMomentoTokenProxyProvider; | ||
use Momento\Cache\SimpleCacheClient; | ||
use Momento\Config\Configurations\Laptop; | ||
use Monolog\Formatter\LineFormatter; | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
use Psr\Log\LoggerInterface; | ||
|
||
$CACHE_NAME = getenv("CACHE_NAME"); | ||
if (!$CACHE_NAME) { | ||
print "Error: Environment variable CACHE_NAME was not found.\n"; | ||
exit; | ||
} | ||
$ITEM_DEFAULT_TTL_SECONDS = 60; | ||
$KEY = "MyKey"; | ||
$VALUE = "MyValue"; | ||
|
||
// Setup | ||
$authProvider = new EnvMomentoTokenProxyProvider( | ||
"MOMENTO_AUTH_TOKEN", | ||
"momento-control:4443", | ||
"momento-cache:4444" | ||
); | ||
|
||
// Use your favorite PSR-3 logger. | ||
$logger = new Logger("example"); | ||
$streamHandler = new StreamHandler("php://stderr"); | ||
$formatter = new LineFormatter("%message%\n"); | ||
$streamHandler->setFormatter($formatter); | ||
$logger->pushHandler($streamHandler); | ||
|
||
// Or use the built-in minimal logger, equivalent to the above Monolog configuration. | ||
//$logger = \Momento\Utilities\LoggingHelper::getMinimalLogger(); | ||
// Or discard all log messages. | ||
//$logger = \Momento\Utilities\LoggingHelper::getNullLogger(); | ||
|
||
$configuration = Laptop::latest($logger); | ||
$client = new SimpleCacheClient($configuration, $authProvider, $ITEM_DEFAULT_TTL_SECONDS); | ||
|
||
function printBanner(string $message, LoggerInterface $logger): void | ||
{ | ||
$line = "******************************************************************"; | ||
$logger->info($line); | ||
$logger->info($message); | ||
$logger->info($line); | ||
} | ||
|
||
printBanner("* Momento Example Start *", $logger); | ||
|
||
// Ensure test cache exists | ||
$response = $client->createCache($CACHE_NAME); | ||
if ($response->asSuccess()) { | ||
$logger->info("Created cache " . $CACHE_NAME . "\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error creating cache: " . $response->asError()->message() . "\n"); | ||
exit; | ||
} elseif ($response->asAlreadyExists()) { | ||
$logger->info("Cache " . $CACHE_NAME . " already exists.\n"); | ||
} | ||
|
||
// List cache | ||
$response = $client->listCaches(); | ||
if ($response->asSuccess()) { | ||
while (true) { | ||
$logger->info("SUCCESS: List caches: \n"); | ||
foreach ($response->asSuccess()->caches() as $cache) { | ||
$cacheName = $cache->name(); | ||
$logger->info("$cacheName\n"); | ||
} | ||
$nextToken = $response->asSuccess()->nextToken(); | ||
if (!$nextToken) { | ||
break; | ||
} | ||
$response = $client->listCaches($nextToken); | ||
} | ||
$logger->info("\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error listing cache: " . $response->asError()->message() . "\n"); | ||
exit; | ||
} | ||
|
||
// Set | ||
$logger->info("Setting key: $KEY to value: $VALUE\n"); | ||
$response = $client->set($CACHE_NAME, $KEY, $VALUE); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS: - Set key: " . $KEY . " value: " . $VALUE . " cache: " . $CACHE_NAME . "\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error setting key: " . $response->asError()->message() . "\n"); | ||
exit; | ||
} | ||
|
||
// Get | ||
$logger->info("Getting value for key: $KEY\n"); | ||
$response = $client->get($CACHE_NAME, $KEY); | ||
if ($response->asHit()) { | ||
$logger->info("SUCCESS: - Get key: " . $KEY . " value: " . $response->asHit()->valueString() . " cache: " . $CACHE_NAME . "\n"); | ||
} elseif ($response->asMiss()) { | ||
$logger->info("Get operation was a MISS\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error getting cache: " . $response->asError()->message() . "\n"); | ||
exit; | ||
} | ||
|
||
printBanner("* Momento Example End *", $logger); |
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
|
||
namespace Momento\Auth; | ||
|
||
use Momento\Auth\AuthUtils; | ||
use Momento\Cache\Errors\InvalidArgumentError; | ||
use function \Momento\Utilities\isNullOrEmpty; | ||
|
||
|
@@ -40,4 +39,14 @@ public function getControlEndpoint(): string | |
{ | ||
return $this->controlEndpoint; | ||
} | ||
|
||
public function getControlProxyEndpoint(): string|null | ||
{ | ||
return null; | ||
} | ||
|
||
public function getCacheProxyEndpoint(): string|null | ||
{ | ||
return null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left you some input in slack about some other ideas for this interface. |
||
} |
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,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Momento\Auth; | ||
|
||
use Momento\Cache\Errors\InvalidArgumentError; | ||
use function Momento\Utilities\isNullOrEmpty; | ||
|
||
class EnvMomentoTokenProxyProvider implements ICredentialProvider | ||
{ | ||
|
||
private string $authToken; | ||
private string $controlProxyEndpoint; | ||
private string $cacheProxyEndpoint; | ||
private string $controlEndpoint; | ||
private string $cacheEndpoint; | ||
|
||
public function __construct( | ||
string $envVariableName, | ||
string $controlProxyEndpoint, | ||
string $cacheProxyEndpoint | ||
) | ||
{ | ||
$this->controlProxyEndpoint = $controlProxyEndpoint; | ||
$this->cacheProxyEndpoint = $cacheProxyEndpoint; | ||
$authToken = getenv($envVariableName); | ||
if ($authToken === false || isNullOrEmpty($authToken)) { | ||
throw new InvalidArgumentError("Environment variable $envVariableName is empty or null."); | ||
} | ||
$payload = AuthUtils::parseAuthToken($authToken); | ||
$this->authToken = $authToken; | ||
$this->controlEndpoint = $payload->cp; | ||
$this->cacheEndpoint = $payload->c; | ||
} | ||
|
||
public function getAuthToken(): string | ||
{ | ||
return $this->authToken; | ||
} | ||
|
||
public function getControlProxyEndpoint(): string|null | ||
{ | ||
return $this->controlProxyEndpoint; | ||
} | ||
|
||
public function getCacheProxyEndpoint(): string|null | ||
{ | ||
return $this->cacheProxyEndpoint; | ||
} | ||
|
||
public function getControlEndpoint(): string | ||
{ | ||
return $this->controlEndpoint; | ||
} | ||
|
||
public function getCacheEndpoint(): string | ||
{ | ||
return $this->cacheEndpoint; | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is so much other code in this example that this bit gets lost in the noise. tbh I think if we want to have a proxy example it should probably just have the auth provider instantiation, with more comments about what it's doing and why, and then like one call to "listCaches" or something. and we should remove the rest of the stuff that's duplicated from the other examples so that their attention is drawn to the important thing.