The Daily Adventures of a SteemPeak Developer #7 - Switch API Endpoint According to the User Country

in dailyadventures •  5 years ago 

Some days ago @robertyan asked in our Discord channel if it is possible to allow Chinese users to use SteemPeak. Before this change the default API endpoint used for new users was always api.steemit.com and this endpoint is blocked in China.
As you may already know SteemPeak support other endpoints (at the moment just anyx.io), but the user must be logged in in order to be able to change that setting. And CN users were not able to login in the first place without a VPN.

After some talks @r00sj3 opened a new issue on our repository to check if we can do something to improve the experience and allow login for those accounts.

Switch API Endpoint According to the User Country

the_daily_adventure_cover_7.png

Read other posts in the series HERE


At first I thought that this ticket would have required a large amount of work and research to find a good solution. But before moving it to a later release I did some research and it turned out that Cloudflare (our CDN) already did most of the work :)

Selection_129.png

For every request sent to our server the Cf-Ipcountry HTTP Header is added and I can easily check that on our server. If the header is found on a request the server just set a Cookie that the browser can read and use to configure the endpoint accordingly.

On the javascript app all I added was this fragment of code:

if (Cookie.get('COUNTRY') === 'CN') {
    steem.api.setOptions({ url: 'https://hive.anyx.io' });
}

With this small change all the request to load contents are routed to the new endpoint. Unfortunately I cannot do much to help with the operation broadcast as that part don't happens on SteemPeak. Luckily users that login with Steem Keychain or the new SteemConnect v3 browser extensions will be able to manually set the endpoint in the browser extension itself.

Not too much code to share in this post (this is always a good thing in my opinion) but hopefully some of you will find the information in this post useful ;)


As always feedback and suggestion are much appreciated, both in the comments or on our Discord ;)


Sergio

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

The Cloudflare ip geo location is awesome, I use that on TravelFeed to show the stupid cookie consent message only to EU users :)

There are many awesome things on Cloudflare and the best part is that most of them are available in the free plan ;)

Cloudflare's free plan is almost too good to be true. A few weeks ago Cloudflare was offline in Germany for a few minutes, and apart from Google most major websites were offline.
No wonder there are conspiracy theories that Cloudflare is funded by the NSA to intercept "encrypted" traffic ;)

Now there is nothing left to say. steempeak has grown up.
Congratulations!

Thanks, we do our best :)

I didn't realize that's what we did and it makes perfect sense... that's why i love this series of posts.

Yeah, this is one of those features that are easier to ship than to test :D

Enjoy a !BEER 🍻, Sergio! 😜

Posted using Partiko Android

To view or trade BEER go to steem-engine.com.

Hey @asgarth, here is your BEER token. Enjoy it!

Enjoy a !BEER 🍻 😜 @jarvie!

Posted using Partiko Android

To view or trade BEER go to steem-engine.com.

Hey @jarvie, here is your BEER token. Enjoy it!

Congratulations @asgarth! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 10000 upvotes. Your next target is to reach 15000 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

I feel you are growing @steempeak with love. 😍
This is why I love @steempeak, dear Sergio! 😜 😁

A huge hug 🤗 from @amico!

Posted using Partiko Android

i had to do a shout on my post to you and your layer, i love your work as a team. already @samstonehill told me about steempeak once but i had weeks using it and today was the best tool i can count with to swim over steemit, i even can see the upvotes from the tribes! thank you.