Skip to content

Commit

Permalink
Add app env
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Nov 14, 2022
1 parent 705a977 commit 0008d3b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions appx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
Expand All @@ -21,6 +22,26 @@ func Uptime() time.Duration {
return time.Since(startTime)
}

var (
env string
envOnce sync.Once
)

// Env returns application environment set via SetEnv func.
func Env() string {
return env
}

// SetEnv for the application. Can be called once, all next calls panics.
// This function should be called at the start of the application.
func SetEnv(v string) {
if env != "" {
panic("appx: SetEnv cannot be called twice")
}

envOnce.Do(func() { env = v })
}

// DoOnSignal runs fn on every signal.
// Function is async, context is used to close underlying goroutine.
func DoOnSignal(ctx context.Context, signal os.Signal, fn func(ctx context.Context)) {
Expand Down

0 comments on commit 0008d3b

Please sign in to comment.