Giter Club home page Giter Club logo

gowebapi's Introduction

GoWebApi

Go Report Card GoDoc

Basic Web API in Go

This project demonstrates how to structure and build an API using the Go language without a framework. The API is still a work-in-progress, but it's designed to be easy to troubleshoot and easy to modify. Everyone structures their API differently, but ultimately consistency is key. The more consistent your API is, the easier it will be for other people to interact with it.

To download, run the following command:

go get github.com/josephspurrier/gowebapi

If you are on Go 1.5, you need to set GOVENDOREXPERIMENT to 1. If you are on Go 1.4 or earlier, the code will not work because it uses the vendor folder.

Quick Start with MySQL

Start MySQL and import config/mysql.sql to create the database and tables.

Open config/config.json and edit the Database section so the connection information matches your MySQL instance.

Build and run from the root directory. Open your REST client to: http://localhost. You should see the welcome message and status 200.

To create a user, send a POST request to http://localhost/user with the following fields: first_name, last_name, email, and password.

Available Endpoints

The following endpoints are available:

* POST   http://localhost/users		 - Create a new user
* GET	 http://localhost/users/{id} - Retrieve a user by ID
* GET	 http://localhost/users 	 - Retrieve a list of all users
* PUT	 http://localhost/users/{id} - Update a user by ID
* DELETE http://localhost/users/{id} - Delete a user by ID
* DELETE http://localhost/users		 - Delete all users

Structure

The majority of the code is in the vendor/app folder. I made this decision originally on my GoWebApp project because there were a lot of users trying to use the code on their own, but had to change all the imports path for it to work properly. The only downside is godoc does not work with the vendor folder method. Luckily, all the code can be moved out of the vendor folder and then a quick find and replace will get it working again if you want.

The files that are probably of most interest to you are these:

Rules for Consistency

Rules for mapping HTTP methods to CRUD:

POST   - Create (add record into database)
GET    - Read (get record from the database)
PUT    - Update (edit record in the database)
DELETE - Delete (remove record from the database)

Rules for status codes:

* Create something - 201 (Created)
* Read something - 200 (OK)
* Update something - 200 (OK)
* Delete something - 200 (OK)
* Create but missing info - 400 (Bad Request)
* Any other error - 500 (Internal Server Error)

Rules for messages:

* 201 - item created
* 200 - item found; no items to find; items deleted; no items to delete; etc
* 400 - [field] is missing; [field] needs to be type: [type]
* 500 - an error occurred, please try again later (should also log error because it's a programming or server issue)

Goals for this project

Integrate security similar to Parse: http://blog.parse.com/learn/secure-your-app-one-class-at-a-time/

Code generation for the following:

  • Controllers with routes
  • Models
  • Endpoint tests
  • Swagger spec

My Other Projects

GoWebApp demonstrates how to build a website using the Go language without a framework. Much of the structure of this project comes from GoWebApp.

I'll use the apigen project for the code generation.

gowebapi's People

Contributors

josephspurrier 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.