Skip to content

Instantly share code, notes, and snippets.

View Duncanian's full-sized avatar
🏠
Working from home

Duncanian

🏠
Working from home
View GitHub Profile
@Duncanian
Duncanian / main.go
Last active June 6, 2023 20:00
The full main.go file for go-rest-api
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
@Duncanian
Duncanian / main.go
Last active May 31, 2019 16:09
All routes for go-rest-api
func main() {
initEvents()
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", homeLink)
router.HandleFunc("/event", createEvent).Methods("POST")
router.HandleFunc("/events", getAllEvents).Methods("GET")
router.HandleFunc("/events/{id}", getOneEvent).Methods("GET")
router.HandleFunc("/events/{id}", updateEvent).Methods("PATCH")
router.HandleFunc("/events/{id}", deleteEvent).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080", router))
@Duncanian
Duncanian / main.go
Last active June 3, 2019 22:40
Handling the endpoint for updating an event in go-rest-api
func updateEvent(w http.ResponseWriter, r *http.Request) {
eventID := mux.Vars(r)["id"]
var updatedEvent event
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update")
}
json.Unmarshal(reqBody, &updatedEvent)
@Duncanian
Duncanian / main.go
Last active May 31, 2019 15:48
Handling the endpoint for deleting an event in go-rest-api
func deleteEvent(w http.ResponseWriter, r *http.Request) {
eventID := mux.Vars(r)["id"]
for i, singleEvent := range events {
if singleEvent.ID == eventID {
events = append(events[:i], events[i+1:]...)
fmt.Fprintf(w, "The event with ID %v has been deleted successfully", eventID)
}
}
}
@Duncanian
Duncanian / main.go
Last active May 31, 2019 11:39
Handling the endpoint for getting all events in go-rest-api
func getAllEvents(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(events)
}
@Duncanian
Duncanian / main.go
Created May 31, 2019 11:33
Handling the endpoint for getting a single event in go-rest-api
func getOneEvent(w http.ResponseWriter, r *http.Request) {
eventID := mux.Vars(r)["id"]
for _, singleEvent := range events {
if singleEvent.ID == eventID {
json.NewEncoder(w).Encode(singleEvent)
}
}
}
@Duncanian
Duncanian / main.go
Last active June 3, 2019 22:40
Handling the endpoint for creating a new event in go-rest-api
func createEvent(w http.ResponseWriter, r *http.Request) {
var newEvent event
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update")
}
json.Unmarshal(reqBody, &newEvent)
events = append(events, newEvent)
w.WriteHeader(http.StatusCreated)
@Duncanian
Duncanian / main.go
Last active September 24, 2019 09:51
This is the dummy go-rest-api database
type event struct {
ID string `json:"ID"`
Title string `json:"Title"`
Description string `json:"Description"`
}
type allEvents []event
var events = allEvents{
{
@Duncanian
Duncanian / main.go
Created May 30, 2019 17:59
This the code for the go-rest-api (The home endpoint)
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
@Duncanian
Duncanian / main.go
Created May 28, 2019 12:12
This is the entry point of our go-rest-api
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}