Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/monkrus/go-rest-api
- Owner: monkrus
- Created: 2020-03-23T23:50:25.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-24T15:32:39.000Z (almost 5 years ago)
- Last Synced: 2024-05-01T14:39:01.602Z (9 months ago)
- Topics: api, golang, http-requests, restapi, server
- Language: Go
- Homepage: https://monkrus.github.io/go_rest_api/
- Size: 5.95 MB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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