Skip to content

Commit

Permalink
#4: Add tunnel collection
Browse files Browse the repository at this point in the history
  • Loading branch information
lika-vorobeva committed Sep 30, 2022
1 parent e3c5a98 commit 15a5b7c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ protocol NoContext {}

final class CommonContext {
let nodesService: NodesServiceType
let tunnelManager: TunnelManagerType

init(
nodesService: NodesServiceType
nodesService: NodesServiceType,
tunnelManager: TunnelManagerType
) {
self.nodesService = nodesService
self.tunnelManager = tunnelManager
}
}

protocol HasNodesService { var nodesService: NodesServiceType { get } }
extension CommonContext: HasNodesService {}

protocol HasTunnelManager { var tunnelManager: TunnelManagerType { get } }
extension CommonContext: HasTunnelManager {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import SOLARAPI
/// This class should configure all required services and inject them into a Context
final class ContextBuilder {
func buildContext() -> CommonContext {
let generalSettingsStorage = GeneralSettingsStorage()

let nodesProvider = NodesProvider(configuration: .init(baseURL: ClientConstants.backendURL))

let nodesService = NodesService(nodesProvider: nodesProvider)

let tunnelManager = TunnelManager(storage: generalSettingsStorage)

return CommonContext(
nodesService: nodesService
nodesService: nodesService,
tunnelManager: tunnelManager
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extension DVPNServer {
do {
let api = app.grouped(.init(stringLiteral: ClientConstants.apiPath))
try api.register(collection: NodesRouteCollection(context: context))
try api.register(collection: TunnelRouteCollection(context: context))
try app.start()
} catch {
fatalError(error.localizedDescription)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// PostConnectionRequest.swift
// SOLARdVPNCommunityCoreiOS
//
// Created by Lika Vorobeva on 30.09.2022.
//

import Foundation

struct PostConnectionRequest: Codable {
let nodeAddress: String

init(nodeAddress: String) {
self.nodeAddress = nodeAddress
}
}

// MARK: - Codable implementation

extension PostConnectionRequest {
enum CodingKeys: String, CodingKey {
case nodeAddress
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// TunnelRouteCollection.swift
// SOLARdVPNCommunityCoreiOS
//
// Created by Lika Vorobeva on 30.09.2022.
//

import Vapor

struct TunnelRouteCollection: RouteCollection {
let context: HasTunnelManager

func boot(routes: RoutesBuilder) throws {
routes.post("connection", use: createNewSession)
routes.delete("connection", use: startDeactivationOfActiveTunnel)
}
}

extension TunnelRouteCollection {
func startDeactivationOfActiveTunnel(_ req: Request) async throws -> Bool {
return !context.tunnelManager.startDeactivationOfActiveTunnel()
}

func createNewSession(_ req: Request) async throws -> Bool {
let continentCode = req.query[String.self, at: PostConnectionRequest.CodingKeys.nodeAddress.rawValue]

return true
}
}

0 comments on commit 15a5b7c

Please sign in to comment.