1

I'm using Perfect server-side Swift, and have some conditions in the main.swift where the server should be terminated before actually starting up. Is there a best-practice for handling this termination?

Currently, I'm throwing an error because a return doesn't work-- because it's not in the context of a function (see This is the error I'm throwing and concerned about. below). Here's what I have so far:

//
//  main.swift
//

import PerfectLib
import PerfectHTTP
import PerfectHTTPServer

enum ServerStartupError : Error {
    case FailedUserControllerSetup
}


if !UserController.setup() {
    // This is the error I'm throwing and concerned about.
    throw ServerStartupError.FailedUserControllerSetup
}

let server = HTTPServer()
let serverRoutes = ServerRoutes()
serverRoutes.addRoutes(server: server)
server.serverPort = 8181

do {
    // Launch the HTTP server.
    try server.start()
} catch PerfectError.networkError(let err, let msg) {
    print("Network error thrown: \(err) \(msg)")
}
Chris Prince
  • 7,288
  • 2
  • 48
  • 66

1 Answers1

1

Sorry, my fault, I misunderstood you! Please note that throw in the top level of main.swift is meaningless because the only process who will catch that error is the OS. Please try the code below:

import PerfectLib
import PerfectHTTP
import PerfectHTTPServer

if UserController.setup() {
  let server = HTTPServer()
  let serverRoutes = ServerRoutes()
  serverRoutes.addRoutes(server: server)
  server.serverPort = 8181

  do {
    // Launch the HTTP server.
    try server.start()
  } catch PerfectError.networkError(let err, let msg) {
    print("Network error thrown: \(err) \(msg)")
  } catch (let panic) {
    print("panic: \(panic)")
  } //end server starting

} else {    

    // here is the code that prompting user to finish the setup
    // before running the server. The program will display this
    // message and automatically exit the process (end of program)
    print("Setup is not completed.")

}//end if

Alternatively, you can also use the exit() function to preform a quit from the main.swift, with importing Foundation (the standard library), as below:

//
//  main.swift
//

import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import Foundation

if !UserController.setup() {
    // This is the error I'm throwing and concerned about.
    print("ServerStartupError.FailedUserControllerSetup")
    exit(-1)
}//end if

let server = HTTPServer()
let serverRoutes = ServerRoutes()
serverRoutes.addRoutes(server: server)
server.serverPort = 8181

do {
    // Launch the HTTP server.
    try server.start()
} catch PerfectError.networkError(let err, let msg) {
    print("Network error thrown: \(err) \(msg)")
} catch (let panic) {
    print("Panic: \(panic)")
} 
exit(0)

Both methods have pros and cons. The first one doesn't require anything else while the second one needs Foundation Library; Also I would say that the second one is more traditional and could provide an exit code, which allows you catch the error by wrapping it up in a customize boot loader or something. Unix / Linux uses exit code to check out the application process information and in some cases you can see these exit codes in system log, if available.

PerfectlyRock
  • 405
  • 2
  • 7
  • Cool. I didn't know exit() was available from this context. I think I'll change to using that. Thanks! I'll try this and if I like it, come back and Thumbs Up on your answer. – Chris Prince Nov 30 '16 at 17:56
  • Yeah, everything in C language of stdlib would be available if Foundation was imported, swift build is calling the clang ... You can say that swift is a modern version of C, C++, C#, or you can call it C##, C, ... lol Caution! when apply Foundation library in your Perfect Server, you must know what you are doing because there are differences between macOS and Linux in operation system level of Foundation Implementation. So I would like to suggest the plan A - simple, OS independant – PerfectlyRock Nov 30 '16 at 18:52
  • We have just actually improved this very special control of configuration and sever launch. Please check the new approach described below: https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/HTTPServer.md – PerfectlyRock Dec 01 '16 at 16:32