From 2dbd7ac062538d007e707290f106f414c7e8ce16 Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Sun, 5 May 2024 00:02:38 +0530 Subject: [PATCH] Add remaining (sans spawn, testbench) --- exec/exec.go | 26 ++++++++++++++++++++++++++ httpctx/httpctx.go | 30 ++++++++++++++++++++++++++++++ main.go | 10 ++++++++-- signals/signals.go | 30 ++++++++++++++++++++++++++++++ spawn/spawn.go | 0 testbench/testbench.go | 0 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 exec/exec.go create mode 100644 httpctx/httpctx.go create mode 100644 signals/signals.go create mode 100644 spawn/spawn.go create mode 100644 testbench/testbench.go diff --git a/exec/exec.go b/exec/exec.go new file mode 100644 index 0000000..a2e7904 --- /dev/null +++ b/exec/exec.go @@ -0,0 +1,26 @@ +package exec + +import ( + "fmt" + "os" + "os/exec" + "syscall" +) + +func Exec() { + defer fmt.Println("defer println") + lsPath, err := exec.LookPath("ls") + if err != nil { + panic(err) + } + + envs := os.Environ() + + // Exec replaces currently running go program and doesn't seem to care for `defer`s either + execErr := syscall.Exec(lsPath, []string{"-l", "-a", "-h", "/"}, envs) + if execErr != nil { + panic(execErr) + } + + fmt.Println("Stopping the program") +} diff --git a/httpctx/httpctx.go b/httpctx/httpctx.go new file mode 100644 index 0000000..e6efd50 --- /dev/null +++ b/httpctx/httpctx.go @@ -0,0 +1,30 @@ +package httpctx + +import ( + "fmt" + "log" + "net/http" + "time" +) + +func helloHandler(w http.ResponseWriter, req *http.Request) { + log.Println("hello handler started") + ctx := req.Context() + defer log.Println("hello handler finishing") + + select { + case <-time.After(10 * time.Second): + fmt.Fprintln(w, "hello there!") + case <-ctx.Done(): + err := ctx.Err() + log.Println("hello error:", err.Error()) + internalError := http.StatusInternalServerError + http.Error(w, err.Error(), internalError) + } +} + +func HttpCtx() { + log.Println("Server listening on :8002") + http.HandleFunc("/hello", helloHandler) + http.ListenAndServe(":8002", nil) +} diff --git a/main.go b/main.go index 49f55f1..313ab85 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,10 @@ import ( // "git.sangeeth.dev/gobyexample/env" // "git.sangeeth.dev/gobyexample/logging" // "git.sangeeth.dev/gobyexample/httpclient" - "git.sangeeth.dev/gobyexample/httpserver" + // "git.sangeeth.dev/gobyexample/httpserver" + // "git.sangeeth.dev/gobyexample/httpctx" + // "git.sangeeth.dev/gobyexample/exec" + "git.sangeeth.dev/gobyexample/signals" ) func main() { @@ -97,5 +100,8 @@ func main() { // env.Env() // logging.Logging() // httpclient.HttpClient() - httpserver.HttpServer() + // httpserver.HttpServer() + // httpctx.HttpCtx() + // exec.Exec() + signals.Signals() } diff --git a/signals/signals.go b/signals/signals.go new file mode 100644 index 0000000..9af7685 --- /dev/null +++ b/signals/signals.go @@ -0,0 +1,30 @@ +package signals + +import ( + "fmt" + "os" + "os/signal" + "syscall" +) + +func Signals() { + // chan MUST be buffered + signals := make(chan os.Signal, 1) + + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + + done := make(chan bool, 1) + + go doWork(done, signals) + + fmt.Println("Awaiting signal") + <-done + fmt.Println("Received done, program exiting") +} + +func doWork(done chan<- bool, signals <-chan os.Signal) { + sig := <-signals + fmt.Println() + fmt.Println("Received signal:", sig) + done <- true +} diff --git a/spawn/spawn.go b/spawn/spawn.go new file mode 100644 index 0000000..e69de29 diff --git a/testbench/testbench.go b/testbench/testbench.go new file mode 100644 index 0000000..e69de29