Routes: Difference between revisions

From neuromatch
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Up to: [[Part Of::Mastodon/Docs]]
Up to: [[Part Of::Mastodon/Docs]]


Routes are the things that define how URLs get mapped to program logic, and masto defines them in a bunch of places:
Routes are the things that define how URLs get mapped to program logic, and masto defines them in a bunch of places.
 
Oddly this is one of the few things that mastodon has developer documentation for<ref>Masto Docs: Routes - https://docs.joinmastodon.org/dev/routes/</ref>


== Rails ==
== Rails ==
Line 12: Line 14:
* API routes: <code>config/routes/api.rb</code> (and other routes in there too)
* API routes: <code>config/routes/api.rb</code> (and other routes in there too)


See the [https://guides.rubyonrails.org/routing.html Rails routing docs] for more on how they work.
See the Rails routing docs<ref>Rails Docs: Routing - https://guides.rubyonrails.org/routing.html </ref> for more on how they work.


The basic pattern for a route will look something like this:
The basic pattern for a route will look something like this:
Line 24: Line 26:
=== Views ===
=== Views ===


Rails then implicitly passes those parameters on to a "view," or an html template that is compiled and then returned to the browser. In this case the relationship to the view is also implicit, depending on file names, so the view that is invoked is <code>views/statuses/show.html.haml</code>.
Rails then implicitly passes those parameters on to a "view,"<ref>Rails Docs: Views - https://guides.rubyonrails.org/layouts_and_rendering.html</ref> or an html template that is compiled and then returned to the browser. In this case the relationship to the view is also implicit, depending on file names, so the view that is invoked is <code>views/statuses/show.html.haml</code>.


That page is mostly a stub, because most of the actual rendering of the page is done clientside with react/redux.
That page is mostly a stub, because most of the actual rendering of the page is done clientside with react/redux.
Line 34: Line 36:
* Main routes <code>app/javascript/flavours/glitch/features/ui/index.jsx</code>
* Main routes <code>app/javascript/flavours/glitch/features/ui/index.jsx</code>


The React routes map a given URL location to the page that is to be displayed. So while the rails routers will give a little bit of header information to the page, the rest of the page that is served is identical across most pages. The clientside React controls what is shown for a given page, rather than the serverside Rails.
The React routes map a given URL location to the page that is to be displayed.<ref>React Router Docs - https://reactrouter.com/en/main</ref> So while the rails routers will give a little bit of header information to the page, the rest of the page that is served is identical across most pages. The clientside React controls what is shown for a given page, rather than the serverside Rails.


So, for example, the statuses route looks like this:
So, for example, the statuses route looks like this:
Line 46: Line 48:
After that, the guide picks up at [[Redux]]
After that, the guide picks up at [[Redux]]


== References ==
<references />


[[Category:Mastodon]]
[[Category:Mastodon]]
[[Category:Docs]]
[[Category:Docs]]

Latest revision as of 20:51, 18 January 2024

Up to: Mastodon/Docs

Routes are the things that define how URLs get mapped to program logic, and masto defines them in a bunch of places.

Oddly this is one of the few things that mastodon has developer documentation for[1]

Rails

Controllers

These are the "primary" routes - when you make a request to the server, rails is the first thing that catches your request.

  • Web interface routes: config/routes.rb
  • API routes: config/routes/api.rb (and other routes in there too)

See the Rails routing docs[2] for more on how they work.

The basic pattern for a route will look something like this:

get '/@:account_username/:id', to: 'statuses#show', as: :short_account_status, constraints: { id: /\d.+/ }

When you go to https://neuromatch.social/@account/status_id, Rails passes the two parameters :account_username and :id to the StatusesController in controllers/statuses_controller.rb, specifically its show method.

Views

Rails then implicitly passes those parameters on to a "view,"[3] or an html template that is compiled and then returned to the browser. In this case the relationship to the view is also implicit, depending on file names, so the view that is invoked is views/statuses/show.html.haml.

That page is mostly a stub, because most of the actual rendering of the page is done clientside with react/redux.

React

The react routes are in:

  • Main routes app/javascript/flavours/glitch/features/ui/index.jsx

The React routes map a given URL location to the page that is to be displayed.[4] So while the rails routers will give a little bit of header information to the page, the rest of the page that is served is identical across most pages. The clientside React controls what is shown for a given page, rather than the serverside Rails.

So, for example, the statuses route looks like this:

<WrappedRoute path='/@:acct/:statusId' exact component={Status} content={children} />

Which displays the Status component (javascript/flavours/glitch/features/status/index.jsx), passing :acct and statusId to the Component's props.

After that, the guide picks up at Redux

References