Add remaining (sans spawn, testbench)

This commit is contained in:
Sangeeth Sudheer 2024-05-05 00:02:38 +05:30
parent 750c23e26e
commit 2dbd7ac062
Signed by: x
GPG Key ID: F6D06ECE734C57D1
6 changed files with 94 additions and 2 deletions

26
exec/exec.go Normal file
View File

@ -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")
}

30
httpctx/httpctx.go Normal file
View File

@ -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)
}

10
main.go
View File

@ -47,7 +47,10 @@ import (
// "git.sangeeth.dev/gobyexample/env" // "git.sangeeth.dev/gobyexample/env"
// "git.sangeeth.dev/gobyexample/logging" // "git.sangeeth.dev/gobyexample/logging"
// "git.sangeeth.dev/gobyexample/httpclient" // "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() { func main() {
@ -97,5 +100,8 @@ func main() {
// env.Env() // env.Env()
// logging.Logging() // logging.Logging()
// httpclient.HttpClient() // httpclient.HttpClient()
httpserver.HttpServer() // httpserver.HttpServer()
// httpctx.HttpCtx()
// exec.Exec()
signals.Signals()
} }

30
signals/signals.go Normal file
View File

@ -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
}

0
spawn/spawn.go Normal file
View File

0
testbench/testbench.go Normal file
View File