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

Please can you make some example projects? #74

Closed
peirancao opened this issue Aug 6, 2016 · 19 comments
Closed

Please can you make some example projects? #74

peirancao opened this issue Aug 6, 2016 · 19 comments

Comments

@peirancao
Copy link

No description provided.

@onbjerg
Copy link
Contributor

onbjerg commented Aug 6, 2016

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
A simple draft for an auth service that takes an e-mail and a password and returns a JWT on success can be expressed in only 67 lines (with comments and a custom error handler):

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
Service discovery is not in micro, and it shouldn't be. Using something like Kubernetes w/ Docker, you can use the built-in DNS service discovery, if you need to communicate between services.

Public REST API
To expose a public API built from your services, you can use the API gateway pattern. I've heard some things about Kong, but I haven't tried it myself.

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
I think we discussed building a wiki at some point in #16. The wiki could contain examples. Furthermore, there are some examples of some patterns in #16 one might use. There's also some more patterns in #8.

@onbjerg
Copy link
Contributor

onbjerg commented Aug 6, 2016

Also, there's https://github.com/zeit/micro-list

@rauchg
Copy link
Member

rauchg commented Aug 12, 2016

@onbjerg definitely agree. Thanks for your wise comment!

@sergiodxa
Copy link

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.

@djindjic
Copy link

djindjic commented Nov 9, 2016

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?

@olifante
Copy link

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/)

@timneutkens
Copy link
Member

Working on it #115. Feel free to add ideas for examples on that PR 😄

@jon-ec
Copy link

jon-ec commented Jan 12, 2017

A really simple example found online: http://mxstbr.blog/2017/01/your-first-node-microservice/

@timneutkens
Copy link
Member

@jon-ec Max showed it to us today. I will have a look into adding a Tutorials section. Feel free to post other useful links you find 👍

@onbjerg
Copy link
Contributor

onbjerg commented Jan 20, 2017

I extracted the example from my previous comment and put it into a repository 🌟

@vuchl
Copy link

vuchl commented Jan 26, 2017

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 !

@littleStudent
Copy link

I created a simple starter repo for a REST api + Authentication. Its based on @onbjerg`s comment.
So thanks for that 👍.

@vuchl
Copy link

vuchl commented Feb 3, 2017

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

@leo
Copy link
Contributor

leo commented Feb 4, 2017

🎈

@leo leo closed this as completed Feb 4, 2017
@timneutkens
Copy link
Member

If you have any other examples at all, please add them to the readme 👍

@andrewmclagan
Copy link

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:

  • routing requests to the correct endpoint
  • handling CORS

nothing else. simple.. later if you need more control migrate to something like Kong. Don't do it till you need it.

@onbjerg
Copy link
Contributor

onbjerg commented Feb 20, 2017

Other good options are LinkedIn's linkerd or Amalgam8.

@augustovictor
Copy link

I took @littleStudent as example (thanks btw) and created a simpler one

@augustovictor
Copy link

An example of middleware

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

No branches or pull requests