Caps-Server/Sources/App/configure.swift

39 lines
1.2 KiB
Swift
Raw Normal View History

import Vapor
import Foundation
2021-11-08 21:58:55 +01:00
private(set) var server: CapServer!
2020-09-20 11:36:35 +02:00
public func configure(_ app: Application) throws {
2020-09-20 12:36:10 +02:00
app.http.server.configuration.port = 6001
2021-01-09 17:30:00 +01:00
app.routes.defaultMaxBodySize = "2mb"
2021-11-08 21:58:55 +01:00
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)
2021-11-08 21:58:55 +01:00
}
try Log.set(logFile: config.logPath)
let publicDirectory = app.directory.publicDirectory
if config.serveFiles {
let middleware = FileMiddleware(publicDirectory: publicDirectory)
app.middleware.use(middleware)
}
2022-05-27 09:25:41 +02:00
server = try CapServer(in: URL(fileURLWithPath: publicDirectory),
writers: config.writers)
2021-11-08 21:58:55 +01:00
// Register routes to the router
2020-09-20 11:36:35 +02:00
try routes(app)
}