Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to serve JSON files? #26

Closed
damianesteban opened this issue Nov 19, 2017 · 2 comments
Closed

Possible to serve JSON files? #26

damianesteban opened this issue Nov 19, 2017 · 2 comments

Comments

@damianesteban
Copy link

Ambassador is a great project - I've been using it for testing and I love it.

Is it possible to serve JSON directly from a file, similar to how json-server works?

@fangpenlin
Copy link
Contributor

fangpenlin commented Nov 20, 2017

@damianesteban Thank you for so much! Really glad to hear 😄 🙌

Yes, it's possible, that's not a build-in function, but you can do it manually with JSONResponse, like this

router["/users/([0-9]+)"] = JSONResponse(handler: { environ -> Any in
    let captures = environ["ambassador.router_captures"] as! [String]
    let id = captures[0]
    // or read the JSON and parse it from a file
    let users = [
        "1": [
            "name": "john"
        ]
    ]
    return users[id]
})

or you can also define your own Response, which reads JSON files, parses URL parameters and return them accordingly. I think reference to how JSONResponse works

https://github.com/envoy/Ambassador/blob/master/Ambassador/Responses/JSONResponse.swift#L14-L71

could be a good start.

@damianesteban
Copy link
Author

Perfect! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants