From ccca48d23757cb1ef1a572968b9a38c822f92349 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 19 May 2024 22:48:06 +0500 Subject: [PATCH] renaming. --- api.go | 2 +- btest.sh | 3 + cmd/hook/main.go | 29 +++++---- cmd/test/main.go | 165 +++++++++++++++++++++-------------------------- context.go | 6 +- decoder.go | 2 +- errors.go | 2 +- go.mod | 3 +- http.go | 8 +-- index.html | 6 +- jsons/time.go | 56 ++++++++++++++++ method.go | 5 +- parse.go | 2 +- path.go | 13 ++-- re.go | 2 +- root.go | 2 +- scan.go | 2 +- short.go | 2 +- static.go | 2 +- taskfile.yml | 10 --- 20 files changed, 176 insertions(+), 146 deletions(-) create mode 100755 btest.sh create mode 100644 jsons/time.go delete mode 100644 taskfile.yml diff --git a/api.go b/api.go index 799c0ee..dbf0aaf 100644 --- a/api.go +++ b/api.go @@ -1,4 +1,4 @@ -package bond + package ss import ( ) diff --git a/btest.sh b/btest.sh new file mode 100755 index 0000000..f5e8192 --- /dev/null +++ b/btest.sh @@ -0,0 +1,3 @@ +#!/bin/sh +# +go build -o ./exe/ ./cmd/test ./cmd/hook diff --git a/cmd/hook/main.go b/cmd/hook/main.go index 5d01d7b..21c3289 100644 --- a/cmd/hook/main.go +++ b/cmd/hook/main.go @@ -1,11 +1,11 @@ package main import ( - "vultras.su/core/bond" - //"vultras.su/core/bond/urlenc" - //"vultras.su/core/bond/methods" - "vultras.su/core/bond/statuses" - "vultras.su/core/bond/jsons" + "surdeus.su/core/ss" + //"surdeus.su/core/ss/urlenc" + //"surdeus.su/core/ss/methods" + "surdeus.su/core/ss/statuses" + "surdeus.su/core/ss/jsons" "fmt" //"io" //"net/url" @@ -40,12 +40,13 @@ type Account struct { SubDomain string `json:"subdomain"` } -var root = bond.Root(bond.Path(). -Def( +var root = ss.Root(ss.Path( + nil, +).Case( "hook", - /*bond.Method().Def( + /*ss.Method().Def( methods.Post,*/ - bond.Func(func(c *bond.Context){ + ss.Func(func(c *ss.Context){ reciever := WebhookRequest{} c.Scan(&reciever) if c.ScanErr() != nil { @@ -67,7 +68,7 @@ Def( } fmt.Printf("unescapeBody: %q\n", unesc) mp := map[string] any{} - err = bond.ParseStr(unesc, mp) + err = ss.ParseStr(unesc, mp) if err != nil { fmt.Printf("err:%s\n", err) return @@ -81,10 +82,10 @@ Def( c.SetStatus(statuses.OK) }), //), -).Def( +).Case( "auth", - bond.Func(func(c *bond.Context){ - }) + ss.Func(func(c *ss.Context){ + }), )) func main() { @@ -101,7 +102,7 @@ func main() { panic(err) }*/ //fmt.Printf("%#v\n", reciever) - srv := bond.Server{ + srv := ss.Server{ Addr: ":15080", Handler: root, } diff --git a/cmd/test/main.go b/cmd/test/main.go index 160205d..35c988b 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -1,114 +1,97 @@ package main import ( - "vultras.su/core/bond" - "vultras.su/core/bond/methods" - "vultras.su/core/bond/contents" - "vultras.su/core/bond/statuses" + "surdeus.su/core/ss" +) +import "surdeus.su/core/ss/methods" +import "surdeus.su/core/ss/contents" +import "surdeus.su/core/ss/statuses" + + +var testHandler = ss.Func(func(c *ss.Context){ + c.SetContentType(contents.Plain, contents.Utf8) + c.Printf( + "AbsPath: %q\n" + + "Prefix: %q\n" + + "Path: %q\n"+ + "Content-Type: %q\n", + c.AbsPath(), + c.PathPrefix(), + c.Path(), + c.ContentType(), + ) + c.Printf("Query:\n") + for k, vs := range c.Query() { + c.Printf("\t%q:\n", k) + for _, v := range vs { + c.Printf("\t\t%q\n", v) + } + } +}) + +var helloHandler = ss.Path( + // Default fallback handler. + testHandler, +).Case( + // Using the relative redirect to force us to the en. + "", ss.Redirect("en", statuses.Found), +).Case( + "en", ss.Func(func(c *ss.Context) { + c.Printf("Hello, World!") + }), +).Case( + "ru", ss.Func(func(c *ss.Context) { + c.Printf("Привет, Мир!") + }), ) type GetNotesOptions struct { Id int `json:"id"` - Name string `json:"name"` + Author string `json:"author"` + Title string `json:"title"` + Text string `json:"text"` } -var root = bond.Root(bond.Path(). -Case( - "", - bond.StaticFile("index.html"), +var apiHandler = ss.Path( + nil, ).Case( - "hello", - bond.Path().Case( - // Using the relative redirect to force us to the en. - "", bond.Redirect("en", statuses.Found), + "note", + ss.Method( + // The default handler. + nil, ).Case( - "en", bond.Func(func(c *bond.Context) { - c.Printf("Hello, World!") - }), - ).Case( - "ru", bond.Func(func(c *bond.Context) { - c.Printf("Привет, Мир!") - }), - ).Default(bond.Func(func(c *bond.Context){ - c.SetContentType(contents.Plain, contents.Utf8) - c.Printf( - "AbsPath: %q\n" + - "Prefix: %q\n" + - "Path: %q\n"+ - "Content-Type: %q\n", - c.AbsPath(), - c.PathPrefix(), - c.Path(), - c.ContentType(), - ) - c.Printf("Query:\n") - for k, vs := range c.Query() { - c.Printf("\t%q:\n", k) - for _, v := range vs { - c.Printf("\t\t%q\n", v) - } - } - })), -).Case( - "google", bond.Redirect("https://google.com", statuses.Found), -).Case( - "web", bond.StaticDir("./static"), -).Case( - "test", bond.Func(func(c *bond.Context) { - c.SetContentType(contents.Plain) - c.Printf( - "AbsPath: %q\n" + - "Prefix: %q\n" + - "Path: %q\n"+ - "Content-Type: %q\n", - c.AbsPath(), - c.PathPrefix(), - c.Path(), - c.ContentType(), - ) - c.Printf("Query:\n") - for k, vs := range c.Query() { - c.Printf("\t%q:\n", k) - for _, v := range vs { - c.Printf("\t\t%q\n", v) - } - } - }), -).Case( - "get-notes", - bond.Method().Case( methods.Get, - bond.Func(func(c *bond.Context) { + nil, + ).Case( + methods.Post, + ss.Func(func(c *ss.Context) { opts := GetNotesOptions{} c.Scan(&opts) c.Printf("%v", opts) }), ), -).Default( - bond.Func(func(c *bond.Context){ - c.SetContentType(contents.Plain) - c.Printf( - "AbsPath: %q\n" + - "Prefix: %q\n" + - "Path: %q\n"+ - "Content-Type: %q\n", - c.AbsPath(), - c.PathPrefix(), - c.Path(), - c.ContentType(), - ) - c.Printf("Query:\n") - for k, vs := range c.Query() { - c.Printf("\t%q:\n", k) - for _, v := range vs { - c.Printf("\t\t%q\n", v) - } - } - }), +) + +var root = ss.Root(ss.Path( + testHandler, +).Case( + // Static file serving example. + "", + ss.StaticFile("index.html"), +).Case( + "hello", + helloHandler, +).Case( + "google", ss.Redirect("https://google.com", statuses.Found), +).Case( + "web", ss.StaticDir("./static"), +).Case( + "api", + apiHandler, )) func main() { - srv := bond.Server{ + srv := ss.Server{ Addr: ":10800", Handler: root, } diff --git a/context.go b/context.go index 4902e50..377f2ec 100644 --- a/context.go +++ b/context.go @@ -1,4 +1,4 @@ -package bond + package ss import ( "io" @@ -7,8 +7,8 @@ import ( "net/http" "net/url" "fmt" - "vultras.su/core/bond/contents" - "vultras.su/core/bond/urlenc" + "surdeus.su/core/ss/contents" + "surdeus.su/core/ss/urlenc" ) type Context struct { diff --git a/decoder.go b/decoder.go index 2e9b108..80e7bf1 100644 --- a/decoder.go +++ b/decoder.go @@ -1,4 +1,4 @@ -package bond + package ss type Decoder interface { Decode(any) error diff --git a/errors.go b/errors.go index 867506d..52bb1aa 100644 --- a/errors.go +++ b/errors.go @@ -1,4 +1,4 @@ -package bond + package ss import ( "errors" diff --git a/go.mod b/go.mod index aa148a6..b7a08ea 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,3 @@ -module vultras.su/core/bond +module surdeus.su/core/ss go 1.21.3 - diff --git a/http.go b/http.go index edc955a..6b39639 100644 --- a/http.go +++ b/http.go @@ -1,9 +1,9 @@ -package bond + package ss import ( "net/http" - "vultras.su/core/bond/contents" - "vultras.su/core/bond/statuses" - "vultras.su/core/bond/methods" + "surdeus.su/core/ss/contents" + "surdeus.su/core/ss/statuses" + "surdeus.su/core/ss/methods" ) type Request = http.Request diff --git a/index.html b/index.html index 5c911a6..38a5c5e 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@

This is the index page!

-
English Hello
-
Русский Привет
-
Static files directory
+
English Hello
+
Русский Привет
+
Static files directory
diff --git a/jsons/time.go b/jsons/time.go new file mode 100644 index 0000000..e64351e --- /dev/null +++ b/jsons/time.go @@ -0,0 +1,56 @@ +package jsons + +import ( + //"time" +) + +// Implementing the interrface provides +// way to easily decode time and type safely +// specify how to decode field. +/*type TimeDecoder interface { + DecodeTime([]byte) (time.Time, error) +} + +// Implementing the interface provides type safe +// way to specify how to encode and decode time. +type TimeEncoder interface { + EncodeTime(time.Time) ([]byte, error) +} + +// The type provides fast way to define +// parsable time variations. +// Like `t jsons.Time[jsons.]` +type Time[D TimeDecoder, E TimeEncoder] struct { + time.Time +} + +func (t *Time[D, E]) UnmarshalJSON(bts []byte) error { + var adec [1]D + dec := adec[0] + + err, ret := dec.DecodeTime(bts) + if err != nil { + return err + } + t.Time = ret + + return nil +} + +func (t *Time[D, E]) MarshalJSON() ([]byte, error) { + var aenc [1]E + enc := enc[0] + + bts, err := enc.EncodeTime(t.Time) + if err != nil { + return nil, err + } + + return bts, err +} + +type DatetimeLocal struct{} +func (dt DatetimeLocal) DecodeTime(bts []byte) (time.Time, error) { + return dt.Format("1986-01-28T11:38:00.010"), nil +}*/ + diff --git a/method.go b/method.go index bc4d4b2..7071101 100644 --- a/method.go +++ b/method.go @@ -1,4 +1,4 @@ -package bond + package ss // The type implements method routing. type MethodRouter struct { @@ -7,9 +7,10 @@ type MethodRouter struct { } // Returns new empty MethodRouter. -func Method() *MethodRouter { +func Method(def Handler) *MethodRouter { ret := &MethodRouter{} ret.methodMap = make(map[ReqMethod]Handler) + ret.def = def return ret } diff --git a/parse.go b/parse.go index cf9dc05..40306d6 100644 --- a/parse.go +++ b/parse.go @@ -1,2 +1,2 @@ -package bond + package ss diff --git a/path.go b/path.go index 34f8f86..f479207 100644 --- a/path.go +++ b/path.go @@ -1,4 +1,4 @@ -package bond + package ss import ( "strings" @@ -12,10 +12,12 @@ type PathRouter struct { def Handler } -// Returns new empty PathRouter. -func Path() *PathRouter { +// Returns new empty PathRouter with the def +// specified as the default fallback. +func Path(def Handler) *PathRouter { ret := &PathRouter{} ret.pathMap = map[string] Handler{} + ret.def = def return ret } @@ -68,9 +70,4 @@ func (router *PathRouter) Handle(c *Context) { handler.Handle(c) } -// The handler to fall to if no match for paths in the router. -func (router *PathRouter) Default(h Handler) *PathRouter { - router.def = h - return router -} diff --git a/re.go b/re.go index a4e719b..1384f79 100644 --- a/re.go +++ b/re.go @@ -1,4 +1,4 @@ -package bond + package ss import ( //"regexp" diff --git a/root.go b/root.go index fa3357c..7a0c51f 100644 --- a/root.go +++ b/root.go @@ -1,4 +1,4 @@ -package bond + package ss // The type implements the entry point // for all the request for the server. diff --git a/scan.go b/scan.go index 1b2f47c..e5c332c 100644 --- a/scan.go +++ b/scan.go @@ -1,3 +1,3 @@ -package bond + package ss diff --git a/short.go b/short.go index 9eda068..969fa60 100644 --- a/short.go +++ b/short.go @@ -1,4 +1,4 @@ -package bond + package ss // Returns the handler that redirects to the specified URL (u) func Redirect(u string, status Status) Handler { diff --git a/static.go b/static.go index f424d52..66b0c39 100644 --- a/static.go +++ b/static.go @@ -1,4 +1,4 @@ -package bond + package ss import ( "net/http" diff --git a/taskfile.yml b/taskfile.yml deleted file mode 100644 index 33ebad3..0000000 --- a/taskfile.yml +++ /dev/null @@ -1,10 +0,0 @@ -version: 3 - -tasks: - btest: - cmds: - - go build ./cmd/test/ - bhook: - cmds: - - go build ./cmd/hook/ -