adds ability to set an env in the app context

This commit is contained in:
2018-01-04 07:32:15 +00:00
parent 95400f4e69
commit 2d05e42555
7 changed files with 26 additions and 11 deletions

View File

@@ -17,4 +17,5 @@ type Context interface {
Set(key string, value interface{}) Set(key string, value interface{})
Render(status int, rr render.Renderer) error Render(status int, rr render.Renderer) error
Redirect(status int, url string) error Redirect(status int, url string) error
Env() string
} }

View File

@@ -11,7 +11,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
func NewContext(r Route, res http.ResponseWriter, req *http.Request) *DefaultContext { func NewContext(r Route, res http.ResponseWriter, req *http.Request, env string) *DefaultContext {
data := make(map[string]interface{}) data := make(map[string]interface{})
data["path"] = r.Path data["path"] = r.Path
@@ -26,6 +26,7 @@ func NewContext(r Route, res http.ResponseWriter, req *http.Request) *DefaultCon
request: req, request: req,
params: params, params: params,
data: data, data: data,
env: env,
} }
} }
@@ -36,6 +37,7 @@ type DefaultContext struct {
params url.Values params url.Values
contentType string contentType string
data map[string]interface{} data map[string]interface{}
env string
} }
// Response returns the original Response for the request. // Response returns the original Response for the request.
@@ -111,3 +113,7 @@ func (d *DefaultContext) Redirect(status int, url string) error {
http.Redirect(d.Response(), d.Request(), url, status) http.Redirect(d.Response(), d.Request(), url, status)
return nil return nil
} }
func (d *DefaultContext) Env() string {
return d.env
}

View File

@@ -15,12 +15,17 @@ type DefaultRouter struct {
StaticRoutes []*StaticRoute StaticRoutes []*StaticRoute
prefix string prefix string
env string
} }
func (r *DefaultRouter) Prefix(path string) { func (r *DefaultRouter) Prefix(path string) {
r.prefix = path r.prefix = path
} }
func (r *DefaultRouter) SetEnv(env string) {
r.env = env
}
func (r *DefaultRouter) GET(path string, h Handler) { func (r *DefaultRouter) GET(path string, h Handler) {
r.addRoute(http.MethodGet, path, h) r.addRoute(http.MethodGet, path, h)
} }

View File

@@ -9,7 +9,7 @@ import (
func main() { func main() {
router := tuu.NewRouter() router := tuu.NewRouter()
router.SetEnv("dev")
router.GET("/home", func(ctx tuu.Context) error { router.GET("/home", func(ctx tuu.Context) error {
ctx.Set("template_data", "some value") ctx.Set("template_data", "some value")
@@ -26,7 +26,11 @@ func main() {
}) })
app := tuu.New(router) app := tuu.New(router)
if err := app.Serve(); err != nil { if err := app.Serve(tuu.Config{
IPAddr: "127.0.0.1",
Port: "8080",
Env: "dev",
}); err != nil {
panic(err) panic(err)
} }
} }

View File

@@ -1,6 +1,5 @@
# Tuu # Tuu
A minimal framework for rapid web development in golang. A minimal framework for rapid web development in golang.
### Things to do before alpha Documentation coming soon.
- [ ] Write some tests
- [ ] Create an example

View File

@@ -6,6 +6,8 @@ type Handler func(Context) error
type Router interface { type Router interface {
Prefix(path string) Prefix(path string)
SetEnv(env string)
GET(path string, h Handler) GET(path string, h Handler)
POST(path string, h Handler) POST(path string, h Handler)
Static(path string, root http.FileSystem) Static(path string, root http.FileSystem)

8
tuu.go
View File

@@ -14,6 +14,7 @@ import (
type Config struct { type Config struct {
IPAddr string IPAddr string
Port string Port string
Env string
} }
func New(r Router) *App { func New(r Router) *App {
@@ -25,7 +26,7 @@ type App struct {
} }
func (a *App) Serve(cfg Config) error { func (a *App) Serve(cfg Config) error {
log.Printf("http server running @ %s:%s", cfg.IPAddr, cfg.Port) log.Printf("http server running @ http://%s:%s", cfg.IPAddr, cfg.Port)
r := mux.NewRouter() r := mux.NewRouter()
@@ -60,8 +61,5 @@ func (a *App) Serve(cfg Config) error {
}() }()
// start the web server // start the web server
if err := server.ListenAndServe(); err != nil { return server.ListenAndServe()
return err
}
return nil
} }