Giter Site home page Giter Site logo

sahandnayebaziz / itunes-search-with-swift-server-blackfish-hypertext-alamofire Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 4.33 MB

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.

Swift 100.00%
swift server itunes hypertext html blackfish alamofire

itunes-search-with-swift-server-blackfish-hypertext-alamofire's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.