Kodeco Forums

Kitura Tutorial: Getting Started with Server Side Swift

Do you wish your iOS skills worked on the backend? This Kitura tutorial will teach you to create RESTful APIs written entirely in Swift.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/99-kitura-tutorial-getting-started-with-server-side-swift

I’ve built the project, I get an output that says everything is running. I’ve opened up a browser (Chrome) and its at the Kitura homepage for localhost:8080/acronyms, but when I type the curl commands, I get could not resolve host: localhost. What am I doing wrong? I’ve also tried this with the downloaded final project you provided and the same occurred.

Hey!

Do you have any other services running on localhost:8080? My first guess would be to try updating the port in the App class to something different like 8090, and see if you get a different result.

Let me know if that works.

apparently I already has something running on 8090…didn’t even know that. I switched back to 8080 and took a screenshot of my logs if that helps.

I did have to create from docker as the brew install stuff wouldn’t work correctly.

Okay, I seem to have gotten it to work now. I was running a different Docker container but yours was not running. killed the other one, started yours and ran again in Xcode. I got back an empty database as I should have.

Are we not allowed to have different docker containers running?

Ok, so these logs indicate that CouchDB is not properly set up on your machine, or perhaps something else is taking up port 5984, which is the default port for CouchDB. This is because the logs indicate the database could not even be created. I’m curious as to why the Homebrew package installation seems to have failed for you, but nonetheless, it looks like the Docker approach worked.

You should be able to have different docker containers running, but it’s a matter of port availability. If you have multiple containers running, then that could create a collision somewhere, but it’s not likely if you know which containers you’re running and which ports you’re opening them to.

Let me know if you have further questions - I’m glad you got it up and running eventually! :grinning:

What is the difference between this an Vapor? Why should I use one over the other?

JT

Hi! I think Vapor is great, and it’s impressive to watch what Tanner, Logan, and the team have worked on.

I like Kitura because it’s modeled like Express.js, which is one of the most popular HTTP frameworks you will encounter in any language. Since that was the first non-Swift HTTP framework I encountered, it became natural to me to use it. Thus, using Kitura feels familiar to me by default.

It would be unfair of me to say anything educational about Vapor, but I can tell you that if you’ve ever used Express before, Kitura will seem familiar to you as well.

Above all, if you’re trying to choose, I’d advise you to try both. Having a choice is always a good thing, and most of the time, it comes down to personal preference.

If you’d like to ask more questions, feel free to sign onto our public slack at http://slack.kitura.io

Hi @dokun1i have some questions:

Do you think that a Swift server is stable for a production api?
Can we use PostgreSQL?

Our servers can support 5-10 million users per day.

Thanks!

PD: Is a very interesting tutorial. Ty

Hi Alvaro,

Yes, it’s ready for production. That could easily be taken as opinion, but IBM, amongst others, are offering support for people running Swift APIs in their clouds.

We do have support for an ORM that connects to PostgreSQL - if you try our FoodTracker tutorial at GitHub - IBM/FoodTrackerBackend: This tutorial teaches how to create a Kitura Swift backend for the FoodTracker iOS app tutorial from Apple. This project contains a version of the tutorial code that has been updated to use Codable rather than NSCoder. then you’ll see how to use it! You might see more materials on that on our website here!

I’m glad you liked the tutorial, thank you for your feedback!

For the delete request, maybe you lost the /<id> in the line app.router.delete("/acronyms", handler: deleteAcronym) in the file AcronymRoutes.swift

Hi,

I read your tutorial and got the same correct result. Now I want to know how we can generate the API with that which we can provide End user to consume. I have seen the API created but didn’t get how to distribute it to Front end.Can you please help on that or provide any reference regarding that?

@dokun1 Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi! I have written another tutorial on this subject about using Stencil to create websites in Swift with a Kitura API. You can try it out here: https://www.raywenderlich.com/181130/kitura-stencil-tutorial-how-to-make-websites-with-swift

Hi! So this is actually not necessary. One of the features of Codable Routing is that it knows the type of route you are expecting, and the mechanism to handle parameters included with the request detects one automatically.

If you we’re to use the v1.x way of routing in Kitura, you would need to include the /:id in the route at the end, to specify a parameter being mapped to the route.

Hope this answers your question!

Hi,

I’m following the tutorial and I got this error:

[2018-06-28T14:17:07.738-03:00] [ERROR] [Application.swift:51 createNewDatabase()] Could not create new database: (Optional(“Error: unauthorized, reason: You are not a server admin.”)) - acronym routes not created

Should I set a username and password for my database?

Thanks!

I get “Cannot GET /acronyms.” when I execute curl http://localhost:8080/acronyms
Same when I download the finished source. What am I doing wrong?

Same when I enter http://localhost:8080/acronyms in Safari, Cannot GET /acronyms.

I also got the same error when i saw my console it was this

[Application.swift:47 createNewDatabase()] Could not create a new database :Optional(“Internal Error”) - acronym routes not created

Hello! Let’s see if we can debug here - what happens if you visit localhost:5984 in your browser?