43 lines
819 B
Go
43 lines
819 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Println("Hi, ", r.Header)
|
|
w.Write([]byte("Hi there!"))
|
|
})
|
|
|
|
http_server := &http.Server{
|
|
Addr: ":8080",
|
|
Handler: nil,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
go func() {
|
|
log.Fatal(http_server.ListenAndServe())
|
|
}()
|
|
fmt.Println("Server started! Listening in on ", http_server.Addr)
|
|
time.Sleep(1 * time.Second)
|
|
|
|
resp, _ := http.Get("http://127.0.0.1:8080/foo")
|
|
body, _ := io.ReadAll(resp.Body)
|
|
fmt.Println(string(body))
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
<-sigs
|
|
fmt.Println("Server stopped!")
|
|
os.Exit(1)
|
|
}
|