From 59301c7622ae92608f4ba52b8bebacd84b80144e Mon Sep 17 00:00:00 2001 From: "luke.rodham" Date: Fri, 10 Nov 2017 10:52:11 +0000 Subject: [PATCH] SAVEPOINT --- .gitignore | 1 + Gopkg.lock | 291 +++++++++++++++++++++++++++++++++++++ Gopkg.toml | 38 +++++ context.go | 6 + default_context.go | 4 +- default_router.go | 14 +- default_router_test.go | 22 +++ examples/default_router.go | 30 ++++ readme.md | 6 + route.go | 5 + router.go | 9 +- tuu.go | 36 ++++- 12 files changed, 455 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 default_router_test.go create mode 100644 examples/default_router.go create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..959429d --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,291 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "dmitri.shuralyov.com/kebabcase" + packages = ["."] + revision = "bf160e40a7918fbe9dc3cc841a023d87242bd2eb" + +[[projects]] + name = "github.com/davecgh/go-spew" + packages = ["spew"] + revision = "346938d642f2ec3594ed81d874461961cd0faa76" + version = "v1.1.0" + +[[projects]] + branch = "master" + name = "github.com/daviddengcn/go-colortext" + packages = ["."] + revision = "805cee6e0d43c72ba1d4e3275965ff41e0da068a" + +[[projects]] + name = "github.com/fatih/color" + packages = ["."] + revision = "570b54cabe6b8eb0bc2dfce68d964677d63b5260" + version = "v1.5.0" + +[[projects]] + name = "github.com/fatih/structs" + packages = ["."] + revision = "a720dfa8df582c51dee1b36feabb906bde1588bd" + version = "v1.0" + +[[projects]] + name = "github.com/go-sql-driver/mysql" + packages = ["."] + revision = "a0583e0143b1624142adab07e0e97fe106d99561" + version = "v1.3" + +[[projects]] + name = "github.com/gobuffalo/buffalo" + packages = ["render"] + revision = "436dc23f762ecc9aef1c759db2e7c09193944414" + version = "v0.10.1" + +[[projects]] + name = "github.com/gobuffalo/envy" + packages = ["."] + revision = "e613c80275b86293880eddeb27417c9a7c670ff3" + version = "v1.3.0" + +[[projects]] + name = "github.com/gobuffalo/packr" + packages = ["."] + revision = "aa752c1e501ca862dbb5720ff7c58b9289e68683" + version = "v1.9.0" + +[[projects]] + name = "github.com/gobuffalo/plush" + packages = [".","ast","lexer","parser","token"] + revision = "7df6c6624e74b78e595d9500ab1acf810cdf7af1" + version = "v3.0.4" + +[[projects]] + name = "github.com/gobuffalo/tags" + packages = [".","form","form/bootstrap"] + revision = "6755de03970f68812cfb0cb1162a66e60bcc1106" + version = "v1.4.0" + +[[projects]] + name = "github.com/gorilla/context" + packages = ["."] + revision = "1ea25387ff6f684839d82767c1733ff4d4d15d0a" + version = "v1.1" + +[[projects]] + name = "github.com/gorilla/mux" + packages = ["."] + revision = "7f08801859139f86dfafd1c296e2cba9a80d292e" + version = "v1.6.0" + +[[projects]] + branch = "master" + name = "github.com/jmoiron/sqlx" + packages = [".","reflectx"] + revision = "3379e5993990b1f927fc8db926485e6f6becf2d2" + +[[projects]] + name = "github.com/joho/godotenv" + packages = ["."] + revision = "a79fa1e548e2c689c241d10173efd51e5d689d5b" + version = "v1.2.0" + +[[projects]] + branch = "master" + name = "github.com/lib/pq" + packages = [".","oid"] + revision = "b609790bd85edf8e9ab7e0f8912750a786177bcf" + +[[projects]] + branch = "master" + name = "github.com/markbates/going" + packages = ["defaults","randx","wait"] + revision = "0576708c56cea02331f864fe6e157ac7841923e4" + +[[projects]] + branch = "master" + name = "github.com/markbates/inflect" + packages = ["."] + revision = "b32533d044574b17faf5615eefb6c912e2099bd4" + +[[projects]] + name = "github.com/markbates/pop" + packages = [".","columns","fizz","fizz/translators"] + revision = "f74995fdffa4ed212d13f3ebe280a58393f14fb5" + version = "v3.40.1" + +[[projects]] + branch = "master" + name = "github.com/markbates/validate" + packages = [".","validators"] + revision = "cd8cc34c703420d6b084fbbfd5085e8e71c5b830" + +[[projects]] + branch = "master" + name = "github.com/mattn/anko" + packages = ["ast","builtins","builtins/encoding/json","builtins/errors","builtins/flag","builtins/fmt","builtins/github.com/daviddengcn/go-colortext","builtins/io","builtins/io/ioutil","builtins/math","builtins/math/big","builtins/math/rand","builtins/net","builtins/net/http","builtins/net/url","builtins/os","builtins/os/exec","builtins/os/signal","builtins/path","builtins/path/filepath","builtins/regexp","builtins/runtime","builtins/sort","builtins/strings","builtins/time","parser","vm"] + revision = "2835104a087c32ce3d02bccd870e70e982b17583" + +[[projects]] + name = "github.com/mattn/go-colorable" + packages = ["."] + revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072" + version = "v0.0.9" + +[[projects]] + name = "github.com/mattn/go-isatty" + packages = ["."] + revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39" + version = "v0.0.3" + +[[projects]] + name = "github.com/mattn/go-sqlite3" + packages = ["."] + revision = "5160b48509cf5c877bc22c11c373f8c7738cdb38" + version = "v1.3.0" + +[[projects]] + branch = "master" + name = "github.com/microcosm-cc/bluemonday" + packages = ["."] + revision = "68fecaef60268522d2ac3f0123cec9d3bcab7b6e" + +[[projects]] + branch = "master" + name = "github.com/mitchellh/go-homedir" + packages = ["."] + revision = "b8bc1bf767474819792c23f32d8286a45736f1c6" + +[[projects]] + name = "github.com/pkg/errors" + packages = ["."] + revision = "645ef00459ed84a119197bfb8d8205042c6df63d" + version = "v0.8.0" + +[[projects]] + name = "github.com/pmezard/go-difflib" + packages = ["difflib"] + revision = "792786c7400a136282c1664665ae0a8db921c6c2" + version = "v1.0.0" + +[[projects]] + branch = "master" + name = "github.com/russross/blackfriday" + packages = ["."] + revision = "6d1ef893fcb01b4f50cb6e57ed7df3e2e627b6b2" + +[[projects]] + name = "github.com/satori/go.uuid" + packages = ["."] + revision = "879c5887cd475cd7864858769793b2ceb0d44feb" + version = "v1.1.0" + +[[projects]] + branch = "master" + name = "github.com/serenize/snaker" + packages = ["."] + revision = "c7a77c38c398a5468cac9579c3b2b1de22fb4da6" + +[[projects]] + branch = "master" + name = "github.com/sergi/go-diff" + packages = ["diffmatchpatch"] + revision = "2fc9cd33b5f86077aa3e0f442fa0476a9fa9a1dc" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/github_flavored_markdown" + packages = ["."] + revision = "8071c74461cb0a1a6e8a677e868cbb79138510c9" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/go" + packages = ["parserutil","printerutil","reflectfind","reflectsource"] + revision = "c661e953e604ba4a84a3c4e458462a481bd6ce72" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/go-goon" + packages = ["."] + revision = "37c2f522c041b74919a9e5e3a6c5c47eb34730a5" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/graphql" + packages = ["ident"] + revision = "cf6db17b893acfad0ca1929ba6be45bf854790ed" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/highlight_diff" + packages = ["."] + revision = "09bb4053de1b1d872a9f25dc21378fa71dca4e4e" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/highlight_go" + packages = ["."] + revision = "78fb10f4a5f89e812a5e26ab723b954a51226086" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/octiconssvg" + packages = ["."] + revision = "8c9861b86a08c72d14e0285d0dc313bb6df52295" + +[[projects]] + branch = "master" + name = "github.com/shurcooL/sanitized_anchor_name" + packages = ["."] + revision = "86672fcb3f950f35f2e675df2240550f2a50762f" + +[[projects]] + branch = "master" + name = "github.com/sourcegraph/annotate" + packages = ["."] + revision = "f4cad6c6324d3f584e1743d8b3e0e017a5f3a636" + +[[projects]] + branch = "master" + name = "github.com/sourcegraph/syntaxhighlight" + packages = ["."] + revision = "bd320f5d308e1a3c4314c678d8227a0d72574ae7" + +[[projects]] + name = "github.com/stretchr/testify" + packages = ["assert","require"] + revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0" + version = "v1.1.4" + +[[projects]] + branch = "master" + name = "golang.org/x/net" + packages = ["context","html","html/atom"] + revision = "a337091b0525af65de94df2eb7e98bd9962dcbe2" + +[[projects]] + branch = "master" + name = "golang.org/x/sync" + packages = ["errgroup"] + revision = "fd80eb99c8f653c847d294a001bdf2a3a6f768f5" + +[[projects]] + branch = "master" + name = "golang.org/x/sys" + packages = ["unix"] + revision = "1e2299c37cc91a509f1b12369872d27be0ce98a6" + +[[projects]] + branch = "v2" + name = "gopkg.in/yaml.v2" + packages = ["."] + revision = "eb3733d160e74a9c7e442f435eb3bea458e1d19f" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "7fe7043406866580df73f74659708b2fdf217782ba07d20308ad056e03b93600" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..baba52f --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,38 @@ + +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" + + +[[constraint]] + name = "github.com/gobuffalo/buffalo" + version = "0.10.1" + +[[constraint]] + name = "github.com/gorilla/context" + version = "1.1.0" + +[[constraint]] + name = "github.com/gorilla/mux" + version = "1.6.0" + +[[constraint]] + name = "github.com/stretchr/testify" + version = "1.1.4" diff --git a/context.go b/context.go index eb41113..697f27a 100644 --- a/context.go +++ b/context.go @@ -3,10 +3,16 @@ package tuu import ( "context" "net/http" + "net/url" + "github.com/gobuffalo/buffalo/render" ) type Context interface { context.Context Response() http.ResponseWriter Request() *http.Request + Params() url.Values + Param(key string) string + Set(key string, value interface{}) + Render(status int, rr render.Renderer) error } diff --git a/default_context.go b/default_context.go index d97f908..0a002e0 100644 --- a/default_context.go +++ b/default_context.go @@ -5,8 +5,6 @@ import ( "net/http" "net/url" - "github.com/gobuffalo/plush" - "github.com/gobuffalo/buffalo/render" ) @@ -70,7 +68,7 @@ func (d *DefaultContext) Value(key interface{}) interface{} { } func (d *DefaultContext) Render(status int, rr render.Renderer) error { - p := plush.NewContext() + //p := plush.NewContext() /*if rr != nil { data := d.data diff --git a/default_router.go b/default_router.go index 1214cdb..7114aa8 100644 --- a/default_router.go +++ b/default_router.go @@ -7,7 +7,8 @@ func NewRouter() *DefaultRouter { } type DefaultRouter struct { - Routes []*Route + Routes []*Route + StaticRoutes []*StaticRoute } func (r *DefaultRouter) GET(path string, h Handler) { @@ -18,10 +19,21 @@ func (r *DefaultRouter) POST(path string, h Handler) { r.addRoute(http.MethodPost, path, h) } +func (r *DefaultRouter) Static(path string, root http.FileSystem) { + r.StaticRoutes = append(r.StaticRoutes, &StaticRoute{ + Path: path, + Handler: http.StripPrefix(path, http.FileServer(root)), + }) +} + func (r *DefaultRouter) GetRoutes() []*Route { return r.Routes } +func (r *DefaultRouter) GetStaticRoutes() []*StaticRoute { + return r.StaticRoutes +} + func (r *DefaultRouter) addRoute(m, p string, h Handler) { r.Routes = append(r.Routes, &Route{ Method: m, diff --git a/default_router_test.go b/default_router_test.go new file mode 100644 index 0000000..bbd3d8b --- /dev/null +++ b/default_router_test.go @@ -0,0 +1,22 @@ +package tuu_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "gitlab.com/lrodham/tuu" +) + +func Test_Route_Creation(t *testing.T) { + r := require.New(t) + + router := tuu.NewRouter() + router.GET("/testing", func(ctx tuu.Context) error { return nil }) + + + routes := router.GetRoutes() + r.Len(routes, 1) + route := routes[0] + r.Equal("GET", route.Method) + r.Equal("/testing", route.Path) +} diff --git a/examples/default_router.go b/examples/default_router.go new file mode 100644 index 0000000..4767b76 --- /dev/null +++ b/examples/default_router.go @@ -0,0 +1,30 @@ +package examples + +import ( + "log" + "gitlab.com/lrodham/tuu" +) + +func main() { + router := tuu.NewRouter() + + router.GET("/home", func(ctx tuu.Context) error { + ctx.Set("template_data", "some value") + + return ctx.Render(200, render.HTML("template_name.html")) + }) + + router.POST("/login", func(ctx tuu.Context) error { + username := ctx.Param("username") + password := ctx.Param("password") + + log.Println(username, password) + + return nil + }) + + app := tuu.New(router) + if err := app.Serve(); err != nil { + panic(err) + } +} \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5265548 --- /dev/null +++ b/readme.md @@ -0,0 +1,6 @@ +# Tuu +A minimal framework for rapid web development in golang. + +### Things to do before alpha +- [ ] Write some tests +- [ ] Create an example diff --git a/route.go b/route.go index 27aa462..95a0898 100644 --- a/route.go +++ b/route.go @@ -22,3 +22,8 @@ func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) { c.Response().Write([]byte(err.Error())) } } + +type StaticRoute struct { + Path string + Handler http.Handler +} diff --git a/router.go b/router.go index a835879..ab70a5b 100644 --- a/router.go +++ b/router.go @@ -1,9 +1,14 @@ package tuu +import "net/http" + type Handler func(Context) error type Router interface { - GET(path string, ctx Context) - POST(path string, ctx Context) + GET(path string, h Handler) + POST(path string, h Handler) + Static(path string, root http.FileSystem) + GetRoutes() []*Route + GetStaticRoutes() []*StaticRoute } diff --git a/tuu.go b/tuu.go index 31ffeb0..457613c 100644 --- a/tuu.go +++ b/tuu.go @@ -1,6 +1,9 @@ package tuu import ( + "log" + "net/http" + "github.com/gorilla/mux" ) @@ -12,11 +15,42 @@ type App struct { router Router } -func (a *App) Serve() { +func (a *App) Serve() error { r := mux.NewRouter() for _, route := range a.router.GetRoutes() { r.Handle(route.Path, route).Methods(route.Method) } + for _, route := range a.router.GetStaticRoutes() { + r.PathPrefix(route.Path).Handler(route.Handler) + } + + server := http.Server{ + Addr: "", + Handler: r, + } + + /*ctx, cancel := sigtx.WithCancel(a.Context, syscall.SIGTERM, os.Interrupt) + defer cancel() + + go func() { + // gracefully shut down the application when the context is cancelled + <-ctx.Done() + fmt.Println("Shutting down application") + + err := a.Stop(ctx.Err()) + if err != nil { + fmt.Println(err) + } + + err = server.Shutdown(ctx) + if err != nil { + fmt.Println(err) + } + + }()*/ + + // start the web server + return server.ListenAndServe() }