Created
July 23, 2019 14:59
-
-
Save haidlir/c1bfae19608a2ca1471d275c14448686 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"net/http" | |
"log" | |
"strconv" | |
"github.com/gorilla/mux" | |
) | |
const ( | |
// ListeningPort is the API listerner port | |
ListeningPort = ":81" | |
) | |
type Response struct { | |
Title string | |
Detail string | |
} | |
func HelloWorld(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("hello world")) | |
} | |
func KelasArkademy(w http.ResponseWriter, r *http.Request) { | |
resp := Response{} | |
resp.Title = "Selamat Datang di Kelas Arkademy" | |
resp.Detail = "Kelas online Golang" | |
encodedResp, err := json.Marshal(resp) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
} | |
// Siswa adalah detail struktur Siswa | |
type Siswa struct { | |
ID int | |
Nama string | |
Kelas int | |
} | |
// SemuaSiswa merupakan kumpulan dari siswa | |
var SemuaSiswa []Siswa | |
var id int | |
// ReadAll mengembalikan semua siswa | |
func ReadAll(w http.ResponseWriter, r *http.Request) { | |
encodedResp, err := json.Marshal(SemuaSiswa) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
} | |
func getVarsID(r *http.Request) (id int, err error) { | |
vars := mux.Vars(r) | |
if val, ok := vars["id"]; ok { | |
convertedVal, err := strconv.Atoi(val) | |
if err != nil { | |
return id, err | |
} | |
id = convertedVal | |
} | |
return | |
} | |
// ReadSpecific mengembalikan semua siswa | |
func ReadSpecific(w http.ResponseWriter, r *http.Request) { | |
id, err := getVarsID(r) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
return | |
} | |
var siswaTertentu Siswa | |
// Searching ID siswa tertentu | |
for _, perSiswa := range SemuaSiswa { | |
if perSiswa.ID == id { | |
siswaTertentu = perSiswa | |
} | |
} | |
// Kalo siswa id tertentu tidak ditemukan | |
if siswaTertentu.ID == 0 { | |
resp := Response{} | |
resp.Title = "ID tidak ditemukan" | |
encodedResp, err := json.Marshal(resp) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
return | |
} | |
// Kalo ditemukan | |
encodedResp, err := json.Marshal(siswaTertentu) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
} | |
// Create menambah siswa baru | |
func Create(w http.ResponseWriter, r *http.Request) { | |
siswaBaru := Siswa{} | |
decoder := json.NewDecoder(r.Body) | |
err := decoder.Decode(&siswaBaru) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
id++ | |
siswaBaru.ID = id | |
SemuaSiswa = append(SemuaSiswa, siswaBaru) | |
resp := Response{} | |
resp.Title = "Input Siswa Berhasil" | |
encodedResp, err := json.Marshal(resp) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
} | |
// UpdateSpecific mengembalikan semua siswa | |
func UpdateSpecific(w http.ResponseWriter, r *http.Request) { | |
// Decode JSON Body | |
siswaUpdate := Siswa{} | |
decoder := json.NewDecoder(r.Body) | |
err := decoder.Decode(&siswaUpdate) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
// Temukan ID | |
id, err := getVarsID(r) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
return | |
} | |
var siswaTertentu Siswa | |
indexDitemukan := -1 | |
// Searching ID siswa tertentu | |
for i, perSiswa := range SemuaSiswa { | |
if perSiswa.ID == id { | |
siswaTertentu = perSiswa | |
indexDitemukan = i | |
} | |
} | |
// Kalo siswa id tertentu tidak ditemukan | |
if siswaTertentu.ID == 0 { | |
resp := Response{} | |
resp.Title = "ID tidak ditemukan" | |
encodedResp, err := json.Marshal(resp) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
return | |
} | |
// Kalo ditemukan | |
SemuaSiswa[indexDitemukan].Nama = siswaUpdate.Nama | |
SemuaSiswa[indexDitemukan].Kelas = siswaUpdate.Kelas | |
encodedResp, err := json.Marshal(siswaUpdate) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
} | |
// DeleteSpecific mengembalikan semua siswa | |
func DeleteSpecific(w http.ResponseWriter, r *http.Request) { | |
// Temukan ID | |
id, err := getVarsID(r) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
return | |
} | |
var siswaTertentu Siswa | |
indexDitemukan := -1 | |
// Searching ID siswa tertentu | |
for i, perSiswa := range SemuaSiswa { | |
if perSiswa.ID == id { | |
siswaTertentu = perSiswa | |
indexDitemukan = i | |
} | |
} | |
// Kalo siswa id tertentu tidak ditemukan | |
if siswaTertentu.ID == 0 { | |
resp := Response{} | |
resp.Title = "ID tidak ditemukan" | |
encodedResp, err := json.Marshal(resp) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
return | |
} | |
// Kalo ditemukan | |
SemuaSiswa = append(SemuaSiswa[:indexDitemukan], SemuaSiswa[indexDitemukan+1:]...) | |
resp := Response{} | |
resp.Title = "ID berhasil dihapus" | |
encodedResp, err := json.Marshal(resp) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
w.Write([]byte("")) | |
} | |
w.Write(encodedResp) | |
} | |
func main() { | |
// Inisiasi Siswa | |
SemuaSiswa = []Siswa{} | |
// Add Routing | |
r := mux.NewRouter() | |
r.HandleFunc("/api/siswa", ReadAll).Methods(http.MethodGet) // Read All | |
r.HandleFunc("/api/siswa/{id:[0-9]+}", ReadSpecific).Methods(http.MethodGet) // Read Specific | |
r.HandleFunc("/api/siswa", Create).Methods(http.MethodPost) // Create | |
r.HandleFunc("/api/siswa/{id:[0-9]+}", UpdateSpecific).Methods(http.MethodPut) // Update Specifc | |
r.HandleFunc("/api/siswa/{id:[0-9]+}", DeleteSpecific).Methods(http.MethodDelete) // Delete Specific | |
// Run Web Server | |
log.Printf("Starting http server at %v", ListeningPort) | |
err := http.ListenAndServe(ListeningPort, r) | |
if err != nil { | |
log.Fatalf("Unable to run http server: %v", err) | |
} | |
log.Println("Stopping API Service...") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment