-
Notifications
You must be signed in to change notification settings - Fork 459
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
Please can you make some example projects? #74
Comments
I am actually working on this. Example projects need not to be complex. I think the issue with micro is the same issue that redux had initially. People need examples, because some people can't figure out how to build a "real-world" app using smaller composable pieces (usually because they've always been doing monoliths or monolith-y apps). Small (draft) example import { json, send, createError } from 'micro-core'
import { compareSync } from 'bcrypt'
import { sign } from 'jsonwebtoken'
// Connect to Postgres
var db = require('knex')({
client: 'pg',
connection: process.env.PG_CONNECTION_STRING
})
/**
* Catch errors from the wrapped function.
* If any errors are catched, a JSON response is generated for that error.
*/
export const handleErrors = (fn) => async (req, res) => {
try {
return await fn(req, res)
} catch (err) {
if (process.env.NODE_ENV !== 'production' &&
err.stack) {
console.error(err.stack)
}
send(res, err.statusCode || 500, {
error: true,
message: err.message
})
}
}
/**
* Attempt to authenticate a user.
*/
const attempt = (email, password) =>
db('users').where({
email,
password
}).select('id')
.then((users) => {
if (!users.length) {
throw createError(401, 'Not authenticated')
}
const user = users[0]
if (!compareSync(password, user.password)) {
throw createError(401, 'Not authenticated')
}
return user
})
/**
* Authenticate a user and generate a JWT if successful.
*/
const auth = ({ email, password }) =>
attempt(email, password)
.then(({ id }) => ({
token: sign({
id
}, process.env.SECRET)
}))
export default handleErrors(
async (req, res) => await auth(
await json(req)
)
) Service discovery Public REST API In Kubernetes, there's some work being done on an Ingress resource that can act like an API gateway. Some of this was discussed shortly in #11. Wiki |
Also, there's https://github.com/zeit/micro-list |
@onbjerg definitely agree. Thanks for your wise comment! |
I made this tiny example to explain how to use micro https://github.com/sergiodxa/micro-platzi-profile. It's already working at now.sh here: https://micro-platzi-profile.now.sh/?username=sergiodxa You can also see an already working (open source) app using micro here http://platzigram.com/ and the source code in this repository https://github.com/platzi/platzigram/tree/season-2/season-2/platzigram-api This app has auth, file upload, routing, testing and a few more functionalities. |
hello guys, maybe I can sound a little weird, but I cant realized why should I use micro? :) Can you add some parallel example with and without micro to show what is the difference? Also, do you have some good article about why we need micro services architecture? |
Micro really needs some examples. I'm really excited by next.js, and micro seems its natural companion for building the backend supporting your next.js frontend. It would be great if someone familiar with micro could contribute an example of a REST API with the usual collection endpoint and detail endpoint (e.g. /things/ and /things/123/) |
Working on it #115. Feel free to add ideas for examples on that PR 😄 |
A really simple example found online: http://mxstbr.blog/2017/01/your-first-node-microservice/ |
@jon-ec Max showed it to us today. I will have a look into adding a |
I extracted the example from my previous comment and put it into a repository 🌟 |
With the help of @timneutkens i was able to setup a really boilerplate micro instance that has 3 mocha tests that test for "structural integrity" and booting up correctly. https://github.com/vuchl/micro-tested Thanks again @timneutkens ! |
I created a simple starter repo for a REST api + Authentication. Its based on @onbjerg`s comment. |
A bit more opinionated and integrated but yesterday I created a template project for vue-cli that incorporates micro as the backend service for a nuxt.js application: https://github.com/vuchl/nuxt-micro-template |
If you have any other examples at all, please add them to the readme 👍 |
Just as a note for others regarding a gateway to your micro-service API. Start simple. We have around 5 micro-services in our app so far and we use nginx as a simple api-gateway for:
nothing else. simple.. later if you need more control migrate to something like Kong. Don't do it till you need it. |
I took @littleStudent as example (thanks btw) and created a simpler one |
An example of middleware |
No description provided.
The text was updated successfully, but these errors were encountered: