Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/monkrus/go-rest-api

Simple server to handle HTTP requests
https://github.com/monkrus/go-rest-api

api golang http-requests restapi server

Last synced: 17 days ago
JSON representation

Simple server to handle HTTP requests

Awesome Lists containing this project

README

        

GoLang champion

# REST API
We are creating a simple server to handle HTTP requests which helps us to provide communication between separate software components.

## Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to start the project on a live system.

### Prerequisites
What things you need to install the software and how to install them
```
install GoLang
```

### Features
> Three distinct functions:

- HomePage function handles all requests to our root URL.
- HandleRequests function matches the URL path hit with a defined function.
- Main function kicks off the API (entry point)

## Running

- We run `go run main.go` to test the basic functionality of those 3 functions. (Result: "Homepage Endpoint Hit" message )
- Create an API that returns a JSON response
- Define an Article struct, set an Article array
- Register our function

# Example
- `$ go run main.go`
- Return a JSON response under `http://localhost:8000/articles`. Success !

# Example (Optional)
![Refactor](recordgif.gif)
- Introducing gorilla/mux
- Add lines to handleRequests func
- Test Gorilla/mux,`$ go run main.go`
```go
myRouter.HandleFunc("/articles", allArticles).Methods("GET")
myRouter.HandleFunc("/articles", testPostArticles).Methods("POST")
```
- Run `$ go run main.go` and compare following lines in Postman