Skip to content

Commit

Permalink
#110 code example to connect to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Reindert Vetter committed May 6, 2021
1 parent bd79fdc commit ae26e9c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions app/http/controllers/homepage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
1 change: 1 addition & 0 deletions app/providers/provider_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ var Providers = struct {
BootProviders: []inter.BootServiceProvider{
AppServiceProvider{},
RouteServiceProvider{},
providers.DatabaseServiceProvider{Connections: config.Database.Connections},
},
}
59 changes: 59 additions & 0 deletions config/database.go
Original file line number Diff line number Diff line change
@@ -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,
},
*/
},
}
11 changes: 6 additions & 5 deletions config/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit ae26e9c

Please sign in to comment.