# Backend code

**URL:** https://meta.discourse.org/t/backend-code/257537
**Category:** Development
**Created:** [March 8, 2023, 8:29pm UTC](https://meta.discourse.org/t/backend-code/257537 "2023-03-08T20:29:43Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Julien\_Bonnier](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/julien_bonnier/32/295545_2.png) [@Julien\_Bonnier](https://meta.discourse.org/u/Julien_Bonnier)
#### Post date: [March 8, 2023, 8:29pm UTC](https://meta.discourse.org/t/backend-code/257537/1 "2023-03-08T20:29:43Z")

</div>

Hello,

I’m trying to find a way to execute some code in the backend.

The ultimate goal is to call the Maxmind API and redirect users coming from outside of Quebec to another site.

I found this [post](https://meta.discourse.org/t/theme-component-v-plugin-whats-the-difference/153951) and I understand that to do this I would have to use a self-hosted Discourse.

Is there a way to do what I am trying to do on the pro plan?

Cheers,

Julien

---

<div class="post-metadata">

### Author: ![Jagster](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagster/32/192154_2.png) [@Jagster](https://meta.discourse.org/u/Jagster)
#### Post date: [March 8, 2023, 8:34pm UTC](https://meta.discourse.org/t/backend-code/257537/2 "2023-03-08T20:34:31Z")

</div>

> [@Julien\_Bonnier](#):
>
> The ultimate goal is to call the Maxmind API and redirect users coming from outside of Quebec to another site.

I don’t know the answer, sorry, but you should think twice such solution. Geo-IP is really unreliable.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [March 8, 2023, 8:38pm UTC](https://meta.discourse.org/t/backend-code/257537/3 "2023-03-08T20:38:24Z")

</div>

> [@Julien\_Bonnier](#):
>
> I’m trying to find a way to execute some code in the backend.

You will need to write a plugin for that. See [Developing Discourse Plugins - Part 1 - Create a basic plugin](https://meta.discourse.org/t/developing-discourse-plugins-part-1-create-a-basic-plugin/30515).

> [@Julien\_Bonnier](#):
>
> The ultimate goal is to call the Maxmind API and redirect users coming from outside of Quebec to another site.

No need for API calls, as Discourse ships with MaxMind integration out of the box, and you can simply call `DiscourseIpInfo.get(ip)` from your plugin.

> [@Julien\_Bonnier](#):
>
> I found this [post](https://meta.discourse.org/t/theme-component-v-plugin-whats-the-difference/153951) and I understand that to do this I would have to use a self-hosted Discourse.

If you are on our hosting, please contact our support. We can help you with custom plugins in our Enterprise plan.

---

<div class="post-metadata">

### Author: ![Julien\_Bonnier](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/julien_bonnier/32/295545_2.png) [@Julien\_Bonnier](https://meta.discourse.org/u/Julien_Bonnier)
#### Post date: [March 8, 2023, 8:49pm UTC](https://meta.discourse.org/t/backend-code/257537/4 "2023-03-08T20:49:17Z")

</div>

> You will need to write a plugin for that. See [Developing Discourse Plugins - Part 1 - Create a basic plugin](https://meta.discourse.org/t/developing-discourse-plugins-part-1-create-a-basic-plugin/30515).

But will I be able to use that plugin with the SaaS version of Discourse (with the pro plan)?

> No need for API calls, as Discourse ships with MaxMind integration out of the box, and you can simply call `DiscourseIpInfo.get(ip)` from your plugin.

Is this precise enough to target user from Quebec, Canada only. Most GeoIP services I found had a country precision but were not able to target a specific province in Canada.

> If you are on our hosting, please contact our support. We can help you with custom plugins in our Enterprise plan.

So not possible with the Standard plan? My guess is the Enterprise plan is at least 3x the price of the pro plan.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 8, 2023, 8:53pm UTC](https://meta.discourse.org/t/backend-code/257537/5 "2023-03-08T20:53:40Z")

</div>

> [@Julien\_Bonnier](#):
>
> Is this precise enough to target user from Quebec, Canada only.

Hard to tell here’s what I find out about my IP:

```plaintext
=> {:country=>"United States",
 :country_code=>"US",
 :region=>"Tennessee",
 :city=>"Nashville",
 :latitude=>36.1315,
 :longitude=>-86.8019,
 :location=>"Nashville, Tennessee, United States",
 :geoname_ids=>[6255149, 6252001, 4644585, 4662168],
 :asn=>7922,
 :organization=>"COMCAST-7922"}

```

There will be a bunch of ways that this could have false positives or negatives. Your best bet is some other solution to the problem that you’re solving with the redirect.

---

<div class="post-metadata">

### Author: ![Julien\_Bonnier](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/julien_bonnier/32/295545_2.png) [@Julien\_Bonnier](https://meta.discourse.org/u/Julien_Bonnier)
#### Post date: [March 8, 2023, 8:57pm UTC](https://meta.discourse.org/t/backend-code/257537/6 "2023-03-08T20:57:49Z")

</div>

That looks precise enough, but a lot of GeoIP services are more precise for IPs from USA than IPs from Canada.

It doesn’t have to be 100% positive or negative, it is a way to have a first fence since the service is offered to people in Quebec only.

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [March 8, 2023, 9:31pm UTC](https://meta.discourse.org/t/backend-code/257537/7 "2023-03-08T21:31:49Z")

</div>

Instead of a full redirect you might be able to use a theme-component that for example if your looking to redirect french users. It could maybe use Local to populate a popup in the login screen for the prefered site for francophones.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 8, 2023, 10:09pm UTC](https://meta.discourse.org/t/backend-code/257537/8 "2023-03-08T22:09:13Z")

</div>

I’d recommend that you just include a user custom field that says “I am in Quebec” that they must click when they sign up.

If you’d like a plugin, though, I might be able to help.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [March 8, 2023, 10:34pm UTC](https://meta.discourse.org/t/backend-code/257537/9 "2023-03-08T22:34:27Z")

</div>

I’d welcome a PR to the Locations plugin (as a site setting) to determine a User’s location automatically and set this in their profile which in this mode could be read only (another setting) to prevent them changing it themselves (but might be by admin on request).

Then, as a bonus, you would be able to see everyone on a map :slight_smile:

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [March 8, 2023, 11:47pm UTC](https://meta.discourse.org/t/backend-code/257537/10 "2023-03-08T23:47:57Z")

</div>

You could turn the logic in [our geo blocking plugin](https://meta.discourse.org/t/geo-blocking-plugin/227824) around and instead of blocking certain regions, you could block all except certain regions. Shouldn’t be too hard - bonus points for the person that can do it with a one character edit. Hint: line 37 !

> [@Julien\_Bonnier](#):
>
> to do this I would have to use a self-hosted Discourse.

Or you can host with [communiteq](https://www.communiteq.com/) on the Professional plan or up. In that case we’ll even extend the plugin for your use case.
