├── .gitignore ├── README.md └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | go-rest-api 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go-rest-api 2 | 3 | ## Prerequisites 4 | 5 | * Have Go installed. If not, check it out [here](https://golang.org/doc/install) 6 | 7 | * Also after installing, make sure you are working inside your GOPATH 8 | 9 | ### Description 10 | 11 | * We are creating a JSON API that will allow users to create, read, update and delete events -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "net/http" 9 | 10 | "github.com/gorilla/mux" 11 | ) 12 | 13 | type event struct { 14 | ID string `json:"ID"` 15 | Title string `json:"Title"` 16 | Description string `json:"Description"` 17 | } 18 | 19 | type allEvents []event 20 | 21 | var events = allEvents{ 22 | { 23 | ID: "1", 24 | Title: "Introduction to Golang", 25 | Description: "Come join us for a chance to learn how golang works and get to eventually try it out", 26 | }, 27 | } 28 | 29 | func homeLink(w http.ResponseWriter, r *http.Request) { 30 | fmt.Fprintf(w, "Welcome home!") 31 | } 32 | 33 | func createEvent(w http.ResponseWriter, r *http.Request) { 34 | var newEvent event 35 | // Convert r.Body into a readable formart 36 | reqBody, err := ioutil.ReadAll(r.Body) 37 | if err != nil { 38 | fmt.Fprintf(w, "Kindly enter data with the event id, title and description only in order to update") 39 | } 40 | 41 | json.Unmarshal(reqBody, &newEvent) 42 | 43 | // Add the newly created event to the array of events 44 | events = append(events, newEvent) 45 | 46 | // Return the 201 created status code 47 | w.WriteHeader(http.StatusCreated) 48 | // Return the newly created event 49 | json.NewEncoder(w).Encode(newEvent) 50 | } 51 | 52 | func getOneEvent(w http.ResponseWriter, r *http.Request) { 53 | // Get the ID from the url 54 | eventID := mux.Vars(r)["id"] 55 | 56 | // Get the details from an existing event 57 | // Use the blank identifier to avoid creating a value that will not be used 58 | for _, singleEvent := range events { 59 | if singleEvent.ID == eventID { 60 | json.NewEncoder(w).Encode(singleEvent) 61 | } 62 | } 63 | } 64 | 65 | func getAllEvents(w http.ResponseWriter, r *http.Request) { 66 | json.NewEncoder(w).Encode(events) 67 | } 68 | 69 | func updateEvent(w http.ResponseWriter, r *http.Request) { 70 | // Get the ID from the url 71 | eventID := mux.Vars(r)["id"] 72 | var updatedEvent event 73 | // Convert r.Body into a readable formart 74 | reqBody, err := ioutil.ReadAll(r.Body) 75 | if err != nil { 76 | fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update") 77 | } 78 | 79 | json.Unmarshal(reqBody, &updatedEvent) 80 | 81 | for i, singleEvent := range events { 82 | if singleEvent.ID == eventID { 83 | singleEvent.Title = updatedEvent.Title 84 | singleEvent.Description = updatedEvent.Description 85 | events[i] = singleEvent 86 | json.NewEncoder(w).Encode(singleEvent) 87 | } 88 | } 89 | } 90 | 91 | func deleteEvent(w http.ResponseWriter, r *http.Request) { 92 | // Get the ID from the url 93 | eventID := mux.Vars(r)["id"] 94 | 95 | // Get the details from an existing event 96 | // Use the blank identifier to avoid creating a value that will not be used 97 | for i, singleEvent := range events { 98 | if singleEvent.ID == eventID { 99 | events = append(events[:i], events[i+1:]...) 100 | fmt.Fprintf(w, "The event with ID %v has been deleted successfully", eventID) 101 | } 102 | } 103 | } 104 | 105 | func main() { 106 | router := mux.NewRouter().StrictSlash(true) 107 | router.HandleFunc("/", homeLink) 108 | router.HandleFunc("/event", createEvent).Methods("POST") 109 | router.HandleFunc("/events", getAllEvents).Methods("GET") 110 | router.HandleFunc("/events/{id}", getOneEvent).Methods("GET") 111 | router.HandleFunc("/events/{id}", updateEvent).Methods("PATCH") 112 | router.HandleFunc("/events/{id}", deleteEvent).Methods("DELETE") 113 | log.Fatal(http.ListenAndServe(":8080", router)) 114 | } 115 | --------------------------------------------------------------------------------