4

first post!

I'm a student learning swift and I'm on a rather big crash course. My professor started teaching us how to do simple get and post requests last time, but I'm still catching up and very clearly haven't grasped some of the basics.

This is my first time working with Kitura and only the second time coding in Swift. For some reason when I use swift run, I get my print and a sudden "Program ended with exit code : 0" rather than having a localhost running on my 8080 port to verify my get response on localhost:8080/.

Could someone help me figure out what I'm not seeing? Or haven't understood about server-side swift and command lines?

print("Hello, world from Swift Main!")

import Kitura

//constant router
let router = Router()


//When the router gets a request (contains everything needed to interpret the request), the server will respond with (Hello World or whatever data)
router.get("/") { request, response, next in
    response.send("Hello world from router.get") //response
    next() //either end the route or go on to the next one
}

//What port for the server to run on
Kitura.addHTTPServer(onPort: 8080, with: router)


//Need to add routes before run(), either in different file or on main
Kitura.run()

Thanks!

Cactus
  • 27,075
  • 9
  • 69
  • 149

1 Answers1

4

The code you have provided is correct, so it may be that your project structure is not.

In order to correctly create the project, you can follow the steps below:
1. Create a new directory, eg jamie.
2. From that directory run swift package init --type=executable to create a new project. The project will be called jamie after the directory name.
3. Edit Sources/jamie/main.swift and add your code.
4. Edit Package.swift and add the following to the dependencies of the "Jamie" package:
.package(url: "https://github.com/IBM-Swift/Kitura.git", .upToNextMajor(from: "2.5.0")),.
5. Edit Package.swift and add the as the list of dependencies for the "jamie" target:
dependencies: ["Kitura"]),

That should then run correctly. If it doesn't, it may be because the port you are trying to use is already in use. If you add a logger to your project, Kitura will log an error message if it cannot bind to the port. To do that follow the steps below:

  1. Edit Package.swift and add the following to the dependencies of the "Jamie" package:
    .package(url: "https://github.com/IBM-Swift/HeliumLogger.git", .upToNextMinor(from: "1.7.1")),.
  2. Edit Package.swift and add the logger to the list of dependencies for the "jamie" target so that it becomes:
    dependencies: ["Kitura", HeliumLogger"]),
  3. Edit Sources/jamie/main.swift and add the following to the top of the file:

    import LoggerAPI
    import HeliumLogger
    
    HeliumLogger.use(LoggerMessageType.info)
    

    If the port is already in use, you'll get a message similar to the following:

[2019-02-17T12:01:40.723Z] [ERROR] [Kitura.swift:139 start()] Error listening on port 8080: Error code: -9992(0x-2708), Address already in use. Use server.failed(callback:) to handle

If you haven't already, its probably worth joining the Kitura slack organisation as http://slack.kitura.io

Chris Bailey
  • 292
  • 1
  • 3
  • 1
    Yup, you're right! It was definitely my port. I'll be sure to throw HeliumLogger in there right away. Thanks so much! :) – Jamie BRANNAN Feb 18 '19 at 09:38
  • If you get that last error stated, follow the first 2 commands [here](https://stackoverflow.com/a/32962676/9607863). – George Feb 05 '20 at 15:06