Home Namaste Swift Server
Post
Cancel

Namaste Swift Server

πŸŒ‘ Outline

Swift has gained significant traction since its launch in 2014. Currently, it’s ranked as the 8th most in-demand language to learn. While many associate Swift with iOS and Mac development, its open-sourcing in 2015 unlocked new possibilities. One exciting area is server-side development, which we’ll immersed in today.

We will use the Vapor Web framework to implement the HTTP web service in Swift. We will try to implement a simple login service with Vapor.

πŸŒ“ Talk is cheap. Show me the code

To use Vapor on macOS, please ensure you have Swift 5.6 or greater.

Instal Vapor on macOS

The Vapor is available through Homebrew only. if you already have a Homebrew setup then run the following command.

brew install vapor

To set up Homebrew please visit the link

Create a Vapor Project

Once Vapor is installed run the following command to create a new project.

vapor new NamasteSwiftServer -n

-n flag for the bare minimum project. if you want to use ORM or any other setting then avoid this.

You can create a project through the Xcode SPM option also, however, to set up a minimal project will also take some effort.

Run Template Server

Open the project in Xcode and run the application.

Congratulations! Your server is running at the following path:

1
[ NOTICE ] Server starting on http://127.0.0.1:8080

If you want to change port or API then open the configure.swift file.

1
2
3
4
5
6
7
// configures your application
public func configure(_ app: Application) async throws {
    // register routes
	app.http.server.configuration.hostname = "127.0.0.1"
    app.http.server.configuration.port = Int(Environment.get("PORT") ?? "8081" ) ?? 8081
    try routes(app)
}

Sometimes it may happen that a specific IP address cannot be assigned to the application due to a system firewall restriction or any other cause, so choose wisely.

Login Service

Navigate to the routes.swift file and you can write your login logic in the following way:

1
2
3
4
5
6
7
8
app.get("auth") { req async -> LoginResponse in
        let username: String? = req.query["username"]
        let userpwd: String? = req.query["password"]
        if username == "admin" && userpwd == "testadmin" {
            return LoginResponse(isSuccess: true, error: [:])
        }
        return LoginResponse(isSuccess: false, error: ["Login_001": "Inavlid Username & Password"])
    }
  • get
    An HTTP Method to get the data. You can use any HTTP method based on your requirements.

  • auth
    It is a part of the request’s URI.
    1
    
    http://127.0.0.1:8080/auth
    
  • req
    An object of the Request class contains all the request details like headers, request query, body etc.

πŸŒ• Time to test server

Once again run the application in Xcode. Open the browser and type the URL:

1
 http://127.0.0.1:8080/auth?username=admin&password=testadmin

The response is:

1
{"error":{},"isSuccess":true}

Now type with invalid username & password:

1
{"isSuccess":false,"error":{"Login_001":"Inavlid Username & Password"}}

Congratulations! You have a server up and running.

🌚 Conclusion

You can find the source code here.

Try it!, one of the major use cases can be to Mock the API for Mobile Testing apart from completely writing the server in Swift.

This post is licensed under CC BY 4.0 by the author.

Case 001 - AnyView kills Performance

SwiftUI - Fixed vs Dynamic Size Spacer

Comments powered by Disqus.