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{})
Render(status int, rr render.Renderer) error
Redirect(status int, url string) error
Env() string
}

View File

@@ -11,7 +11,7 @@ import (
"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["path"] = r.Path
@@ -26,6 +26,7 @@ func NewContext(r Route, res http.ResponseWriter, req *http.Request) *DefaultCon
request: req,
params: params,
data: data,
env: env,
}
}
@@ -36,6 +37,7 @@ type DefaultContext struct {
params url.Values
contentType string
data map[string]interface{}
env string
}
// 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)
return nil
}
func (d *DefaultContext) Env() string {
return d.env
}

View File

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

View File

@@ -9,7 +9,7 @@ import (
func main() {
router := tuu.NewRouter()
router.SetEnv("dev")
router.GET("/home", func(ctx tuu.Context) error {
ctx.Set("template_data", "some value")
@@ -26,7 +26,11 @@ func main() {
})
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)
}
}

View File

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

View File

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

8
tuu.go
View File

@@ -14,6 +14,7 @@ import (
type Config struct {
IPAddr string
Port string
Env string
}
func New(r Router) *App {
@@ -25,7 +26,7 @@ type App struct {
}
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()
@@ -60,8 +61,5 @@ func (a *App) Serve(cfg Config) error {
}()
// start the web server
if err := server.ListenAndServe(); err != nil {
return err
}
return nil
return server.ListenAndServe()
}