From e26dee007f14bc9768daba7d855b2ded2b0014b3 Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 7 Dec 2023 12:29:57 +0300 Subject: [PATCH] Init. --- .gitignore | 4 ++++ cmd/test/main.go | 37 ++++++++++++++++++++++++++++++++ errors.go | 9 ++++++++ go.mod | 3 +++ handler.go | 25 +++++++++++++++++++++ readme.md | 5 +++++ router.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ taskfile.yml | 7 ++++++ 8 files changed, 146 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/test/main.go create mode 100644 errors.go create mode 100644 go.mod create mode 100644 handler.go create mode 100644 readme.md create mode 100644 router.go create mode 100644 taskfile.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a741b13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.exe +exe +*.vim +test diff --git a/cmd/test/main.go b/cmd/test/main.go new file mode 100644 index 0000000..785f1e8 --- /dev/null +++ b/cmd/test/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "github.com/omnipunk/bond" +) + +var root = bond.Mux(). +Def( + "", + bond.ContextFunc(func(c *bond.Context){ + c.W.Write([]byte("This is the index page")) + }), +).Def( + "hello", + bond.Mux().Def( + "en", + bond.ContextFunc(func(c *bond.Context){ + c.W.Write([]byte("Hello, World!")) + }), + ).Def( + "ru", + bond.ContextFunc(func(c *bond.Context){ + c.W.Write([]byte("Привет, Мир!")) + }), + ), +) + +func main() { + srv := bond.Server{ + Addr: ":10800", + Handler: root, + } + err := srv.ListenAndServe() + if err != nil { + panic(err) + } +} diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..e78a765 --- /dev/null +++ b/errors.go @@ -0,0 +1,9 @@ +package bond + +import ( + "errors" +) + +var ( + DupDefErr = errors.New("duplicate route define") +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..20a078e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/omnipunk/bond + +go 1.21.3 diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..1c54872 --- /dev/null +++ b/handler.go @@ -0,0 +1,25 @@ +package bond + +import ( + "net/http" +) + +type Request = http.Request +type ResponseWriter = http.ResponseWriter +type HandlerFunc = http.HandlerFunc +type Handler = http.Handler +type Server = http.Server + +type Context struct { + R *Request + W ResponseWriter +} + +type ContextFunc func(*Context) +func (fn ContextFunc) ServeHTTP(w ResponseWriter, r *Request) { + fn(&Context{ + R: r, + W: w, + }) +} + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..657ada6 --- /dev/null +++ b/readme.md @@ -0,0 +1,5 @@ +# bond + +Golang library to easily write backend and connect +it to the frontend. + diff --git a/router.go b/router.go new file mode 100644 index 0000000..2d2c7f9 --- /dev/null +++ b/router.go @@ -0,0 +1,56 @@ +package bond + +import ( + "net/http" + "strings" + "fmt" + "path" +) + +type Router struct { + pathMap map[string] Handler +} + +func Mux() *Router { + ret := &Router{} + ret.pathMap = map[string] Handler{} + return ret +} + +func (router *Router) Def(pth string, handler Handler) *Router { + _, dup := router.pathMap[pth] + if dup { + panic(DupDefErr) + } + router.pathMap[pth] = handler + return router +} + +func (router *Router) ServeHTTP(w ResponseWriter, r *Request) { + pth := r.URL.Path + pth = path.Clean(pth) + pths := strings.SplitN(pth, "/", 3) + + var name string + if len(pths) > 1 { + name = pths[1] + } + name, _ = strings.CutSuffix(name, "/") + + prefix := "/" + if pth != "/" { + prefix = path.Clean("/" + name) + } + + fmt.Printf("Path: %q\n", r.URL.Path) + fmt.Printf("%q %q %q %q\n", pth, prefix, pths, name) + handler, ok := router.pathMap[name] + if !ok { + http.NotFound(w, r) + return + } + + r.URL.Path = pth + http.StripPrefix(prefix, handler).ServeHTTP(w, r) +} + diff --git a/taskfile.yml b/taskfile.yml new file mode 100644 index 0000000..b6ca3f8 --- /dev/null +++ b/taskfile.yml @@ -0,0 +1,7 @@ +version: 3 + +tasks: + btest: + cmds: + - go build -o ./test ./cmd/test/ +