Initial import

This commit is contained in:
George Suntres
2026-03-29 11:37:56 -04:00
commit 783e21ea9d
4 changed files with 101 additions and 0 deletions

26
.gitignore vendored Normal file
View File

@@ -0,0 +1,26 @@
# Allowlisting gitignore template for GO projects prevents us
# from adding various unwanted local files, such as generated
# files, developer configurations or IDE-specific files etc.
#
# Recommended: Go.AllowList.gitignore
# Ignore everything
*
# But these files...
!.gitignore
!*.go
!go.sum
!go.mod
!README.md
!LICENSE
!Makefile
!*.sh
!*.md
# ...even if they are in subdirectories
!*/

18
fs.go Normal file
View File

@@ -0,0 +1,18 @@
package sys
import (
"runtime"
"path/filepath"
)
func Rootify(path string) string {
return filepath.Join(GetProjectRoot(), path)
}
func GetProjectRoot() string {
_, b, _, _ := runtime.Caller(0)
// b is the absolute path to this specific .go file
// 1st Dir() gets the folder containing the file
// 2nd Dir() gets the parent of that folder
return filepath.Dir(filepath.Dir(b))
}

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module git.gsuntres.com/gsuntres/sys
go 1.25.0

54
main.go Normal file
View File

@@ -0,0 +1,54 @@
package sys
import (
"os"
"os/signal"
"syscall"
"sync"
"log"
)
var onExitMutex sync.Mutex
var onExitHandlers int
func init() {
onExitHandlers = 0
}
type OnExitFunc func()
func OnExit(fn OnExitFunc) {
onExitMutex.Lock()
onExitHandlers++
onExitMutex.Unlock()
// shutdown hook
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
// ctx, cancel := context.WithTimeout(context.Background(), *wait)
// defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
log.Println("Calling on exit...")
// controller.Stop()
fn()
onExitMutex.Lock()
onExitHandlers--
onExitMutex.Unlock()
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
if onExitHandlers == 0 {
log.Println("no more exit handler, will exit")
os.Exit(0)
}
}()
}