Code Monkey home page Code Monkey logo

contact-book-api's Introduction

Contacts Book Using Go

Installation

Build the source for the target machine; e.g.; for linux use: env GOOS=linux GOARCH=arm go build. If your build machine is similar to the target machine, you can just use go build instead.

The binary will be named contact-book-api.

First time setup (with nginx on CentOS)

  1. Move binary to /usr/share/contact-book-api/ and copy the config.json file to /usr/share/contact-book-api/config/ folder.
  2. Setup a reverse proxy in nginx to point to http://localhost:8888. This is the default port, use whatever port you set in config.json
  3. Remember to add proxy_set_header Authorization $http_authorization; in nginx config for this service so that the Authorization header is sent to the API for every request.
  4. Restart nginx service using systemctl restart nginx
  5. We will call this service with contactbookapi. To create a service configuration, copy the below config to /etc/systemd/system/contactbookapi.service
[Unit]
Description=Contacts Boook Service
ConditionPathExists=/usr/share/contact-book-api/contact-book-api
After=network.target

[Service]
Type=simple
User=<linux user>
Group=<linux group>
LimitNOFILE=1024

Restart=on-failure
RestartSec=10
startLimitIntervalSec=60

WorkingDirectory=/usr/share/contact-book-api/
ExecStart=/usr/share/contact-book-api/contact-book-api

# make sure log directory exists and owned by the user specified
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/log/contactbookapi
ExecStartPre=/bin/chown <linux user>:<linux user> /var/log/contactbookapi
ExecStartPre=/bin/chmod 755 /var/log/contactbookapi
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=contactbookapi

[Install]
WantedBy=multi-user.target
  1. Enable this service using systemctl enable contactbookapi
  2. We want the logs to be stored at /var/log/contactbookapi/contactbookapi.log, so make contactbookapi dir under /var/log
  3. For logging, create a file at /etc/rsyslog.d/30-contactbookapi.conf with the following contents:
if $programname == 'contactbookapi' or $syslogtag == 'contactbookapi' then /var/log/contactbookapi/contactbookapi.log
& stop
  1. Start contactbookapi by systemctl start contactbookapi.

You will start seeing the logs in the log file.

Endpoints

All the endpoints except the ping endpoint requires Basic Auth. Since the API uses email in place of username therefore, Authorization header will be Basic base64(email:password).

  1. GET /ping: PING test
  2. GET /v1/contacts/page/:page: Get contact details list by page number (each page contains 10 entities as of now)
  3. DELETE /v1/contact/:id: Delete a contact by contact ID
  4. POST /v1/contact: Add a contact. The body should be JSON with the following structure: { "first_name": "John", "last_name": "Doe", "email": "[email protected]" }
  5. PUT /v1/contact/:id: Edit a contact by contact ID (JSON request is same as POST).
  6. PUT /v1/contact: Edit a contact by email (JSON request is same as POST).
  7. GET /v1/contacts/search: Search for contacts. The URL should be in the following format: /v1/contacts/search?first_name=john&last_name=doe&[email protected]&page=1. At least one of the given parameters are required. The search gives preference in the following order: first_name > last_name > email. If only email param is passed, then full preference will be given to email field.

contact-book-api's People

Contributors

jayantsinha avatar

Watchers

 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.