VeryGoodOpenSource / dart_frog

A fast, minimalistic backend framework for Dart 🎯
https://dartfrog.vgv.dev
MIT License
1.87k stars 150 forks source link

feat: database support and .env #302

Closed SPiercer closed 1 year ago

SPiercer commented 2 years ago

Description

Adding .env configurations in order to add databases support (i.e mysql or postgres) to the dart_frog server whereas once you have the .env values of for instance mysql address and credentials it will add it to the server initialization

Requirements

P.S

i'm glad to help by any means if anyone is ready to take this contribution outside of the vgv team i've been working on a similar project before called Palace but it's archived now since we went to uni and decided to come back later.

aaltynbek commented 2 years ago

@SPiercer Hello there is package https://pub.dev/packages/mysql_client to work with mysql DB, it's good for starting, but .env will be good for dart_frog

SPiercer commented 2 years ago

@SPiercer Hello there is package https://pub.dev/packages/mysql_client to work with mysql DB, it's good for starting, but .env will be good for dart_frog

@aaltynbek man, there is not way i can initialize the mysql client on the server because i have no access to edit server.dart basically in dev mode

ushieru commented 2 years ago

@SPiercer You can create a custom entrypoint, simply create a main.dart file at the root of your Dart Frog project. The main.dart file must expose a top-level run method.

Official Doc

import 'dart:io';

import 'package:dart_frog/dart_frog.dart';

Future<HttpServer> run(Handler handler, InternetAddress ip, int port) {
  // 1. Execute any custom code prior to starting the server...

  // 2. Use the provided `handler`, `ip`, and `port` to create a custom `HttpServer`.
  // Or use the Dart Frog serve method to do that for you.
  return serve(handler, ip, port);
}
mgesmundo commented 2 years ago

I use a custom entry point to set some values, but the problem is that it could be useful to set some env variables on dev mode using the dart_from CLI like this example:

dart_frog --define=SOME_TOKEN=1234 dev

In this way it is possible to compile and use the server for production:

dart_frog build
dart run --define=SOME_TOKEN=1234 build/bin/server.dart

Actually I can't figured out some workaround to set an env variable using dart_frog CLI. There is some other chance to run a compiled version?

dart compile exe build/bin/server.dart -o build/bin/server
build/bin/server --SOME_TOKEN=1234

Thank you.

GabrielRozendo commented 1 year ago

The package dotenv solves this issue for me very well. In my dev env, I'm using a .env file and in the prd server, the env var from machine.

IMO, I don't think it is a issue that Dart Frog should address, as you can do it by yourself, reading from a file from Dart or from yaml files as I've seen other dart server's project doing.

mgesmundo commented 1 year ago

Yes of course: to read a yaml file is the easy solution (I've also applied it), but if you want to make more safe the environment, you do not save password or other sensitive data in any local file.

GabrielRozendo commented 1 year ago

Yes of course: to read a yaml file is the easy solution (I've also applied it), but if you want to make more safe the environment, you do not save password or other sensitive data in any local file.

for that reason I use dotenv. In my machine, I have a .env file with the password in open text, and (without change any line of code), in prd server, it'll use the env from server (no file anymore)

AseemWangoo commented 1 year ago

I'm trying to use the envied package for storing keys. But whenever I run the

dart_frog dev

I always get this error

image

This is my project structure:

image

and this is the package that has the package envied installed

image
wolfenrain commented 1 year ago

I am closing this issue as this is outside of the scope of Dart Frog, both setting up env and database are really specific to how a user wants to do it and people have already given nice solutions in the issue thread.

We can in the near future maybe create some documentation/tutorials on how to set it up, but that will be it's own issue.