Initial import
This commit is contained in:
26
.gitignore
vendored
Normal file
26
.gitignore
vendored
Normal 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
18
fs.go
Normal 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))
|
||||||
|
}
|
||||||
54
main.go
Normal file
54
main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user