renaming.

This commit is contained in:
Andrey Parhomenko 2024-05-19 22:48:06 +05:00
parent 1e988c99af
commit ccca48d237
20 changed files with 176 additions and 146 deletions

2
api.go
View file

@ -1,4 +1,4 @@
package bond package ss
import ( import (
) )

3
btest.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/sh
#
go build -o ./exe/ ./cmd/test ./cmd/hook

View file

@ -1,11 +1,11 @@
package main package main
import ( import (
"vultras.su/core/bond" "surdeus.su/core/ss"
//"vultras.su/core/bond/urlenc" //"surdeus.su/core/ss/urlenc"
//"vultras.su/core/bond/methods" //"surdeus.su/core/ss/methods"
"vultras.su/core/bond/statuses" "surdeus.su/core/ss/statuses"
"vultras.su/core/bond/jsons" "surdeus.su/core/ss/jsons"
"fmt" "fmt"
//"io" //"io"
//"net/url" //"net/url"
@ -40,12 +40,13 @@ type Account struct {
SubDomain string `json:"subdomain"` SubDomain string `json:"subdomain"`
} }
var root = bond.Root(bond.Path(). var root = ss.Root(ss.Path(
Def( nil,
).Case(
"hook", "hook",
/*bond.Method().Def( /*ss.Method().Def(
methods.Post,*/ methods.Post,*/
bond.Func(func(c *bond.Context){ ss.Func(func(c *ss.Context){
reciever := WebhookRequest{} reciever := WebhookRequest{}
c.Scan(&reciever) c.Scan(&reciever)
if c.ScanErr() != nil { if c.ScanErr() != nil {
@ -67,7 +68,7 @@ Def(
} }
fmt.Printf("unescapeBody: %q\n", unesc) fmt.Printf("unescapeBody: %q\n", unesc)
mp := map[string] any{} mp := map[string] any{}
err = bond.ParseStr(unesc, mp) err = ss.ParseStr(unesc, mp)
if err != nil { if err != nil {
fmt.Printf("err:%s\n", err) fmt.Printf("err:%s\n", err)
return return
@ -81,10 +82,10 @@ Def(
c.SetStatus(statuses.OK) c.SetStatus(statuses.OK)
}), }),
//), //),
).Def( ).Case(
"auth", "auth",
bond.Func(func(c *bond.Context){ ss.Func(func(c *ss.Context){
}) }),
)) ))
func main() { func main() {
@ -101,7 +102,7 @@ func main() {
panic(err) panic(err)
}*/ }*/
//fmt.Printf("%#v\n", reciever) //fmt.Printf("%#v\n", reciever)
srv := bond.Server{ srv := ss.Server{
Addr: ":15080", Addr: ":15080",
Handler: root, Handler: root,
} }

View file

@ -1,35 +1,14 @@
package main package main
import ( import (
"vultras.su/core/bond" "surdeus.su/core/ss"
"vultras.su/core/bond/methods"
"vultras.su/core/bond/contents"
"vultras.su/core/bond/statuses"
) )
import "surdeus.su/core/ss/methods"
import "surdeus.su/core/ss/contents"
import "surdeus.su/core/ss/statuses"
type GetNotesOptions struct {
Id int `json:"id"`
Name string `json:"name"`
}
var root = bond.Root(bond.Path(). var testHandler = ss.Func(func(c *ss.Context){
Case(
"",
bond.StaticFile("index.html"),
).Case(
"hello",
bond.Path().Case(
// Using the relative redirect to force us to the en.
"", bond.Redirect("en", statuses.Found),
).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.SetContentType(contents.Plain, contents.Utf8)
c.Printf( c.Printf(
"AbsPath: %q\n" + "AbsPath: %q\n" +
@ -48,67 +27,71 @@ Case(
c.Printf("\t\t%q\n", v) c.Printf("\t\t%q\n", v)
} }
} }
})), })
var helloHandler = ss.Path(
// Default fallback handler.
testHandler,
).Case( ).Case(
"google", bond.Redirect("https://google.com", statuses.Found), // Using the relative redirect to force us to the en.
"", ss.Redirect("en", statuses.Found),
).Case( ).Case(
"web", bond.StaticDir("./static"), "en", ss.Func(func(c *ss.Context) {
).Case( c.Printf("Hello, World!")
"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( ).Case(
"get-notes", "ru", ss.Func(func(c *ss.Context) {
bond.Method().Case( c.Printf("Привет, Мир!")
}),
)
type GetNotesOptions struct {
Id int `json:"id"`
Author string `json:"author"`
Title string `json:"title"`
Text string `json:"text"`
}
var apiHandler = ss.Path(
nil,
).Case(
"note",
ss.Method(
// The default handler.
nil,
).Case(
methods.Get, methods.Get,
bond.Func(func(c *bond.Context) { nil,
).Case(
methods.Post,
ss.Func(func(c *ss.Context) {
opts := GetNotesOptions{} opts := GetNotesOptions{}
c.Scan(&opts) c.Scan(&opts)
c.Printf("%v", 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() { var root = ss.Root(ss.Path(
c.Printf("\t%q:\n", k) testHandler,
for _, v := range vs { ).Case(
c.Printf("\t\t%q\n", v) // 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() { func main() {
srv := bond.Server{ srv := ss.Server{
Addr: ":10800", Addr: ":10800",
Handler: root, Handler: root,
} }

View file

@ -1,4 +1,4 @@
package bond package ss
import ( import (
"io" "io"
@ -7,8 +7,8 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"fmt" "fmt"
"vultras.su/core/bond/contents" "surdeus.su/core/ss/contents"
"vultras.su/core/bond/urlenc" "surdeus.su/core/ss/urlenc"
) )
type Context struct { type Context struct {

View file

@ -1,4 +1,4 @@
package bond package ss
type Decoder interface { type Decoder interface {
Decode(any) error Decode(any) error

View file

@ -1,4 +1,4 @@
package bond package ss
import ( import (
"errors" "errors"

3
go.mod
View file

@ -1,4 +1,3 @@
module vultras.su/core/bond module surdeus.su/core/ss
go 1.21.3 go 1.21.3

View file

@ -1,9 +1,9 @@
package bond package ss
import ( import (
"net/http" "net/http"
"vultras.su/core/bond/contents" "surdeus.su/core/ss/contents"
"vultras.su/core/bond/statuses" "surdeus.su/core/ss/statuses"
"vultras.su/core/bond/methods" "surdeus.su/core/ss/methods"
) )
type Request = http.Request type Request = http.Request

View file

@ -4,7 +4,7 @@
<p> <p>
This is the index page! This is the index page!
</p> </p>
<div><a href="hello/en">English Hello</a></div> <div><a href="./hello/en">English Hello</a></div>
<div><a href="hello/ru">Русский Привет</a></div> <div><a href="./hello/ru">Русский Привет</a></div>
<div><a href="web/">Static files directory</a></div> <div><a href="./web/">Static files directory</a></div>
</body></html> </body></html>

56
jsons/time.go Normal file
View file

@ -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
}*/

View file

@ -1,4 +1,4 @@
package bond package ss
// The type implements method routing. // The type implements method routing.
type MethodRouter struct { type MethodRouter struct {
@ -7,9 +7,10 @@ type MethodRouter struct {
} }
// Returns new empty MethodRouter. // Returns new empty MethodRouter.
func Method() *MethodRouter { func Method(def Handler) *MethodRouter {
ret := &MethodRouter{} ret := &MethodRouter{}
ret.methodMap = make(map[ReqMethod]Handler) ret.methodMap = make(map[ReqMethod]Handler)
ret.def = def
return ret return ret
} }

View file

@ -1,2 +1,2 @@
package bond package ss

13
path.go
View file

@ -1,4 +1,4 @@
package bond package ss
import ( import (
"strings" "strings"
@ -12,10 +12,12 @@ type PathRouter struct {
def Handler def Handler
} }
// Returns new empty PathRouter. // Returns new empty PathRouter with the def
func Path() *PathRouter { // specified as the default fallback.
func Path(def Handler) *PathRouter {
ret := &PathRouter{} ret := &PathRouter{}
ret.pathMap = map[string] Handler{} ret.pathMap = map[string] Handler{}
ret.def = def
return ret return ret
} }
@ -68,9 +70,4 @@ func (router *PathRouter) Handle(c *Context) {
handler.Handle(c) 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
}

2
re.go
View file

@ -1,4 +1,4 @@
package bond package ss
import ( import (
//"regexp" //"regexp"

View file

@ -1,4 +1,4 @@
package bond package ss
// The type implements the entry point // The type implements the entry point
// for all the request for the server. // for all the request for the server.

View file

@ -1,3 +1,3 @@
package bond package ss

View file

@ -1,4 +1,4 @@
package bond package ss
// Returns the handler that redirects to the specified URL (u) // Returns the handler that redirects to the specified URL (u)
func Redirect(u string, status Status) Handler { func Redirect(u string, status Status) Handler {

View file

@ -1,4 +1,4 @@
package bond package ss
import ( import (
"net/http" "net/http"

View file

@ -1,10 +0,0 @@
version: 3
tasks:
btest:
cmds:
- go build ./cmd/test/
bhook:
cmds:
- go build ./cmd/hook/