Skip to content

Commit

Permalink
Add Arango+BFF to SuperAdmin APP
Browse files Browse the repository at this point in the history
  • Loading branch information
helderbetiol authored and vbeaucha committed Aug 14, 2023
1 parent 23f5870 commit 9d1b717
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 25 deletions.
27 changes: 16 additions & 11 deletions APP/ogree_app/lib/models/tenant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Tenant {
String docUrl;
String docPort;
String imageTag;
bool hasBff;
TenantStatus? status;

Tenant(
Expand All @@ -29,6 +30,7 @@ class Tenant {
this.docUrl,
this.docPort,
this.imageTag,
this.hasBff,
{this.status});

Map<String, dynamic> toMap() {
Expand All @@ -44,22 +46,25 @@ class Tenant {
'docUrl': docUrl,
'docPort': docPort,
'imageTag': imageTag,
'hasBff': hasBff,
};
}

factory Tenant.fromMap(Map<String, dynamic> map) {
return Tenant(
map['name'].toString(),
map['customerPassword'].toString(),
map['apiUrl'].toString(),
map['webUrl'].toString(),
map['apiPort'].toString(),
map['webPort'].toString(),
map['hasWeb'],
map['hasDoc'],
map['docUrl'].toString(),
map['docPort'].toString(),
map['imageTag'].toString());
map['name'].toString(),
map['customerPassword'].toString(),
map['apiUrl'].toString(),
map['webUrl'].toString(),
map['apiPort'].toString(),
map['webPort'].toString(),
map['hasWeb'],
map['hasDoc'],
map['docUrl'].toString(),
map['docPort'].toString(),
map['imageTag'].toString(),
map['hasBff'],
);
}

String toJson() => json.encode(toMap());
Expand Down
16 changes: 12 additions & 4 deletions APP/ogree_app/lib/widgets/tenants/popups/create_tenant_popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class _CreateTenantPopupState extends State<CreateTenantPopup> {
String _docPort = "";
bool _hasWeb = true;
bool _hasDoc = false;
bool _hasBff = false;
bool _isLoading = false;
PlatformFile? _loadedImage;
String _imageTag = "main";
Expand Down Expand Up @@ -78,8 +79,8 @@ class _CreateTenantPopupState extends State<CreateTenantPopup> {
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
getCheckBox("API", true, (_) {},
enabled: false),
// getCheckBox("API", true, (_) {},
// enabled: false),
getCheckBox(
"WEB",
_hasWeb,
Expand All @@ -92,6 +93,12 @@ class _CreateTenantPopupState extends State<CreateTenantPopup> {
(value) => setState(() {
_hasDoc = value!;
})),
getCheckBox(
"Arango",
_hasBff,
(value) => setState(() {
_hasBff = value!;
})),
],
),
const SizedBox(height: 10),
Expand Down Expand Up @@ -259,7 +266,8 @@ class _CreateTenantPopupState extends State<CreateTenantPopup> {
_hasDoc,
_docUrl,
_docPort,
_imageTag));
_imageTag,
_hasBff));
switch (result) {
case Success(value: final value):
widget.parentCallback();
Expand All @@ -278,7 +286,7 @@ class _CreateTenantPopupState extends State<CreateTenantPopup> {
getCheckBox(String title, bool value, Function(bool?) onChange,
{bool enabled = true}) {
return SizedBox(
width: 95,
width: 110,
child: CheckboxListTile(
activeColor: Colors.blue.shade600,
contentPadding: EdgeInsets.zero,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class _UpdateTenantPopupState extends State<UpdateTenantPopup> {
alignment: WrapAlignment.start,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
getCheckBox("API", true, (_) {}, enabled: false),
// getCheckBox("API", true, (_) {}, enabled: false),
getCheckBox(
"WEB",
widget.tenant.hasWeb,
Expand All @@ -67,6 +67,8 @@ class _UpdateTenantPopupState extends State<UpdateTenantPopup> {
(value) => setState(() {
widget.tenant.hasDoc = value!;
})),
getCheckBox("Arango", widget.tenant.hasBff, (_) {},
enabled: false),
],
),
getFormField(
Expand Down Expand Up @@ -182,7 +184,7 @@ class _UpdateTenantPopupState extends State<UpdateTenantPopup> {
getCheckBox(String title, bool value, Function(bool?) onChange,
{bool enabled = true}) {
return SizedBox(
width: 95,
width: 105,
child: CheckboxListTile(
activeColor: Colors.blue.shade600,
contentPadding: EdgeInsets.zero,
Expand Down
2 changes: 2 additions & 0 deletions APP/ogree_app_backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

var tmplt *template.Template
var bfftmplt *template.Template
var apptmplt *template.Template
var servertmplt *template.Template
var netboxtmplt *template.Template
Expand All @@ -35,6 +36,7 @@ func init() {
// []byte("password"), bcrypt.DefaultCost)
// println(string(hashedPassword))
tmplt = template.Must(template.ParseFiles("backend-assets/docker-env-template.txt"))
bfftmplt = template.Must(template.ParseFiles("backend-assets/bff-api-list-template.txt"))
apptmplt = template.Must(template.ParseFiles("flutter-assets/flutter-env-template.txt"))
servertmplt = template.Must(template.ParseFiles("backend-assets/template.service"))
netboxtmplt = template.Must(template.ParseFiles("tools-assets/netbox-docker-template.txt"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{"name":"server", "url": "http://{{.Name}}_arango_api:8080"},
{"name":"objects", "url": "http://{{.Name}}_api:{{.ApiPort}}"}
]
14 changes: 13 additions & 1 deletion APP/ogree_app_backend/backend-assets/docker-env-template.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE_DIR=https://github.com/ditrit/OGrEE-Core.git#{{.ImageTag}}
CORE_DIR=https://github.com/ditrit/OGrEE-Core.git#feat/bff
API_BUILD_DIR=API
APP_BUILD_DIR=APP
API_DOC_UI_PORT={{.DocPort}}
Expand All @@ -9,3 +9,15 @@ API_EXTERNALURL={{.ApiUrl}}
COMPOSE_PROJECT_NAME={{.Name}}
APP_ASSETS_DIR={{.AssetsDir}}
IMAGE_TAG={{.ImageTag}}

ARANGO_API_BUILD_DIR=ARANGO_API
ARANGO_API_PORT=8080
ARANGO_PASS=ogree
ARANGO_USER=root
ARANGO_DB=_system

BFF_BUILD_DIR=BFF
BFF_PORT={{.BffPort}}
BFF_API_LIST={{.BffApiListFile}}

AUTH_SECRET=myAwesomeApiSecret
43 changes: 37 additions & 6 deletions APP/ogree_app_backend/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -27,10 +28,13 @@ type tenant struct {
WebPort string `json:"webPort"`
DocUrl string `json:"docUrl"`
DocPort string `json:"docPort"`
BffPort string `json:"bffPort"`
HasWeb bool `json:"hasWeb"`
HasDoc bool `json:"hasDoc"`
HasBff bool `json:"hasBff"`
AssetsDir string `json:"assetsDir"`
ImageTag string `json:"imageTag"`
BffApiListFile string
}

type container struct {
Expand Down Expand Up @@ -182,6 +186,12 @@ func addTenant(c *gin.Context) {

func dockerCreateTenant(newTenant tenant) string {
tenantLower := strings.ToLower(newTenant.Name)
appDeployDir := DOCKER_DIR + "app-deploy/" + tenantLower + "/"
err := os.MkdirAll(appDeployDir, 0755)
if err != nil && !strings.Contains(err.Error(), "already") {
println(err.Error())
}

// Image tagging
if newTenant.ImageTag == "" {
newTenant.ImageTag = "main"
Expand All @@ -193,29 +203,50 @@ func dockerCreateTenant(newTenant tenant) string {
args = append(args, "--profile")
args = append(args, "web")
// Create flutter assets folder
newTenant.AssetsDir = DOCKER_DIR + "app-deploy/" + tenantLower
newTenant.AssetsDir = appDeployDir + "flutter"
addAppAssets(newTenant)
} else {
// docker does not accept it empty, even if it wont be created
newTenant.AssetsDir = DOCKER_DIR
}

if newTenant.HasDoc {
args = append(args, "--profile")
args = append(args, "doc")
}

if newTenant.HasBff {
args = append(args, "--profile")
args = append(args, "arango")
if newTenant.BffPort == "" {
// Set BFF and API ports
newTenant.BffPort = newTenant.ApiPort
port, _ := strconv.Atoi(newTenant.ApiPort)
newTenant.ApiPort = strconv.Itoa(port + 1)
}
file, _ := os.Create(appDeployDir + tenantLower + "-bff-api-list.json")
err := bfftmplt.Execute(file, newTenant)
if err != nil {
fmt.Println("Error creating bff api list file: " + err.Error())
newTenant.BffApiListFile = "./bff_api_list.json"
} else {
newTenant.BffApiListFile = "./app-deploy/" + tenantLower + "/" + tenantLower + "-bff-api-list.json"
}
file.Close()
}
args = append(args, "up")
args = append(args, "--build")
args = append(args, "-d")

// Create .env file
file, _ := os.Create(DOCKER_DIR + ".env")
err := tmplt.Execute(file, newTenant)
err = tmplt.Execute(file, newTenant)
if err != nil {
panic(err)
}
file.Close()
// Create tenantName.env as a copy
file, _ = os.Create(DOCKER_DIR + tenantLower + ".env")
file, _ = os.Create(appDeployDir + tenantLower + ".env")
err = tmplt.Execute(file, newTenant)
if err != nil {
fmt.Println("Error creating .env copy: " + err.Error())
Expand Down Expand Up @@ -285,7 +316,7 @@ func addTenantLogo(c *gin.Context) {
c.String(http.StatusInternalServerError, err.Error())
}
// Make sure destination dir is created
assetsDir := DOCKER_DIR + "app-deploy/" + tenantName
assetsDir := DOCKER_DIR + "app-deploy/" + tenantName + "/flutter"
err = os.MkdirAll(assetsDir, 0755)
if err != nil && !strings.Contains(err.Error(), "already") {
c.String(http.StatusInternalServerError, err.Error())
Expand All @@ -302,7 +333,7 @@ func removeTenant(c *gin.Context) {
tenantName := strings.ToLower(c.Param("name"))

// Stop and remove containers
for _, str := range []string{"_webapp", "_api", "_db", "_doc"} {
for _, str := range []string{"_webapp", "_api", "_db", "_doc", "_bff", "_arango_api", "_arango_db"} {
cmd := exec.Command("docker", "rm", "--force", strings.ToLower(tenantName)+str)
cmd.Dir = DOCKER_DIR
var stderr bytes.Buffer
Expand All @@ -315,7 +346,7 @@ func removeTenant(c *gin.Context) {
}

// Remove assets
os.RemoveAll(DOCKER_DIR + "app-deploy/" + tenantName)
os.RemoveAll(DOCKER_DIR + "app-deploy/" + tenantName + "/flutter")
os.Remove(DOCKER_DIR + tenantName + ".env")

// Update local file
Expand Down
1 change: 1 addition & 0 deletions deploy/docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ARANGO_DB=_system
#BFF
BFF_BUILD_DIR=BFF
BFF_PORT=8085
BFF_API_LIST=./bff_api_list.json

#AUTH
AUTH_SECRET=myAwesomeApiSecret
Expand Down
2 changes: 1 addition & 1 deletion deploy/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ services:
- BFF_PORT=${BFF_PORT}
- BFF_SECRET=${AUTH_SECRET}
volumes:
- ./bff_api_list.json:/bin/api.json
- ${BFF_API_LIST}:/bin/api.json
depends_on:
- ogree_api
- arango_api
Expand Down

0 comments on commit 9d1b717

Please sign in to comment.