Created
June 26, 2022 06:15
-
-
Save goodylili/8ab74c09c26fa3ee0ce85f0bc177c9f9 to your computer and use it in GitHub Desktop.
A CRUD API using the net/http package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"net/http" | |
) | |
type Person struct { | |
Name string `json:"name"` | |
Age int `json:"age"` | |
} | |
var store = make([]Person, 0) | |
func handleAddPerson(writer http.ResponseWriter, request *http.Request) { | |
writer.Header().Set("Content-Type", "application/json") | |
if request.Method == "POST" { | |
writer.WriteHeader(http.StatusOK) | |
var human Person | |
err := json.NewDecoder(request.Body).Decode(&human) | |
if err != nil { | |
log.Fatalln("There was an error decoding the request body into the struct") | |
} | |
store = append(store, human) | |
err = json.NewEncoder(writer).Encode(&human) | |
if err != nil { | |
log.Fatalln("There was an error encoding the initialized struct") | |
} | |
} else { | |
writer.WriteHeader(http.StatusBadRequest) | |
writer.Write([]byte("Bad Request")) | |
} | |
} | |
func UpdatePerson(writer http.ResponseWriter, request *http.Request) { | |
writer.Header().Set("Content-Type", "application/json") | |
if request.Method == "PUT" { | |
var human Person | |
err := json.NewDecoder(request.Body).Decode(&human) | |
if err != nil { | |
log.Fatalln("There was an error decoding the request body into the struct") | |
} | |
for index, structs := range store { | |
if structs.Name == human.Name { | |
store = append(store[:index], store[index+1:]...) | |
} | |
} | |
store = append(store, human) | |
err = json.NewEncoder(writer).Encode(&human) | |
if err != nil { | |
log.Fatalln("There was an error encoding the initialized struct") | |
} | |
} else { | |
writer.WriteHeader(http.StatusBadRequest) | |
writer.Write([]byte("Bad Request")) | |
} | |
} | |
func handleGetPerson(writer http.ResponseWriter, request *http.Request) { | |
writer.Header().Set("Content-Type", "application/json") | |
if request.Method == "GET" { | |
name := request.URL.Query()["name"][0] | |
for _, structs := range store { | |
if structs.Name == name { | |
err := json.NewEncoder(writer).Encode(&structs) | |
if err != nil { | |
log.Fatalln("There was an error encoding the initialized struct") | |
} | |
} | |
} | |
} else { | |
writer.WriteHeader(http.StatusBadRequest) | |
writer.Write([]byte("Bad Request")) | |
} | |
} | |
func handleDeletePerson(writer http.ResponseWriter, request *http.Request) { | |
writer.Header().Set("Content-Type", "application/json") | |
if request.Method == "DELETE" { | |
name := request.URL.Query()["name"][0] | |
indexChoice := 0 | |
for index, structs := range store { | |
if structs.Name == name { | |
indexChoice = index | |
} | |
} | |
store = append(store[:indexChoice], store[indexChoice+1:]...) | |
writer.Write([]byte("Deleted It")) | |
} else { | |
writer.WriteHeader(http.StatusBadRequest) | |
writer.Write([]byte("Bad Request")) | |
} | |
} | |
func routeEndpoints() { | |
http.HandleFunc("/api/v1/getName", handleGetPerson) | |
http.HandleFunc("/api/v1/updateName", UpdatePerson) | |
http.HandleFunc("/api/v1/addName", handleAddPerson) | |
http.HandleFunc("/api/v1/deleteName", handleDeletePerson) | |
err := http.ListenAndServe(":8080", nil) | |
if err != nil { | |
log.Fatalln("There's an error with the server:", err) | |
} | |
} | |
func main() { | |
routeEndpoints() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment