Skip to content

Commit

Permalink
feat(env): Ambientes criados
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRoger committed Dec 2, 2022
1 parent bee6eb4 commit 6dbf0c8
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WEBSERVER_HOST="http://:::3000"
1 change: 1 addition & 0 deletions .env.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WEBSERVER_HOST="https://www.tabnews.com.br"
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:flutter/material.dart';

import 'package:tabnews/src/app.dart';
import 'package:tabnews/src/enviroment_vars.dart';
import 'package:tabnews/src/preferences.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

await Preferences.init();

await EnviromentVars.init(useProdEnv: true);

runApp(const App());
}
29 changes: 29 additions & 0 deletions lib/src/enviroment_vars.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

class EnviromentVars {
static late EnviromentVars _singleton;
static EnviromentVars get getVars => _singleton;

late final String _webserver;
String get webserver => _webserver;

late final bool _debugEnv;
bool get debugEnv => _debugEnv;

static Future init({bool useProdEnv = false}) async {
bool devEnviroment = kDebugMode && !useProdEnv;
dotenv = DotEnv();

if (devEnviroment) {
await dotenv.load(fileName: ".env.dev");
} else {
await dotenv.load(fileName: ".env.prod");
}

_singleton = EnviromentVars();
_singleton._webserver =
dotenv.get("WEBSERVER_HOST", fallback: "www.tabnews.com.br");
_singleton._debugEnv = devEnviroment;
}
}
3 changes: 2 additions & 1 deletion lib/src/services/auth.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:tabnews/src/enviroment_vars.dart';

import 'package:tabnews/src/services/http_response.dart';

class AuthService {
final apiUrl = 'https://www.tabnews.com.br/api/v1';
String get apiUrl => '${EnviromentVars.getVars.webserver}/api/v1';

Future<HttpResponse> postLogin(String email, String password) async {
final response = await http.post(
Expand Down
3 changes: 2 additions & 1 deletion lib/src/services/content.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:tabnews/src/enviroment_vars.dart';
import 'package:tabnews/src/models/comment.dart';

import 'package:tabnews/src/models/content.dart';
import 'package:tabnews/src/services/http_response.dart';

class ContentService {
final apiUrl = 'https://www.tabnews.com.br/api/v1/contents';
String get apiUrl => "${EnviromentVars.getVars.webserver}/api/v1/contents";

Future<List<Content>> fetchContents({int page = 1}) async {
final response = await http.get(Uri.parse('$apiUrl?page=$page'));
Expand Down
3 changes: 2 additions & 1 deletion lib/src/services/user.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:tabnews/src/enviroment_vars.dart';

import 'package:tabnews/src/models/user.dart';

class UserService {
final apiUrl = 'https://www.tabnews.com.br/api/v1';
String get apiUrl => '${EnviromentVars.getVars.webserver}/api/v1';

Future<User> fetchUser(String username) async {
final response = await http.get(
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_dotenv:
dependency: "direct main"
description:
name: flutter_dotenv
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.2"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies:
markdown: ^6.0.1
markdown_editable_textinput: ^2.1.0
url_launcher: ^6.1.7
flutter_dotenv: ^5.0.2

dev_dependencies:
flutter_test:
Expand All @@ -69,7 +70,8 @@ flutter:
# To add assets to your application, add an assets section, like this:
assets:
- lib/assets/
# - images/a_dot_ham.jpeg
- .env.dev
- .env.prod

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
Expand Down

0 comments on commit 6dbf0c8

Please sign in to comment.