| import ( | import ( | ||||
| "io" | "io" | ||||
| "github.com/goanywhere/env" | |||||
| "github.com/goanywhere/rex" | "github.com/goanywhere/rex" | ||||
| ) | ) | ||||
| func main() { | func main() { | ||||
| // Override default 5000 port here. | // Override default 5000 port here. | ||||
| env.Set("PORT", 9394) | |||||
| rex.Env.Set("PORT", 9394) | |||||
| rex.Get("/", index) | rex.Get("/", index) | ||||
| rex.Run() | rex.Run() | ||||
| } | } |
| package rex | |||||
| import ( | |||||
| "flag" | |||||
| "runtime" | |||||
| ) | |||||
| var ( | |||||
| debug bool | |||||
| port int | |||||
| maxprocs int | |||||
| ) | |||||
| func configure() { | |||||
| flag.BoolVar(&debug, "debug", Env.Bool("DEBUG", true), "flag to toggle debug mode") | |||||
| flag.IntVar(&port, "port", Env.Int("PORT", 5000), "port to run the application server") | |||||
| flag.IntVar(&maxprocs, "maxprocs", Env.Int("MAXPROCS", runtime.NumCPU()), "maximum cpu processes to run the server") | |||||
| flag.Parse() | |||||
| } |
| package internal | |||||
| import ( | |||||
| "path" | |||||
| "github.com/goanywhere/env" | |||||
| ) | |||||
| type Env struct { | |||||
| Base string | |||||
| } | |||||
| func New(dotenv string) *Env { | |||||
| env.Load(dotenv) | |||||
| return &Env{Base: path.Base(dotenv)} | |||||
| } | |||||
| func (self *Env) Get(key string) (string, bool) { | |||||
| return env.Get(key) | |||||
| } | |||||
| func (self *Env) Set(key string, value interface{}) error { | |||||
| return env.Set(key, value) | |||||
| } | |||||
| func (self *Env) String(key string, fallback ...string) string { | |||||
| return env.String(key, fallback...) | |||||
| } | |||||
| func (self *Env) Strings(key string, fallback ...[]string) []string { | |||||
| return env.Strings(key, fallback...) | |||||
| } | |||||
| func (self *Env) Int(key string, fallback ...int) int { | |||||
| return env.Int(key, fallback...) | |||||
| } | |||||
| func (self *Env) Int64(key string, fallback ...int64) int64 { | |||||
| return env.Int64(key, fallback...) | |||||
| } | |||||
| func (self *Env) Uint(key string, fallback ...uint) uint { | |||||
| return env.Uint(key, fallback...) | |||||
| } | |||||
| func (self *Env) Uint64(key string, fallback ...uint64) uint64 { | |||||
| return env.Uint64(key, fallback...) | |||||
| } | |||||
| func (self *Env) Bool(key string, fallback ...bool) bool { | |||||
| return env.Bool(key, fallback...) | |||||
| } | |||||
| func (self *Env) Float(key string, fallback ...float64) float64 { | |||||
| return env.Float(key, fallback...) | |||||
| } | |||||
| func (self *Env) Map(spec interface{}) error { | |||||
| return env.Map(spec) | |||||
| } |
| package rex | |||||
| import ( | |||||
| "flag" | |||||
| "runtime" | |||||
| "sync" | |||||
| "github.com/goanywhere/env" | |||||
| ) | |||||
| var ( | |||||
| debug bool | |||||
| port int | |||||
| maxprocs int | |||||
| once sync.Once | |||||
| ) | |||||
| func configure() { | |||||
| once.Do(func() { | |||||
| flag.BoolVar(&debug, "debug", env.Bool("DEBUG", true), "flag to toggle debug mode") | |||||
| flag.IntVar(&port, "port", env.Int("PORT", 5000), "port to run the application server") | |||||
| flag.IntVar(&maxprocs, "maxprocs", env.Int("MAXPROCS", runtime.NumCPU()), "maximum cpu processes to run the server") | |||||
| flag.Parse() | |||||
| }) | |||||
| } |
| "net/http" | "net/http" | ||||
| "path/filepath" | "path/filepath" | ||||
| "github.com/goanywhere/env" | |||||
| "github.com/goanywhere/fs" | "github.com/goanywhere/fs" | ||||
| "github.com/goanywhere/rex/internal" | "github.com/goanywhere/rex/internal" | ||||
| . "github.com/goanywhere/rex/middleware" | . "github.com/goanywhere/rex/middleware" | ||||
| var ( | var ( | ||||
| Default = New() | Default = New() | ||||
| Env *internal.Env | |||||
| ) | ) | ||||
| // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"), | // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"), | ||||
| } | } | ||||
| func init() { | func init() { | ||||
| var root = fs.Getcd(2) | |||||
| env.Set(internal.ROOT, root) | |||||
| env.Load(filepath.Join(root, ".env")) | |||||
| var basedir = fs.Getcd(2) | |||||
| Env = internal.New(filepath.Join(basedir, ".env")) | |||||
| } | } |