feat: added the statuses package.

This commit is contained in:
Andrey Parhomenko 2024-01-10 18:23:02 +03:00
parent 89be9a69be
commit 9209039b32
5 changed files with 192 additions and 5 deletions

2
api.go
View file

@ -69,6 +69,8 @@ func (c *Context) Scan(v any) bool {
switch typ {
case contents.Json:
c.dec = json.NewDecoder(c.R.Body)
//case contents.UrlEncoded:
// return false
default:
c.scanErr = UnknownContentTypeErr
return false

96
cmd/hook/main.go Normal file
View file

@ -0,0 +1,96 @@
package main
import (
"vultras.su/core/bond"
"vultras.su/core/bond/methods"
"vultras.su/core/bond/contents"
"fmt"
"io"
"net/url"
)
type GetNotesOptions struct {
Id int `json:"id"`
Name string `json:"name"`
}
var root = bond.Mux().
Def(
"",
bond.Func(func(c *bond.Context) {
c.W.Write([]byte("This is the index page"))
}),
).Def(
"hello",
bond.Mux().Def(
"en",
bond.Func(func(c *bond.Context) {
c.Printf("Hello, World!")
}),
).Def(
"ru",
bond.Func(func(c *bond.Context) {
c.Printf("Привет, Мир!")
}),
),
).Def(
"web",
bond.Static("./static"),
).Def(
"test",
bond.Func(func(c *bond.Context) {
c.SetContentType(contents.Plain)
c.Printf(
"Path: %q\n"+
"Content-Type: %q\n",
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)
}
}
}),
).Def(
"get-notes",
bond.Method().Def(
methods.Get,
bond.Func(func(c *bond.Context) {
opts := GetNotesOptions{}
c.Scan(&opts)
c.Printf("%v", opts)
}),
),
).Def(
"hook",
/*bond.Method().Def(
methods.Post,*/
bond.Func(func(c *bond.Context){
fmt.Printf("content-type: %q", c.ContentType())
body, err := io.ReadAll(c.R.Body)
if err != nil {
fmt.Printf("err:%s\n", err)
return
}
values, err := url.ParseQuery(string(body))
if err != nil {
fmt.Printf("err:%s\n", err)
return
}
fmt.Printf("values: %q", values)
}),
//),
)
func main() {
srv := bond.Server{
Addr: ":15080",
Handler: root,
}
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}

View file

@ -1,15 +1,25 @@
package contents
import (
)
type CharSet string
const (
Utf8 = "utf-8"
)
type Type string
const (
// Using the UTF-8 by default.
Unknown Type = "application/octet-stream"
Binary = Unknown
Binary
Plain Type = "text/plain; charset=utf-8"
Css Type = "text/css"
Html Type = "text/html"
Json Type = "application/json"
UrlEncoded = "application/x-www-form-urlencoded"
)
func (t Type) CharSet(set CharSet) Type {
return t + ";" + Type(set)
}

76
statuses/main.go Normal file
View file

@ -0,0 +1,76 @@
package statuses
import (
"net/http"
)
type Status int
const (
Continue Status = http.StatusContinue
SwitchingProtocols = http.StatusSwitchingProtocols // RFC 9110, 15.2.2
Processing = http.StatusProcessing // RFC 2518, 10.1
EarlyHints = http.StatusEarlyHints // RFC 8297
OK = http.StatusOK // RFC 9110, 15.3.1
Created = http.StatusCreated // RFC 9110, 15.3.2
Accepted = http.StatusAccepted // RFC 9110, 15.3.3
NonAuthoritativeInfo = http.StatusNonAuthoritativeInfo // RFC 9110, 15.3.4
NoContent = http.StatusNoContent // RFC 9110, 15.3.5
ResetContent = http.StatusResetContent // RFC 9110, 15.3.6
PartialContent = http.StatusPartialContent // RFC 9110, 15.3.7
MultiStatus = http.StatusMultiStatus // RFC 4918, 11.1
AlreadyReported = http.StatusAlreadyReported // RFC 5842, 7.1
IMUsed = http.StatusIMUsed // RFC 3229, 10.4.1
MultipleChoices = http.StatusMultipleChoices // RFC 9110, 15.4.1
MovedPermanently = http.StatusMovedPermanently // RFC 9110, 15.4.2
Found = http.StatusFound // RFC 9110, 15.4.3
SeeOther = http.StatusSeeOther // RFC 9110, 15.4.4
NotModified = http.StatusNotModified // RFC 9110, 15.4.5
UseProxy = http.StatusUseProxy // RFC 9110, 15.4.6
TemporaryRedirect = http.StatusTemporaryRedirect // RFC 9110, 15.4.8
PermanentRedirect = http.StatusPermanentRedirect // RFC 9110, 15.4.9
BadRequest = http.StatusBadRequest // RFC 9110, 15.5.1
Unauthorized = http.StatusUnauthorized // RFC 9110, 15.5.2
PaymentRequired = http.StatusPaymentRequired // RFC 9110, 15.5.3
Forbidden = http.StatusForbidden // RFC 9110, 15.5.4
NotFound = http.StatusNotFound // RFC 9110, 15.5.5
MethodNotAllowed = http.StatusMethodNotAllowed // RFC 9110, 15.5.6
NotAcceptable = http.StatusNotAcceptable // RFC 9110, 15.5.7
ProxyAuthRequired = http.StatusProxyAuthRequired // RFC 9110, 15.5.8
RequestTimeout = http.StatusRequestTimeout // RFC 9110, 15.5.9
Conflict = http.StatusConflict // RFC 9110, 15.5.10
Gone = http.StatusGone // RFC 9110, 15.5.11
LengthRequired = http.StatusLengthRequired // RFC 9110, 15.5.12
PreconditionFailed = http.StatusPreconditionFailed // RFC 9110, 15.5.13
RequestEntityTooLarge = http.StatusRequestEntityTooLarge // RFC 9110, 15.5.14
RequestURITooLong = http.StatusRequestURITooLong // RFC 9110, 15.5.15
UnsupportedMediaType = http.StatusUnsupportedMediaType // RFC 9110, 15.5.16
RequestedRangeNotSatisfiable = http.StatusRequestedRangeNotSatisfiable // RFC 9110, 15.5.17
ExpectationFailed = http.StatusExpectationFailed // RFC 9110, 15.5.18
Teapot = http.StatusTeapot // RFC 9110, 15.5.19 (Unused)
MisdirectedRequest = http.StatusMisdirectedRequest // RFC 9110, 15.5.20
UnprocessableEntity = http.StatusUnprocessableEntity // RFC 9110, 15.5.21
Locked = http.StatusLocked // RFC 4918, 11.3
FailedDependency = http.StatusFailedDependency // RFC 4918, 11.4
TooEarly = http.StatusTooEarly // RFC 8470, 5.2.
UpgradeRequired = http.StatusUpgradeRequired // RFC 9110, 15.5.22
PreconditionRequired = http.StatusPreconditionRequired // RFC 6585, 3
TooManyRequests = http.StatusTooManyRequests // RFC 6585, 4
RequestHeaderFieldsTooLarge = http.StatusRequestHeaderFieldsTooLarge // RFC 6585, 5
UnavailableForLegalReasons = http.StatusUnavailableForLegalReasons // RFC 7725, 3
InternalServerError = http.StatusInternalServerError // RFC 9110, 15.6.1
NotImplemented = http.StatusNotImplemented // RFC 9110, 15.6.2
BadGateway = http.StatusBadGateway // RFC 9110, 15.6.3
ServiceUnavailable = http.StatusServiceUnavailable // RFC 9110, 15.6.4
GatewayTimeout = http.StatusGatewayTimeout // RFC 9110, 15.6.5
HTTPVersionNotSupported = http.StatusHTTPVersionNotSupported // RFC 9110, 15.6.6
VariantAlsoNegotiates = http.StatusVariantAlsoNegotiates // RFC 2295, 8.1
InsufficientStorage = http.StatusInsufficientStorage // RFC 4918, 11.5
LoopDetected = http.StatusLoopDetected // RFC 5842, 7.2
NotExtended = http.StatusNotExtended // RFC 2774, 7
NetworkAuthenticationRequired = http.StatusNetworkAuthenticationRequired // RFC 6585, 6
)

View file

@ -4,4 +4,7 @@ tasks:
btest:
cmds:
- go build ./cmd/test/
bhook:
cmds:
- go build ./cmd/hook/