~kotrunga/simple-rest-api

e6dc6910b5516706b36d699492338766af36e5c3 — coltonhurst 4 years ago
First commit with the api
3 files changed, 109 insertions(+), 0 deletions(-)

A .gitignore
A README.md
A main.go
A  => .gitignore +20 -0
@@ 1,20 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# OS X
.DS_Store

# Ignore files without extensions
*
!/**/
!*.*

A  => README.md +3 -0
@@ 1,3 @@
# simple-rest-api

A simple rest api in Golang. I'm learning Golang, so I thought [this](https://www.codementor.io/codehakase/building-a-restful-api-with-golang-a6yivzqdo) would be a good beginning tutorial to follow.

A  => main.go +86 -0
@@ 1,86 @@
/*
	Golang Project
	Followed: https://www.codementor.io/codehakase/building-a-restful-api-with-golang-a6yivzqdo
*/

package main

// packages we want to import
import (
	"encoding/json"
	"log"
	"net/http"
	"github.com/gorilla/mux"	// mux = HTTP request multiplexer
)

// data objects
type Person struct {
	ID				string 	 `json:"id,omitempty"`
	Firstname	string	 `json:"firstname,omitempty"`
	Lastname	string	 `json:"lastname,omitempty"`
	Address		*Address `json:"address,omitempty"`
}
type Address struct {
	City  string `json:"city,omitempty"`
	State string `json:"state,omitemtpy`
}

// array to hold our people
// fine for an example, but this wouldn't be realistic in production
var people []Person

// our main function
func main() {
	// create the router
	router := mux.NewRouter()

	// add people to people array
	people = append(people, Person{ID: "1", Firstname: "John", Lastname: "Doe", Address: &Address{City: "Norfolk", State: "Virginia"}})
	people = append(people, Person{ID: "2", Firstname: "David", Lastname: "Doe", Address: &Address{City: "Tampa", State: "Florida"}})
	people = append(people, Person{ID: "3", Firstname: "Eduardo", Lastname: "Mardini"})

	// create the routes
	router.HandleFunc("/people", GetPeople).Methods("GET")
	router.HandleFunc("/people/{id}", GetPerson).Methods("GET")
	router.HandleFunc("/people/{id}", CreatePerson).Methods("POST")
	router.HandleFunc("/people/{id}", DeletePerson).Methods("DELETE")

	// start listening
	log.Fatal(http.ListenAndServe(":8000", router))
}

/* -------------------- ROUTES -------------------- */

func GetPeople(w http.ResponseWriter, r *http.Request) {
	json.NewEncoder(w).Encode(people)	// return the people array
}

func GetPerson(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	for _, item := range people {			// loop through the mapped names to check if the id param sent match any in the Person struct
		if item.ID == params["id"] {
      json.NewEncoder(w).Encode(item)
      return
    }
	}
}

func CreatePerson(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	var person Person
	_ = json.NewDecoder(r.Body).Decode(&person)
	person.ID = params["id"]
	people = append(people, person)
	json.NewEncoder(w).Encode(people)
}

func DeletePerson(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	for index, item := range people {
		if item.ID == params["id"] {
			people = append(people[:index], people[index+1])
			break
		}
		json.NewEncoder(w).Encode(people)
	}
}
\ No newline at end of file