| ##Roadmap for v1.0 | ##Roadmap for v1.0 | ||||
| - [X] Env-Based Configurations | - [X] Env-Based Configurations | ||||
| - [X] CLI Apps Integrations | - [X] CLI Apps Integrations | ||||
| - [X] Performance Boost | - [X] Performance Boost | ||||
| - [X] Live Reload Integration | - [X] Live Reload Integration | ||||
| - [X] Common Middleware Modules | - [X] Common Middleware Modules | ||||
| - [X] Continuous Integration | - [X] Continuous Integration | ||||
| - [ ] Full Test Converage | |||||
| - [ ] Improved Template Rendering | |||||
| - [X] Full Test Converage | |||||
| - [ ] Unified Rendering | - [ ] Unified Rendering | ||||
| - [ ] Project Wiki | - [ ] Project Wiki | ||||
| - [ ] Stable API | - [ ] Stable API |
| package rex | package rex | ||||
| type options struct { | |||||
| import ( | |||||
| "flag" | |||||
| "runtime" | |||||
| "sync" | |||||
| "github.com/goanywhere/env" | |||||
| ) | |||||
| var ( | |||||
| debug bool | debug bool | ||||
| port int | port int | ||||
| maxprocs 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() | |||||
| }) | |||||
| } | } |
| package rex | package rex | ||||
| import ( | import ( | ||||
| "flag" | |||||
| "net/http" | "net/http" | ||||
| "path/filepath" | "path/filepath" | ||||
| "runtime" | |||||
| "github.com/goanywhere/env" | "github.com/goanywhere/env" | ||||
| "github.com/goanywhere/fs" | "github.com/goanywhere/fs" | ||||
| "github.com/goanywhere/rex/internal" | "github.com/goanywhere/rex/internal" | ||||
| mw "github.com/goanywhere/rex/middleware" | |||||
| . "github.com/goanywhere/rex/middleware" | |||||
| ) | ) | ||||
| var ( | var ( | ||||
| DefaultMux = New() | DefaultMux = New() | ||||
| config = &options{ | |||||
| debug: true, | |||||
| port: 5000, | |||||
| maxprocs: runtime.NumCPU(), | |||||
| } | |||||
| ) | ) | ||||
| // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"), | // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"), | ||||
| } | } | ||||
| func Run() { | func Run() { | ||||
| DefaultMux.Use(mw.Logger) | |||||
| DefaultMux.Use(Logger) | |||||
| DefaultMux.Run() | DefaultMux.Run() | ||||
| } | } | ||||
| func init() { | func init() { | ||||
| // setup project root | |||||
| var root = fs.Getcd(2) | var root = fs.Getcd(2) | ||||
| env.Set(internal.ROOT, root) | env.Set(internal.ROOT, root) | ||||
| env.Load(filepath.Join(root, ".env")) | 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() | |||||
| } | } |
| // Run starts the application server to serve incoming requests at the given address. | // Run starts the application server to serve incoming requests at the given address. | ||||
| func (self *Router) Run() { | func (self *Router) Run() { | ||||
| runtime.GOMAXPROCS(config.maxprocs) | |||||
| configure() | |||||
| runtime.GOMAXPROCS(maxprocs) | |||||
| go func() { | go func() { | ||||
| time.Sleep(500 * time.Millisecond) | 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) | Fatalf("Failed to start the server: %v", err) | ||||
| } | } | ||||
| } | } |