Skip to content

Commit

Permalink
Merge pull request #2 from loloop/mc/de-ginnify
Browse files Browse the repository at this point in the history
removes ginny from vapor project
  • Loading branch information
loloop authored Sep 28, 2023
2 parents adc79df + fdf8072 commit dc61c1c
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 63 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*/.build
/.build
/Packages
/*.xcodeproj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ final class APIRequester: APIRequesting {

func makeURL(path: String, queryItems: [URLQueryItem]?) throws -> URL? {
#if DEBUG
let host: String = "localhost"
// let host: String = "localhost"
let host: String = "quake.host"
#else
let host: String = "myones-backend.onrender.com"
#endif
Expand All @@ -51,9 +52,10 @@ final class APIRequester: APIRequesting {
// let host: String = try Configuration.value(for: "BASE_URL", in: .main)

var components = URLComponents()
components.scheme = isLocalhost ? "http" : "https"
components.scheme = isLocalhost ? "http" : "http"
components.host = host
if isLocalhost { components.port = 8080 }
// if isLocalhost { components.port = 8080 }
components.port = 8080
components.path = "/\(path)"
components.queryItems = queryItems
return components.url
Expand Down
57 changes: 55 additions & 2 deletions SuperLandoAdmin/AdminLib/Sources/AdminLib/UploadRace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ public struct UploadRaceEvent: Codable, Equatable, Identifiable {
var date: Date
}

public enum UploadRaceEventBundle: Equatable {
case F1Sprint
case F1Regular

var tag: String {
switch self {
case .F1Sprint, .F1Regular:
"f1"
}
}

var events: [UploadRaceEvent] {
switch self {
case .F1Sprint:
[
.init(title: "Practice 1", date: Date()),
.init(title: "Qualifying", date: Date()),
.init(title: "Practice 2", date: Date()),
.init(title: "Sprint", date: Date()),
.init(title: "Race", date: Date()),
]
case .F1Regular:
[
.init(title: "Practice 1", date: Date()),
.init(title: "Practice 2", date: Date()),
.init(title: "Practice 3", date: Date()),
.init(title: "Qualifying", date: Date()),
.init(title: "Race", date: Date()),
]
}
}
}

public struct UploadRace: Reducer {
public init() {}

Expand All @@ -40,6 +73,7 @@ public struct UploadRace: Reducer {
case uploadRace(APIClient<UploadRaceRequest>.Action)
case submitRace
case addEvent
case addBundle(UploadRaceEventBundle)
case updateEventTitle(UploadRaceEvent, String)
case updateEventDate(UploadRaceEvent, Date)
case binding(BindingAction<State>)
Expand All @@ -51,7 +85,16 @@ public struct UploadRace: Reducer {
Reduce { state, action in
switch action {
case .addEvent:
state.events.append(.init(title: "", date: Date()))
if let ev = state.events.first {
state.events.append(.init(title: "", date: ev.date))
} else {
state.events.append(.init(title: "", date: Date()))
}
return .none

case .addBundle(let bundle):
state.raceTag = bundle.tag
state.events = bundle.events
return .none

case .updateEventTitle(let event, let title):
Expand Down Expand Up @@ -82,7 +125,7 @@ public struct UploadRace: Reducer {
try await send(.uploadRace(.request(.post(request))))
}

case .uploadRace(.response(.finished)):
case .uploadRace(.response(.finished(.success))):
state.raceTitle = ""
state.raceTag = ""
state.events = []
Expand Down Expand Up @@ -146,6 +189,16 @@ public struct UploadRaceView: View {
viewStore.send(.addEvent)
}

Menu("add bundle") {
Button("F1 Sprint") {
viewStore.send(.addBundle(.F1Sprint))
}

Button("F1 Regular") {
viewStore.send(.addBundle(.F1Regular))
}
}

} header: {
Text("upload race")
} footer: {
Expand Down
4 changes: 4 additions & 0 deletions SuperLandoAdmin/SuperLandoAdmin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/* Begin PBXFileReference section */
B40901912AC1B7B900B6F436 /* Upload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Upload.swift; sourceTree = "<group>"; };
B40901932AC1C0E200B6F436 /* TestRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestRequest.swift; sourceTree = "<group>"; };
B42603E32AC59C43009F9936 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
B48609DB2AC126650098866E /* SuperLandoAdmin.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SuperLandoAdmin.app; sourceTree = BUILT_PRODUCTS_DIR; };
B48609DE2AC126650098866E /* SuperLandoAdminApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuperLandoAdminApp.swift; sourceTree = "<group>"; };
B48609E02AC126650098866E /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -68,6 +69,7 @@
B48609DD2AC126650098866E /* SuperLandoAdmin */ = {
isa = PBXGroup;
children = (
B42603E32AC59C43009F9936 /* Info.plist */,
B48609EC2AC126860098866E /* SuperLandoAdmin.entitlements */,
B48609DE2AC126650098866E /* SuperLandoAdminApp.swift */,
B48609E02AC126650098866E /* ContentView.swift */,
Expand Down Expand Up @@ -301,6 +303,7 @@
DEVELOPMENT_TEAM = 7N9Z7384YN;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SuperLandoAdmin/Info.plist;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down Expand Up @@ -334,6 +337,7 @@
DEVELOPMENT_TEAM = 7N9Z7384YN;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SuperLandoAdmin/Info.plist;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down
19 changes: 19 additions & 0 deletions SuperLandoAdmin/SuperLandoAdmin/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://quake.host</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>
27 changes: 0 additions & 27 deletions backend/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@
"version" : "2.8.0"
}
},
{
"identity" : "ginny",
"kind" : "remoteSourceControl",
"location" : "https://github.com/gonzalonunez/ginny.git",
"state" : {
"revision" : "4ab1e7d43d3ef4d161cccdad6145fe90f11c666c",
"version" : "0.1.1"
}
},
{
"identity" : "multipart-kit",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -117,15 +108,6 @@
"version" : "1.0.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -225,15 +207,6 @@
"version" : "1.0.2"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "edd2d0cdb988ac45e2515e0dd0624e4a6de54a94",
"version" : "0.50800.0-SNAPSHOT-2022-12-29-a"
}
},
{
"identity" : "vapor",
"kind" : "remoteSourceControl",
Expand Down
5 changes: 0 additions & 5 deletions backend/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ let package = Package(
.package(url: "https://github.com/vapor/fluent.git", from: "4.8.0"),
// 🐘 Fluent driver for Postgres.
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.7.2"),
.package(url: "https://github.com/gonzalonunez/ginny.git", from: "0.1.1"),
],
targets: [
.executableTarget(
Expand All @@ -22,10 +21,6 @@ let package = Package(
.product(name: "Fluent", package: "fluent"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.product(name: "Vapor", package: "vapor"),
.product(name: "Ginny", package: "ginny"),
],
plugins: [
.plugin(name: "GinnyPlugin", package: "ginny")
]
),
.testTarget(name: "AppTests", dependencies: [
Expand Down
37 changes: 37 additions & 0 deletions backend/Sources/App/AsyncRequestHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// AsyncRequestHandler.swift
//
//
// Created by Mauricio Cardozo on 28/09/23.
//

import Foundation
import Vapor

// This file is part of the de-ginnyfication of the repo - We should investigate the memory issues and bring ginny back

public protocol AsyncRequestHandler {
init()
associatedtype Response: AsyncResponseEncodable
var method: HTTPMethod { get }
var path: String { get }
func handle(req: Request) async throws -> Response
}

extension AsyncRequestHandler {
public func register(in app: Application) {
app.logger.info("registering \(path.pathComponents) as \(method.string)")
app
.on(method, path.pathComponents) { [handle] req async throws in
return try await handle(req)
}
}
}

extension Array where Element == any AsyncRequestHandler {
func register(in app: Application) {
self.forEach {
$0.register(in: app)
}
}
}
6 changes: 6 additions & 0 deletions backend/Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ public func configure(_ app: Application) async throws {

app.migrations.add(v0_1Migration())

[
UploadCategoryHandler(),
NextRaceHandler(),
UploadRaceHandler()
].register(in: app)

try await app.autoMigrate()
}
2 changes: 0 additions & 2 deletions backend/Sources/App/entrypoint.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Vapor
import Dispatch
import Logging
import Ginny

/// This extension is temporary and can be removed once Vapor gets this support.
private extension Vapor.Application {
Expand Down Expand Up @@ -29,7 +28,6 @@ enum Entrypoint {

let app = Application(env)
defer { app.shutdown() }
app.registerRoutes()

do {
try await configure(app)
Expand Down
2 changes: 1 addition & 1 deletion backend/Sources/App/pages/category.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import Vapor
import Foundation
import Ginny

struct UploadCategoryHandler: AsyncRequestHandler {
var method: HTTPMethod { .POST }
var path: String { "category" }

func handle(req: Request) async throws -> some AsyncResponseEncodable {
let request = try req.content.decode(UploadCategoryRequest.self)
Expand Down
17 changes: 12 additions & 5 deletions backend/Sources/App/pages/next-race.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@

import Foundation
import Vapor
import Ginny

struct NextRaceHandler: AsyncRequestHandler {
var method: HTTPMethod { .GET }

var path: String { "next-race" }

@Sendable
func handle(req: Request) async throws -> some AsyncResponseEncodable {
let args = try req.query.decode(NextRaceRequest.self)
var args: String
do {
args = try req.query.decode(NextRaceRequest.self).argument
} catch {
args = ""
}

let currentDate = Date()

let query = Race
Expand All @@ -24,8 +31,8 @@ struct NextRaceHandler: AsyncRequestHandler {
.with(\.$events)
.with(\.$category)

if !args.argument.isEmpty {
query.filter(Category.self, \.$tag, .equal, args.argument)
if !args.isEmpty {
query.filter(Category.self, \.$tag, .equal, args)
}

let nextRace = try await query.first()
Expand Down
2 changes: 1 addition & 1 deletion backend/Sources/App/pages/race.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import Vapor
import Foundation
import Ginny

struct UploadRaceHandler: AsyncRequestHandler {
var method: HTTPMethod { .POST }
var path: String { "race" }

func handle(req: Request) async throws -> some AsyncResponseEncodable {
let request = try req.content.decode(UploadRaceRequest.self)
Expand Down
15 changes: 9 additions & 6 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

git fetch
git pull origin main
cd telegram
swift build
sudo systemctl restart landinho.service
cd ..
cd backend
docker compose up -d
(
cd telegram
swift build
sudo systemctl restart landinho.service
)
(
cd backend
docker compose up -d --build
)
Loading

0 comments on commit dc61c1c

Please sign in to comment.