thojanssens1

thojanssens1

How can I better split the code in the _web folder by interface/technology?

Say I have the following interfaces/sections in my app:

  • presentation/marketing pages with the registration form, using simple Phoenix templates and controllers for those pages and the registration (no LiveView)

  • a GraphQL interface, using resolvers and schema (serving a JavaScript SPA)

  • an admin backend using LiveView

All those different sections of my application use the same business logic/domain (which would be the foo folder alongside the foo_web folder).

At some point I just thought to create separate apps or dependencies, depending on the common business logic/domain code, but I’m not sure whether it’s a good idea. (I still think that having the marketing website separate in the future might be a good idea in order to separate the traffic of the registered users using the app with the traffic of the public website’s guests and bots by different deployment, but I probably think too prematurely).

As those sections of the website are separate and using different technologies, and in order to not have all this code mixed up together and clearly see which code belongs to which section of the app, is there any code organization technique I could use to group code together by section?
(I still think there might be some code shared such as the Endpoint and other connection configuration, not sure yet).

Any advice is appreciated:)

Marked As Solved

al2o3cr

al2o3cr

I’ve found that an umbrella project works really well for this kind of setup. You’d end up with four directories in apps:

  • a domain (please think of a better name) that has Ecto etc machinery in it and starts a Repo
  • a marketing_web that has an Endpoint attached to (say) port 4000 and has a simple Router
  • an api_web that has an Endpoint attached to port 4100 and talks GraphQL
  • an admin_web that has an Endpoint attached to port 4200 and talks LiveView

The upside of this approach is that each individual *_web is simpler and can skip bits it doesn’t care about - for instance, api_web doesn’t need to start esbuild etc because it doesn’t have assets.

The downside is that network routing gets trickier since there are now three Endpoints on different ports. You can use something like main_proxy to do this inside of your application, or use an external load-balancer that takes care of the details. The last time I set one of these up, we had Nginx routing different subdomains to different ports so that admin.example.com went to admin_web while api.example.com went to api_web.

Also Liked

cmo

cmo

Folders.

It doesn’t really matter where the file is. Create a directory structure under web that makes sense to you, e.g one for marketing, one for admin, one for API.

derpycoder

derpycoder

Yup there is a way to namespace your logic:

Excerpt:

By default, the LiveView modules will be namespaced by the web module. You can customize the web module namespace by passing the --web flag with a module name, for example:

mix phx.gen.live Accounts User users --web Sales

Which would generate the LiveViews in lib/app_web/live/sales/user_live/, namespaced AppWeb.Sales.UserLive instead of AppWeb.UserLive.


See: Web Namespace


P.S. This kinds of separation is why I asked the following question in the forum, to avoid writing presentation and marketing pages in old school way, can’t we just mark a LiveView as InertView or dead view?!?:

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement