Advertisement
Uhuuuyy

handler.go

May 23rd, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.61 KB | Source Code | 0 0
  1. package handler
  2.  
  3. import (
  4.     "database/sql"
  5.     "fmt"
  6.     "nama_npm_pert4/model" //sesuaikan dengan nama folder (case sensitive)
  7.     "net/http"
  8.     "strings"
  9. )
  10.  
  11. var username, password, host, namaDB, defaultDB string
  12. var db *sql.DB
  13. var err error
  14.  
  15. func init() {
  16.     username = "root" //Misal : CPC21
  17.     password = ""
  18.     host = "localhost"
  19.     namaDB = "db_npm_pert4" //Misal : db_13116429
  20.     defaultDB = "mysql"
  21. }
  22. func API(w http.ResponseWriter, r *http.Request) {
  23.     db, err = model.Connect(username, password, host, namaDB)
  24.     if err != nil {
  25.         return
  26.     }
  27.     defer db.Close()
  28.  
  29.     w.Header().Set("Content-Type", "text-html; charset=utf-8; application/json")
  30.     dataURL := strings.Split(fmt.Sprintf("%s", r.URL.Path), "/")
  31.     switch dataURL[2] {
  32.     case "mahasiswa":
  33.         switch r.Method {
  34.         case "GET":
  35.             HandlerMahasiswaGet(w, r)
  36.         case "POST":
  37.             HandlerMahasiswaPost(w, r)
  38.         case "PUT":
  39.             HandlerMahasiswaPut(w, r)
  40.         case "DELETE":
  41.             HandlerMahasiswaDelete(w, r)
  42.         default:
  43.             w.Write([]byte("method tidak ditemukan"))
  44.         }
  45.  
  46.     case "matkul":
  47.         switch r.Method {
  48.         case "GET":
  49.             HandlerMatkulGet(w, r)
  50.         case "POST":
  51.             HandlerMatkulPost(w, r)
  52.         case "PUT":
  53.             HandlerMatkulPut(w, r)
  54.         case "DELETE":
  55.             HandlerMatkulDelete(w, r)
  56.         default:
  57.             w.Write([]byte("method tidak ditemukan"))
  58.         }
  59.  
  60.     case "nilai":
  61.         switch r.Method {
  62.         case "GET":
  63.             HandlerNilaiGet(w, r)
  64.         case "POST":
  65.             HandlerNilaiPost(w, r)
  66.         case "PUT":
  67.             HandlerNilaiPut(w, r)
  68.         case "DELETE":
  69.             HandlerNilaiDelete(w, r)
  70.         default:
  71.             w.Write([]byte("method tidak ditemukan"))
  72.         }
  73.  
  74.     default:
  75.         w.Write([]byte("request tidak ditemukan"))
  76.     }
  77. }
  78.  
Tags: inter4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement