浏览代码

add rex.Env for environment based settings

tags/v0.9.0
jimzhan 9 年前
父节点
当前提交
5f12856c0b
共有 5 个文件被更改,包括 83 次插入31 次删除
  1. +1
    -2
      README.md
  2. +19
    -0
      config.go
  3. +60
    -0
      internal/env.go
  4. +0
    -25
      options.go
  5. +3
    -4
      rex.go

+ 1
- 2
README.md 查看文件

@@ -68,7 +68,6 @@ package main
import (
"io"

"github.com/goanywhere/env"
"github.com/goanywhere/rex"
)

@@ -78,7 +77,7 @@ func index(w http.ResponseWriter, r *http.Request) {

func main() {
// Override default 5000 port here.
env.Set("PORT", 9394)
rex.Env.Set("PORT", 9394)
rex.Get("/", index)
rex.Run()
}

+ 19
- 0
config.go 查看文件

@@ -0,0 +1,19 @@
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()
}

+ 60
- 0
internal/env.go 查看文件

@@ -0,0 +1,60 @@
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)
}

+ 0
- 25
options.go 查看文件

@@ -1,25 +0,0 @@
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()
})
}

+ 3
- 4
rex.go 查看文件

@@ -4,7 +4,6 @@ import (
"net/http"
"path/filepath"

"github.com/goanywhere/env"
"github.com/goanywhere/fs"
"github.com/goanywhere/rex/internal"
. "github.com/goanywhere/rex/middleware"
@@ -12,6 +11,7 @@ import (

var (
Default = New()
Env *internal.Env
)

// Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
@@ -73,7 +73,6 @@ func Run() {
}

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

正在加载...
取消
保存