Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

My personal, minimum-viable Swift Server example. Blackfish for a Node.js Express-like API, Hypertext for HTML, and Alamofire for requests. Feels pretty good.

Notifications You must be signed in to change notification settings

sahandnayebaziz/iTunes-Search-with-Swift-Server-Blackfish-Hypertext-Alamofire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Demo.gif

Creating routes just like Express on Node...

app.get("/") { (req, res) in
    print("fulfilling request")
    res.send(text: "Hello World")
}

app.get("/search", AppleMusicController.getSearch)

Using structs and protocols in Swift to have great data structures...

struct AppleMusicTrack: Renderable {
    let trackId: String
    let trackName: String
    let artistName: String
    let trackAlbumArt: String
    var spotifyId: String?
    
    ...
}

Using Hypertext to create HTML with full type safety...

struct AppleMusicTrack: Renderable {
    ...
    
    func render() -> String {
        return div {
            [
                img( ["src": trackAlbumArt] ),
                p { [ b { artistName }, " ", trackName ] }
            ]
        }.render()
    }
}

Making requests with Alamofire

static func getSearch(req: Blackfire.Request, res: Response) -> Void {
        guard let searchQuery = (req.query["query"] as? String)?.removingPercentEncoding, searchQuery != "" else {
            res.status = 400
            res.send(text: "Please include a value under the parameter \"query\" to search iTunes.")
            return
        }

        let parameters = [ "entity": "song", "term": searchQuery, "limit": "10" ]
        request("https://itunes.apple.com/search", method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil)
            .responseJSON { response in
                let httpResponse = response.response!
                switch httpResponse.statusCode {
                case 200:
                
        ...
 }

I love using Node.js and I love using Swift. I haven't yet made a server with Swift, but trying this out today felt pretty good, so I just wanted to share this example to show how little code it takes to make something that does something.

This example uses Blackfire, a minimal web framework inspired by express, Hypertext, a small library for type-safe HTML, and Alamofire, the beloved HTTP library.

About

My personal, minimum-viable Swift Server example. Blackfish for a Node.js Express-like API, Hypertext for HTML, and Alamofire for requests. Feels pretty good.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages