Skip to content

Instantly share code, notes, and snippets.

@araeuchle
Created August 18, 2019 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save araeuchle/6b1d389fd20eca9c4567dd9c8be1a9da to your computer and use it in GitHub Desktop.
Save araeuchle/6b1d389fd20eca9c4567dd9c8be1a9da to your computer and use it in GitHub Desktop.
import FluentMySQL
import Vapor
/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
// Register providers first
try services.register(FluentMySQLProvider())
// Register routes to the router
let router = EngineRouter.default()
try routes(router)
services.register(router, as: Router.self)
// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
// middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory
middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response
services.register(middlewares)
var databases = DatabasesConfig()
// Configure MySQL Database
let databaseConfig = MySQLDatabaseConfig(
hostname: Environment.get("DB_HOSTNAME")!,
username: Environment.get("DB_USER")!,
password: Environment.get("DB_PASSWORD")!,
database: Environment.get("DB_NAME")!
)
let database = MySQLDatabase(config: databaseConfig)
databases.add(database: database, as: .mysql)
services.register(database)
// Configure migrations
var migrations = MigrationConfig()
migrations.add(model: Dish.self, database: .mysql)
services.register(migrations)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment