From ae26e9c49e88581d0a9398fbe9296b0a745f8599 Mon Sep 17 00:00:00 2001 From: Reindert Vetter Date: Thu, 6 May 2021 22:45:08 +0200 Subject: [PATCH] confetti-framework/confetti#110 code example to connect to database --- .env | 6 ++-- app/http/controllers/homepage.go | 9 +++-- app/providers/provider_index.go | 1 + config/database.go | 59 ++++++++++++++++++++++++++++++++ config/index.go | 11 +++--- 5 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 config/database.go diff --git a/.env b/.env index 918faf7..a072613 100755 --- a/.env +++ b/.env @@ -6,8 +6,8 @@ APP_DEBUG=true APP_URL=http://localhost DB_CONNECTION=mysql -DB_HOST=127.0.0.1 +DB_HOST=mysql DB_PORT=3306 DB_DATABASE=confetti -DB_USERNAME=root -DB_PASSWORD= +DB_USERNAME=app +DB_PASSWORD=secret diff --git a/app/http/controllers/homepage.go b/app/http/controllers/homepage.go index 35add1e..16d050b 100644 --- a/app/http/controllers/homepage.go +++ b/app/http/controllers/homepage.go @@ -3,10 +3,15 @@ package controllers import ( "github.com/confetti-framework/contract/inter" "github.com/confetti-framework/foundation/http/outcome" - "src/resources/views" ) // Homepage contains the code responsible for generating the home page. func Homepage(request inter.Request) inter.Response { - return outcome.Html(views.Homepage(request.App(), "Confetti", "Let's be creative!")) + db := request.App().Db() + databases := db.Query("SHOW databases;") + if databases.Empty() { + + return outcome.Html("Database empty") + } + return outcome.Html("Databases: "+databases.First().String()) } diff --git a/app/providers/provider_index.go b/app/providers/provider_index.go index 6fdc31c..6d9a8c9 100644 --- a/app/providers/provider_index.go +++ b/app/providers/provider_index.go @@ -49,5 +49,6 @@ var Providers = struct { BootProviders: []inter.BootServiceProvider{ AppServiceProvider{}, RouteServiceProvider{}, + providers.DatabaseServiceProvider{Connections: config.Database.Connections}, }, } diff --git a/config/database.go b/config/database.go new file mode 100644 index 0000000..1aef959 --- /dev/null +++ b/config/database.go @@ -0,0 +1,59 @@ +package config + +import ( + "github.com/confetti-framework/contract/inter" + "github.com/confetti-framework/foundation/db" + "github.com/confetti-framework/support/env" + "time" +) + +var Database = struct { + Default string + Connections map[string]inter.Connection +}{ + + /* + |-------------------------------------------------------------------------- + | Default Database Connection Name + |-------------------------------------------------------------------------- + | + | Here you may specify which of the database connections below you wish + | to use as your default connection for all database work. Of course + | you may use many connections at once. + | + */ + Default: env.StringOr("DB_CONNECTION", "mysql"), + + /* + |-------------------------------------------------------------------------- + | Database Connections + |-------------------------------------------------------------------------- + | + | Here are each of the database connections setup for your application. + | Of course, examples of configuring each database platform that is + | supported by Confetti is shown below to make development simple. + | + */ + + Connections: map[string]inter.Connection{ + "mysql": &db.MySQL{ + Host: env.StringOr("DB_HOST", "127.0.0.1"), + Port: env.IntOr("DB_PORT", 3306), + Database: env.StringOr("DB_DATABASE", "confetti"), + Username: env.StringOr("DB_USERNAME", "app"), + Password: env.StringOr("DB_PASSWORD", ""), + QueryTimeout: 10 * time.Second, + }, + + /* + "postgresql": &db.PostgreSQL{ + Host: env.StringOr("DB_HOST", "127.0.0.1"), + Port: env.IntOr("DB_PORT", 5432), + Database: env.StringOr("DB_DATABASE", "confetti"), + Username: env.StringOr("DB_USERNAME", "app"), + Password: env.StringOr("DB_PASSWORD", ""), + QueryTimeout: 10 * time.Second, + }, + */ + }, +} diff --git a/config/index.go b/config/index.go index 633105c..42f892b 100644 --- a/config/index.go +++ b/config/index.go @@ -3,9 +3,10 @@ package config // Index of all configuration. Add your configuration to the list. This allows // the framework and modules to use your configuration. var Index = map[string]interface{}{ - "App": App, - "Path": Path, - "Errors": Errors, - "Logging": Logging, - "Embed": Embed, + "App": App, + "Path": Path, + "Errors": Errors, + "Logging": Logging, + "Embed": Embed, + "Database": Database, }