Skip to content

Commit c115701

Browse files
authored
chore: menambahkan penjelasan tentang net http package (#58)
* chore: menambahkan penjelasan tentang net http package Signed-off-by: slowy07 <[email protected]> * fix(SecSLL): error codacy Signed-off-by: slowy07 <[email protected]> * docs: update link Signed-off-by: slowy07 <[email protected]> --------- Signed-off-by: slowy07 <[email protected]>
1 parent 1c3a87d commit c115701

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

utilities/net_http/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## net/http
2+
3+
`net/http` adalah standard library bawaan Go yang digunakan untuk membangun aplikasi berbasis protokol HTTP. Tujuan antara lain:
4+
- Menangani request dari client seperti API atau browser
5+
- Mengirim request ke server lain
6+
- Middleware, routing dasar, header manipulation, cookie dan lain-lain.
7+
8+
Ada dua bagian utama dalam `net/http`:
9+
10+
1. Server Side
11+
Membuat server yang akan listen request dari client, yang terdiri dari
12+
- `http.Request` yang merepresentasikan request HTTP yang masuk
13+
- `http.ResponseWriter` yang nantinya akan digunakan dalam memberikan response ke client
14+
- `http.Handler` interface untuk menangani request dan response
15+
- `http.HandleFunc` untuk register handler yang berdasarkan dari path URL
16+
17+
2. Client Side
18+
Digunakan untuk melakukan request ke server lain (contoh: `GET/POS` ke `API` eksternal), dalam component client side terdapat
19+
- `http.Get(url string)`, `http.Post(...)`
20+
- `http.Client{}` digunakan untuk konfigurasi custom request (seperti request, headers, dan lain-lain)
21+
22+
## contoh implementasi
23+
24+
Fungsi `homeHandler` yang akan dipanggil saat user mengakses `/`. dan mencatat response ke client.
25+
```go
26+
func homeHandler(rw http.ResponseWriter, req *http.Request) {
27+
fmt.Fprintf(rw, "selamat datang di bellshade Golang !\n")
28+
}
29+
```
30+
31+
Fungsi `aboutHandler` yang akan dipanggil sama seperti `homeHandler` tetapi bedanya dia digunakan untuk page lain misalnya `/about`
32+
```go
33+
func aboutHandler(rw http.ResponseWriter, req *http.Request) {
34+
fmt.Printf(rw, "kita di page about - bellshade Golang\n")
35+
}
36+
```
37+
38+
Fungsi `loggingMiddleware` adalah fungsi wrapping handler lain dan akan record log setiap request-request yang akan masuk
39+
```go
40+
func loggingMiddleware(hf http.HandlerFunc) http.HandlerFunc {
41+
return func(rw http.ResponseWriter, req *http.Request) {
42+
fmt.Printf("request sudah diterima: %s %s\n", rw.Method, req.URL.Path)
43+
hf.ServeHTTP(rw, req)
44+
}
45+
}
46+
```
47+
48+
kita bisa mencoba dengan menggunakan fungsi `http.HandleFunc(...)` untuk menghubungkan URL path ke handler
49+
50+
```go
51+
http.HandleFunc("/", loggingMiddleware(homeHandler))
52+
http.HandleFunc("/about", loggingMiddleware(aboutHandler))
53+
54+
fmt.Println("server berjalan dengan http://localhost:8443")
55+
```
56+
ketika dijalankan output akan
57+
```
58+
server berjalan dengan http://localhost:8443
59+
```
60+
61+
untuk source code dari package ini bisa lihat [disini](netHttp.go)

utilities/net_http/netHttp.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package nethttp
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
// fungsi handler function untuk routing `/`
9+
func homeHandler(w http.ResponseWriter, r *http.Request) {
10+
fmt.Fprintf(w, "welcome di bellshade: home hadler\n")
11+
}
12+
13+
// fungsi handler function untuk routing `/about`
14+
func aboutHandler(w http.ResponseWriter, r *http.Request) {
15+
fmt.Fprintf(w, "about: bellshade golang \n")
16+
}
17+
18+
// middleware sederhana yang digunakan untuk logging
19+
// setiap request
20+
func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
21+
return func(w http.ResponseWriter, r *http.Request) {
22+
fmt.Printf("request diterima: %s %s\n", r.Method, r.URL.Path)
23+
next.ServeHTTP(w, r)
24+
}
25+
}
26+
27+
func main() {
28+
http.HandleFunc("/", loggingMiddleware(homeHandler))
29+
http.HandleFunc("/about", loggingMiddleware(aboutHandler))
30+
fmt.Println("server berjalan di http://localhost:8443")
31+
32+
err := http.ListenAndServeTLS(":8443", "cert.perm", "key.perm", nil)
33+
if err != nil {
34+
panic(err)
35+
}
36+
}

0 commit comments

Comments
 (0)