Skip to content

Commit

Permalink
Add server.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
marzvrover committed Dec 19, 2020
1 parent bb011db commit 8369ab6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 1 addition & 4 deletions Sources/Swiftcraft/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ let logger = Logger(label: "Swiftcraft")

logger.info("Welcome to Swiftcraft!")

let host = "127.0.0.1"
let port = 25565

var server = Server(host: host, port: port)
var server = Server()

defer {
server.shutdown()
Expand Down
18 changes: 15 additions & 3 deletions Sources/SwiftcraftLibrary/Networking/Server.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import NIO
import DotEnv

/// Server class
open class Server {
Expand Down Expand Up @@ -34,10 +35,21 @@ open class Server {
.childChannelOption(ChannelOptions.recvAllocator, value: AdaptiveRecvByteBufferAllocator())
}
/// To instantiate the server you provide the host and path
public init(host: String, port: Int) {
public init(host: String? = nil, port: Int? = nil) {
self.isRunning = false
self.host = host
self.port = port
DotEnv.load(path: "server.properties")
if host != nil {
self.host = host!
} else {
self.host = ProcessInfo.processInfo.environment["server-ip"] ?? "127.0.0.1"
}
if port != nil {
self.port = port!
} else if ProcessInfo.processInfo.environment["server-port"] == nil {
self.port = 25565
} else {
self.port = Int(ProcessInfo.processInfo.environment["server-port"]!)!
}
}
/// You start the server with the `run()` method.
/// This binds the `Server`.`bootstrap` to the `Server`.`host` and `Server`.`port`.
Expand Down
2 changes: 2 additions & 0 deletions server.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server-ip="127.0.0.1"
server-port=25565

0 comments on commit 8369ab6

Please sign in to comment.