feat: add HTTP migration bridges and extensible API binding
This commit is contained in:
29
examples/compatibility/ids/id.go
Normal file
29
examples/compatibility/ids/id.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Package ids demonstrates an application-owned scalar. The framework does not
|
||||
// impose this ID policy on any other type or project.
|
||||
package ids
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ID int64
|
||||
|
||||
func (id ID) MarshalJSON() ([]byte, error) { return json.Marshal(strconv.FormatInt(int64(id), 10)) }
|
||||
func (id *ID) UnmarshalJSON(raw []byte) error {
|
||||
value := string(raw)
|
||||
if len(raw) > 0 && raw[0] == '"' {
|
||||
if err := json.Unmarshal(raw, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return id.UnmarshalText([]byte(value))
|
||||
}
|
||||
func (id *ID) UnmarshalText(raw []byte) error {
|
||||
value, err := strconv.ParseInt(string(raw), 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*id = ID(value)
|
||||
return nil
|
||||
}
|
||||
75
examples/compatibility/main.go
Normal file
75
examples/compatibility/main.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// This example only uses local HTTP and bounded streaming; it has no database.
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
"git.yuntuo.hk/k2safe/go-omni/contract"
|
||||
"git.yuntuo.hk/k2safe/go-omni/examples/compatibility/ids"
|
||||
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
||||
)
|
||||
|
||||
type echoRequest struct {
|
||||
ID ids.ID `json:"id"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := omni.New(omni.WithLogger(true))
|
||||
defer app.Close()
|
||||
app.Use(httpx.WrapMiddleware(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Example", "compatibility")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}))
|
||||
app.GET("/legacy", httpx.WrapHandler(http.HandlerFunc(http.NotFound)))
|
||||
binder := contract.MustNew[echoRequest](contract.ScalarOf[ids.ID]("string|int64", "string", true))
|
||||
app.POST("/echo", func(c *httpx.Context) error {
|
||||
var req echoRequest
|
||||
if err := binder.Bind(c.Request(), nil, &req); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.OK(req)
|
||||
})
|
||||
app.HandleWithBodyLimit("POST", "/upload", 10<<20, nil, func(c *httpx.Context) error {
|
||||
reader, err := c.Request().MultipartReader()
|
||||
if err != nil {
|
||||
return omni.BadRequest("INVALID_MULTIPART", "multipart body required")
|
||||
}
|
||||
var total int64
|
||||
for {
|
||||
part, err := reader.NextPart()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Replace io.Discard with a streaming storage writer. Do not trust file names
|
||||
// as filesystem paths. io.Copy never buffers the complete uploaded file.
|
||||
n, copyErr := io.Copy(io.Discard, part)
|
||||
closeErr := part.Close()
|
||||
if copyErr != nil {
|
||||
return copyErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
return closeErr
|
||||
}
|
||||
total += n
|
||||
}
|
||||
return c.OK(map[string]int64{"bytes": total})
|
||||
})
|
||||
app.GET("/download", httpx.WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="example.txt"`)
|
||||
_, _ = io.Copy(w, strings.NewReader("A standard Handler keeps its original download response.\n"))
|
||||
})))
|
||||
if err := app.Run("127.0.0.1:8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user