Skip to content

Server re-build with latest AdonisJS framework & Typescript#47

Merged
SpecialAro merged 87 commits intoferdium:mainfrom
cino:v2-rebuild
Oct 13, 2023
Merged

Server re-build with latest AdonisJS framework & Typescript#47
SpecialAro merged 87 commits intoferdium:mainfrom
cino:v2-rebuild

Conversation

@cino
Copy link
Contributor

@cino cino commented Jul 22, 2022

I apologize for the size of this PR but as we discussed earlier on Discord it's impossible to upgrade the dependencies without heavily restructuring the project. For anyone wanting to have a look at the code please open this in an editor or make use of github.dev by pressing the . (dot) on your keyboard.

So for now, this is a WIP Pull request so anyone can contribute and see where we are at the moment.

There are a few rules I've committed to while doing this "v2":

  • No breaking changes in the public interface
  • Everything in Typescript
  • Everything tested

Other improvements:

  • All routes should be pointed at controllers, no direct view response / callbacks from the route file
  • Never using Env() to retrieve environment variables outside the config files, the config files should be retrieving the environment variables and providing these to the application

Routes:

Dashboard (Conditional, only if enabled)

  • Login
  • Logout
  • Forgot password
  • Reset password
  • Account page
  • Export services/workspaces
  • Transfer services/workspaces (importing above export)
  • Delete account
  • Import endpoint (different from transfer)

API:

GitHub

  • Add linter/tests in Github action

General:

  • Copy docker building from the original branch into this one
  • ??

This also closes #70

@cino cino added the enhancement New feature or request label Jul 22, 2022
@cino cino requested a review from a team July 22, 2022 14:31
@cino cino self-assigned this Jul 25, 2022
@cino cino linked an issue Nov 3, 2022 that may be closed by this pull request
@mcmxcdev
Copy link
Member

Hey @cino, I had some available time and pushed 4 new commits to the server overhaul.

All dependencies are up-to-date now, no more CVEs are reported in prod code, no TS/ESLint issues and made sure that all adonis commands work as expected.

I think this migration is moving to be in a great state, but ofc most of the routes have to be rewritten still.

Let's see, maybe some other Ferdium contributors want to jump in and help as well, this is probably one of the biggest tech debt we have to solve.

@SpecialAro
Copy link
Member

Hi all!

I've been trying to learn a bit of AdonisJS so I can push this PR forward. So far I was able to update the following endpoints:

  • Signup
  • Login
  • Me (GET and PUT)

Tested all 3 with Ferdium and they were all looking fine. Let me know what you think (pushing bellow).

Important security note: I'm using the API token guard of adonis auth for handling API authentication, which is different from the JWT we used to have (now it is not supported natively by adonis). I've set an expiration of 7 days, but let me know what you think about this. For this to work I had to add a new migration to introduce two more columns in the Tokens table.

Tomorrow, hopefully, I'll start with the Service or Workspace endpoints x)

@cino you were tagged on the Login and Signup endpoints - if you could review my work I would really apreciate it :D Thank you!

@SpecialAro
Copy link
Member

Hey again!

I just refactored the code to use JWT (so that we don't have to change anything on the Ferdium Client to accomodate that change - that is something we can do in the future, if needed).

I've also finished porting all the API endpoints!! 😄 But I'll be glad if someone could help me test them so that we can validate what I've done. The majority of them are working properly (tested with Ferdium Client), but some specific ones I havent tested (check the TODOS in the files)

@ferdium/reviewers I'm pinging everyone in hope that someone can review my code, given that this substantialy pushes this PR forward.

@cino, sorry to tag you again, but I'm trying to test the web endpoints and I don't seem to be able to past the initial page (no access to login, or dashboad). All I get is:
image

... or if I click on the button to go to dashboard:
image

Comment on lines +27 to +31
@column()
public name: string;

@column.dateTime()
public expires_at: DateTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add these new fields? How are they going to be backwards compatible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a great question. I'm not entirely sure if it is ok to add them like so or if we need to provide a @default value. I've never used Adonis before so I'm not sure how to handle it.

If you test the auth jwt or even the api you will noticed that this 2 fields were added in the Adonis upgrade.

Generally, we have a bigger problem than this... I've tried to test the db in production and it fails to even initiate migrations (even without this new migration I added). We will probably need to code a script to migrate old databases to the new database schema manually

emailValidated: true,
features: {},
firstname: auth.user.username,
id: '82c1cf9d-ab58-4da2-b55e-aaa41d2142d8',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same hardcoded id for every user?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-pasted from the previous code.... It was strange to me as well xD

Comment on lines +125 to +171
isPremium: true,
isSubscriptionOwner: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to drop these fields since Ferdium is fully OSS, but probably better to split it off in a PR that would happen after this one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is here to circunvent the fact that we are OSS. Forcing features that depend on those fields to be set to true on the Ferdium-App

emailValidated: true,
features: {},
firstname: auth.user.username,
id: '82c1cf9d-ab58-4da2-b55e-aaa41d2142d8',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, don't know why xD

.length > 0
); // eslint-disable-line no-await-in-loop
// eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member
(await Service.query().where('serviceId', serviceId)).length > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this code even make sense? It is querying for a service but then the result of the call is not assigned to any variable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside a while block. so it will run while it is > 0 (don't like it either but lets make improvements after we merge this to codebase)

? `${Env.get('APP_URL')}/v1/icon/${settings.iconId}`
: null,
? `${url}/v1/icon/${settings.iconId}`
: // eslint-disable-next-line unicorn/no-null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to disable that lint rule directly in eslint config, I always do that for all projects where I use eslint-plugin-unicorn, guess I forgot here when I implemented it.

.delete();

return response.send({
message: 'Sucessfully deleted service',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably more descriptive as "Sucessfully deleted service: xyz"

.env.example Outdated
Comment on lines +35 to +34
JWT_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\nMIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDZwMVZu/F04fMd\nXAXEHob+X9Eb5Y18rPEpvE75Ya6i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g8AMIICCgKCAgEA2cDFWbvxdOHzHVwFxB6G\n/l/RG+WNfKzxKbxO+WGuokMH2PHmDQgHvepe5UQ2CRTOPAeLcnvrfQbTwYjQE5MH\nyC0Z+MlUGIgENefrVguZr/eEqddajwklzt08GLt+YTSus+CQMifdV705nUB78i5N\n2Jk+aFTnc4appJdn1g8LFIggv41ob7n4LjFb3eAK0Jg8PQBrrdDunrLKMIbmaJ2b\nv0V6TPSH/z476okeG12eJDixU0BjKu5JZVWfGhLbUN3Kuz/n2VgSxEgdcSiTi4zl\nPjPzx3uxTg9r4oH3iYVDRkTJT4/nRKsuHLDb2tpPDcl9TPcjXtnw9H4lnQtWUpf+\naS69SDXe0EHYNtpuY3ldj79nI87hlEAHikpLOBwC7u4f9Ky7btZOu3rDtGjY/i4g\nkHZApt4GI/LazMwYkVhLdaPNHxWY54E7hBIqIFOIN9BKX7yt395OvngccpSGrmaV\nrfqDGGdaDI7mWXDl0JHf+CA82wAlDOR9fBz2yOFgm7S3WdKaSksZ/RRovlo1fA+/\nUcg7HY94pmOaOKTS+z3qU2zgfvhw1mBka4MoMFy+z35oZl+j7Ihd3VF0zl3lxjLb\n0ZZWTOM12Jm13WajpxWaHciIZNJlDlmLkbXW2aZR40JcyNYBxzkeENRjpukup8wg\na/aItHHyl6uFuaanqbrZkQECAwEAAQ==\n-----END PUBLIC KEY-----\n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a less verbose way than specifying the JWT keys like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is... this was automatically created by running the install commands on adonis-jwt 😞

package.json Outdated
"@adonisjs/session": "6.4.0",
"@adonisjs/shield": "7.1.1",
"@adonisjs/view": "6.2.0",
"adonis5-jwt": "1.1.7",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/maxgalbu/adonis5-jwt hasn't had any commits for a year now. which means we will pull in some outdated dependencies eventually through them.

But I guess there is no alternative.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep... I dont think we do... The only alternative would be to refactor both Ferdium-App and Ferdium-Server afterwards - which would result in Breaking Changes on both sides for users that haven't been updating the client (they would be forced to update unless we keep a v1 API that works with jwt middleware).

}),
});

return response.send('Created new recipe');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Created new recipe with id abc and name xyz"

@SpecialAro
Copy link
Member

SpecialAro commented Sep 6, 2023

Been doing some more work in this. I think we are close to finish.

Things that are missing:

  • In the data.edge file it seems like @each(service in services) and the same logic for workspaces is not working (services and workspaces' info are not shown). I would gladly accept help on this point!
  • The import and export data is currently failing (this is also a issue in the current server, so we should try to fix it in this new version) - the exported data is not being parsed correctly (too many JSON.parse and JSON.stringify)
  • I think all endpoints are working (API at least). But it would be great if someone other than me could test them out, please.
  • We really need to be careful on how we are going to migrate the database in production. It seems that it doesn't recognized the already applied migrations... I dont think I did anything wrong because node ace migrate:run should work but it fails. This implies that we are probably going to need a script that runs a migration "manually" to update from adonisjs 4 to 5. Just made a script that solves this migration 😄
  • Tests are still missing. But maybe we can add them after we merge this to codebase, wdyt? - I'm not saying they are not important, but we should really try to push this forward as soon as possible (it has been stalled from long enough)
  • Lint all the files. Typescript is failing in plenty of the files even though the js code is working just fine. We should try to fix it before merging - or if we can't, this could also be done after.

These are my takes. Please let me know what you guys think 😄

@marie-bnl marie-bnl mentioned this pull request Sep 12, 2023
.nvmrc Outdated
@@ -1 +1 @@
18.17.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ferdium/ferdium-recipes@cdafac1 actually uses newer versions than this since recently

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn xD I'll fix it before merging

await Recipe.create({
name: data.name,
recipeId: data.id,
// @ts-expect-error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually take the TS error message and put it there like // @ts-expect-error foo to make it more self-explanatory what the problem is.

fs
.readJsonSync(path.join(Application.appRoot, 'recipes', 'all.json'))
.filter(recipe => recipe.featured),
.filter((recipe: any) => recipe.featured),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have some Recipe to use for this instead of any?

Copy link
Member

@SpecialAro SpecialAro Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but it isn't working (types are not correct) - and I'm not in the "mood" of fixing it.... I just want to close this PR as soon as possible to work on improvements later (mainly lint, types and tests). Don't know what you think about that?

@SpecialAro SpecialAro self-assigned this Oct 1, 2023
@SpecialAro SpecialAro changed the title WIP: Server re-build with latest AdonisJS framework & Typescript Server re-build with latest AdonisJS framework & Typescript Oct 1, 2023
@SpecialAro SpecialAro requested a review from Alphrag October 1, 2023 11:13
@mcmxcdev
Copy link
Member

mcmxcdev commented Oct 1, 2023

Amazing work! 🎉

I noticed a couple of improvement possiblities:

  • pnpm reformat-files produces 10 files diff when run with latest commit which it shouldn't
    • make sure to disable the lint rules which conflict with prettier formatting
  • pnpm start always errored with "missing env value HOST", although I had HOST defined inside .env
  • pnpm test gave me failed tests for various files
    • add the pnpm test step to the CI so we avoid test failures in the future

@SpecialAro
Copy link
Member

Amazing work! 🎉

I noticed a couple of improvement possiblities:

  • pnpm reformat-files produces 10 files diff when run with latest commit which it shouldn't

    • make sure to disable the lint rules which conflict with prettier formatting
  • pnpm start always errored with "missing env value HOST", although I had HOST defined inside .env

  • pnpm test gave me failed tests for various files

    • add the pnpm test step to the CI so we avoid test failures in the future

Thank you for your comment and review @mcmxcdev. Indeed I haven't focused too much on tests up until now.

I've pushed some commits that fix the majority of the fails but I still have to fix the tests with the transfer endpoint because it seems that the endpoint data is being seen as body json (if you look at the controller) and not so much as file - so those tests are not up to date. I'm commenting them out for now if you agree (later we can improve tests)

@SpecialAro SpecialAro requested a review from a team October 2, 2023 01:48
@SpecialAro SpecialAro linked an issue Oct 12, 2023 that may be closed by this pull request
@SpecialAro SpecialAro merged commit e503468 into ferdium:main Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

5 participants