Caps-Server/Sources/App/configure.swift
2022-05-27 09:25:41 +02:00

39 lines
1.2 KiB
Swift
Executable File

import Vapor
import Foundation
private(set) var server: CapServer!
public func configure(_ app: Application) throws {
app.http.server.configuration.port = 6001
app.routes.defaultMaxBodySize = "2mb"
let configFile = URL(fileURLWithPath: app.directory.resourcesDirectory)
.appendingPathComponent("config.json")
let config: Config
if !FileManager.default.fileExists(atPath: configFile.path) {
config = .default
let configData = try JSONEncoder().encode(config)
try configData.write(to: configFile)
print("Wrote default configuration")
} else {
let configData = try Data(contentsOf: configFile)
config = try JSONDecoder().decode(Config.self, from: configData)
}
try Log.set(logFile: config.logPath)
let publicDirectory = app.directory.publicDirectory
if config.serveFiles {
let middleware = FileMiddleware(publicDirectory: publicDirectory)
app.middleware.use(middleware)
}
server = try CapServer(in: URL(fileURLWithPath: publicDirectory),
writers: config.writers)
// Register routes to the router
try routes(app)
}