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

avatar

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



0
0
0.000
14 comments
avatar

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

0
0
0.000
avatar

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

0
0
0.000
avatar

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

0
0
0.000
avatar

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 ;)

0
0
0.000
avatar

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!
0
0
0.000
avatar

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

0
0
0.000
avatar

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.

0
0
0.000