ソースを参照

env options supports

tags/v0.9.0
jimzhan 9年前
コミット
0b146a01f2
4個のファイルの変更26行の追加25行の削除
  1. +1
    -3
      README.md
  2. +19
    -1
      options.go
  3. +2
    -18
      rex.go
  4. +4
    -3
      router.go

+ 1
- 3
README.md ファイルの表示

@@ -141,7 +141,6 @@ Positive! Rex is an internal/fundamental project at GoAnywhere. We developed it

##Roadmap for v1.0


- [X] Env-Based Configurations
- [X] CLI Apps Integrations
- [X] Performance Boost
@@ -149,8 +148,7 @@ Positive! Rex is an internal/fundamental project at GoAnywhere. We developed it
- [X] Live Reload Integration
- [X] Common Middleware Modules
- [X] Continuous Integration
- [ ] Full Test Converage
- [ ] Improved Template Rendering
- [X] Full Test Converage
- [ ] Unified Rendering
- [ ] Project Wiki
- [ ] Stable API

+ 19
- 1
options.go ファイルの表示

@@ -1,7 +1,25 @@
package rex

type options struct {
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()
})
}

+ 2
- 18
rex.go ファイルの表示

@@ -1,25 +1,17 @@
package rex

import (
"flag"
"net/http"
"path/filepath"
"runtime"

"github.com/goanywhere/env"
"github.com/goanywhere/fs"
"github.com/goanywhere/rex/internal"
mw "github.com/goanywhere/rex/middleware"
. "github.com/goanywhere/rex/middleware"
)

var (
DefaultMux = New()

config = &options{
debug: true,
port: 5000,
maxprocs: runtime.NumCPU(),
}
)

// Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
@@ -70,20 +62,12 @@ func Use(module func(http.Handler) http.Handler) {
}

func Run() {
DefaultMux.Use(mw.Logger)
DefaultMux.Use(Logger)
DefaultMux.Run()
}

func init() {
// setup project root
var root = fs.Getcd(2)
env.Set(internal.ROOT, root)
env.Load(filepath.Join(root, ".env"))

// cmd arguments
flag.BoolVar(&config.debug, "debug", config.debug, "flag to toggle debug mode")
flag.IntVar(&config.port, "port", config.port, "port to run the application server")
flag.IntVar(&config.maxprocs, "maxprocs", config.maxprocs, "maximum cpu processes to run the server")

flag.Parse()
}

+ 4
- 3
router.go ファイルの表示

@@ -130,14 +130,15 @@ func (self *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// Run starts the application server to serve incoming requests at the given address.
func (self *Router) Run() {
runtime.GOMAXPROCS(config.maxprocs)
configure()
runtime.GOMAXPROCS(maxprocs)

go func() {
time.Sleep(500 * time.Millisecond)
Infof("Application server is listening at %d", config.port)
Infof("Application server is listening at %d", port)
}()

if err := http.ListenAndServe(fmt.Sprintf(":%d", config.port), self); err != nil {
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), self); err != nil {
Fatalf("Failed to start the server: %v", err)
}
}

読み込み中…
キャンセル
保存