Code Monkey home page Code Monkey logo

user-management-service's Introduction

user-management-service

Build

Simple HTTP server that manages user data. XML and JSON payload types are both accepted.

Functionalities are below:

  1. POST /users create new user given user email and name. It will return a created user with a unique system assigned user ID. Additionally, I added the constraint that emails are unique.
  2. GET /users/:userID get user data given a system assigned user ID. It will returns saved user data.
  3. PATCH /users/:userID update user data given a system assigned user ID. It will allow to change user name only.

As extra, I have added below behaviors to the API:

  • basic rate limiting of 30 req/min.
  • autogenerated X-Request-Id which is logged during every request and returned in headers.
  • returned 409 for all domain business errors. This is to reduce the number of HTTP status that a consumer has to handle. Also, 400 is returned when there is a validation error with the request. 500 is for unexpected or critical errors.

Dependencies

  • Fiber: web framework used, an express like for Go.
  • Air: for live reloading of the service during local development.
  • Testify: assertion library used.
  • go.uuid: for unique id generation.
  • validator: for validating using struct tags.

Usage

Run locally

# clone repo
git clone https://github.com/JordyBaylac/user-management-service.git -b main

# install dependencies
go get ./...

# build, test and run
go build -v ./...
go test -v
go run main.go

# make sure api is working
curl --request GET --url http://localhost:8080/health

## (optional) for live reloading

# install air
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

# run
air

# if error 'air: command not found', then add to PATH and run again
export PATH=$PATH:$(go env GOPATH)/bin

cURL commands

Create new user

curl --request POST \
  --url https://jbaylac-user-management-svc.herokuapp.com/users \
  --header 'Content-Type: application/xml' \
  --data '<?xml version="1.0" encoding="UTF-8"?>
<user>
  <name>Neymar</name>
  <email>[email protected]</email>
</user>'

Get user data

curl --request GET --url https://jbaylac-user-management-svc.herokuapp.com/users/:userID

Update user data

curl --request PATCH \
  --url https://jbaylac-user-management-svc.herokuapp.com/users/:userID \
  --header 'Content-Type: application/json' \
  --data '{
	"name": "Neymar da Silva Santos Junior"
}'

Architecture

This solution embraces decoupling and extensibility through inversion of control. There is a lot of room for improvement, but I think this architecture is easy to maintain, change and evolve with new requirements.

Project layout

  • api/ package is the interface of this service. It's where the http server is configured (routes, middlewares, etc)
  • user/ it is a domain specific package that contains business regarding the management of users.

Diagram

Architecture Diagram

TODO

  • pentest the api.
  • implement persistence storage with a real database.
  • implement a custom error handler.
  • improve error response model and add codes to business errros.
  • add swagger generation for the api.
  • add correlation id for distributed tracing.
  • add structured logging.
  • add metrics.
  • refactor project layout to a more conventional one.
  • authenticate calls, so that updating a user is only allowed to the creator.
  • integrate with a configuration as a service to get secrets and other configs per environment.
  • run performance tests, pprof and check race conditions.

user-management-service's People

Contributors

jordybaylac avatar

Watchers

James Cloos avatar  avatar

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.