Init.
This commit is contained in:
commit
e26dee007f
8 changed files with 146 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
*.exe
|
||||
exe
|
||||
*.vim
|
||||
test
|
37
cmd/test/main.go
Normal file
37
cmd/test/main.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
9
errors.go
Normal file
9
errors.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package bond
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
DupDefErr = errors.New("duplicate route define")
|
||||
)
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module github.com/omnipunk/bond
|
||||
|
||||
go 1.21.3
|
25
handler.go
Normal file
25
handler.go
Normal file
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
5
readme.md
Normal file
5
readme.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# bond
|
||||
|
||||
Golang library to easily write backend and connect
|
||||
it to the frontend.
|
||||
|
56
router.go
Normal file
56
router.go
Normal file
|
@ -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)
|
||||
}
|
||||
|
7
taskfile.yml
Normal file
7
taskfile.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
version: 3
|
||||
|
||||
tasks:
|
||||
btest:
|
||||
cmds:
|
||||
- go build -o ./test ./cmd/test/
|
||||
|
Loading…
Reference in a new issue