This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
+ less boilerplate | |
+ default initializer | |
- you can alter `limit` | |
*/ | |
struct MyRequestInput1 { | |
let searchTerm: String | |
var limit: Int = 10 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum IntOrString: Encodable { | |
case integer(Int) | |
case string(String) | |
func encode(to encoder: Encoder) throws { | |
var container = encoder.singleValueContainer() | |
switch self { | |
case .integer(let val): try container.encode(val) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func configure(_ app: Application) throws { | |
// Serves files from `Public/` directory | |
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) | |
app.middleware.use(CORSMiddleware()) // Add CORS with default configuration | |
// Configure SQLite database | |
app.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite) | |
// Configure migrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if you are using GitHub, you can use this tool to change all repos at once | |
# https://github.com/mheap/github-default-branch | |
# Rename dev to develop | |
github-default-branch --pat <token> --repo user/repo --old dev --new develop | |
# Rename all repos owned by an org | |
github-default-branch --pat <token> --org my-org-name | |
# Rename all repos owned by a user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Assuming the route handler is attached to `http://localhost:8080/import-data` you can test the implementation with this request (cURL): | |
``` | |
curl -X "POST" "http://localhost:8080/import-data" \ | |
-H 'Content-Type: application/json; charset=utf-8' \ | |
-d $'{ "values": ["first", "foo", "bar", "last"] }' | |
``` | |
Prints: | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import FluentMySQL | |
/* | |
Starting with Swift 5.2 the compiler is not able to resolve this automatically: | |
`migrations.add(model: Todo.self, database: .mysql)` | |
See: https://forums.swift.org/t/vapor-3-swift-5-2-regression/34764 | |
Instead of adding this to each model, I added these extensions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker run -d \ | |
--restart=always \ | |
-p 33060:3306 \ | |
-e MYSQL_ROOT_PASSWORD=root \ | |
-v ~/docker/volumes/mysql:/var/lib/mysql \ | |
--name mysql-5.7 \ | |
mysql:5.7 \ | |
--sql-mode="NO_ENGINE_SUBSTITUTION" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NIO | |
/// Author: vzsg (Discord) - https://twitter.com/vzsg_dev | |
/// Source: https://discordapp.com/channels/431917998102675485/684159753189982218/684537099378098272 | |
extension EventLoopFuture { | |
func tryFlatMap<NewValue>( | |
file: StaticString = #file, | |
line: UInt = #line, | |
_ callback: @escaping (Value) throws -> EventLoopFuture<NewValue> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* NOTE: check out 3-MySQLDataConvertibleEnum_Better_Version.swift. */ | |
import Fluent | |
import FluentMySQL | |
import Vapor | |
protocol MySQLDataConvertibleEnum: MySQLDataConvertible, ReflectionDecodable, CaseIterable { } | |
extension MySQLDataConvertibleEnum where Self: RawRepresentable, Self.RawValue == String { | |
func convertToMySQLData() -> MySQLData { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func boot(routes: RoutesBuilder) throws { | |
routes.get("", use: getBooks) | |
routes.get(":bookID", use: getBook) | |
} |
NewerOlder