Skip to content

Instantly share code, notes, and snippets.

@andipyk
Created August 5, 2020 19:54
Show Gist options
  • Save andipyk/ff7b82fd0c538a422d7d44f682123a1a to your computer and use it in GitHub Desktop.
Save andipyk/ff7b82fd0c538a422d7d44f682123a1a to your computer and use it in GitHub Desktop.
rest-api go
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
)
//Role adalah model dari sushi API | blueprint
type Roll struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Ingredient string `json:"ingredient"`
}
// Init rolls var as a slice (untuk simpan data) | penyimpanan
var rools []Roll
func main() {
// Generate Mock Data
rools = append(rools,
Roll{
ID: "1",
Name: "Salmon",
Description: "apa saja",
Ingredient: "Salmon",
},
Roll{
ID: "2",
Name: "Udang",
Description: "apa saja",
Ingredient: "xxn",
},
)
// init Router
router := mux.NewRouter()
//Handle EndPoints/Routing
router.HandleFunc("/sushi", getRolls).Methods("GET")
router.HandleFunc("/sushi/{id}", getRoll).Methods("GET")
router.HandleFunc("/sushi", createRoll).Methods("POST")
router.HandleFunc("/sushi/{id}", updateRoll).Methods("POST")
router.HandleFunc("/sushi/{id}", deleteRoll).Methods("DELETE")
// untuk tau kapan fail koneksi
log.Fatal(http.ListenAndServe(":8080", router))
}
// delete single shusi
func deleteRoll(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
params := mux.Vars(request)
for i, item := range rools {
if item.ID == params["id"] {
rools = append(rools[:i], rools[i+1:]...)
break
}
}
}
// create single shusi
func createRoll(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
var newRoll Roll
_ = json.NewDecoder(request.Body).Decode(&newRoll)
// helper generate id
newRoll.ID = strconv.Itoa(len(rools)+1)
rools = append(rools, newRoll)
_ = json.NewEncoder(writer).Encode(newRoll)
}
// update single shusi
func updateRoll(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
params := mux.Vars(request)
for i, item := range rools {
if item.ID == params["id"] {
rools = append(rools[:i], rools[i+1:]...)
var newRoll Roll
_ = json.NewDecoder(request.Body).Decode(&newRoll)
// helper generate id
newRoll.ID = params["id"]
rools = append(rools, newRoll)
_ = json.NewEncoder(writer).Encode(newRoll)
return
}
}
}
// Show Single
func getRoll(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
params := mux.Vars(request)
for _, item := range rools {
if item.ID == params["id"] {
_ = json.NewEncoder(writer).Encode(item)
return
}
}
// kalau bisa pakai map untuk parkir, tapi bebas sih
}
// Show All sushi
func getRolls(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(writer).Encode(rools)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment