wip: refactory uapp to uzone

feat: config first
This commit is contained in:
loveuer
2024-12-30 15:09:02 +08:00
commit 7efeb093f2
56 changed files with 6120 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package interfaces
import (
"context"
"mime/multipart"
"net"
)
type ApiGroup interface {
Group(path string, handlers ...ApiHandler) ApiGroup
GET(path string, handlers ...ApiHandler)
POST(path string, handlers ...ApiHandler)
PUT(path string, handlers ...ApiHandler)
DELETE(path string, handlers ...ApiHandler)
HEAD(path string, handlers ...ApiHandler)
PATCH(path string, handlers ...ApiHandler)
OPTIONS(path string, handlers ...ApiHandler)
Handle(method, path string, handlers ...ApiHandler)
Use(handlers ...ApiHandler)
}
type ApiEngine interface {
ApiGroup
Run(address string) error
RunListener(ln net.Listener) error
}
type ApiContext interface {
App() Uzone
// parse body, form, json
BodyParser(out any) error
Context() context.Context
Cookie(string) string
FormFile(string) (*multipart.FileHeader, error)
FormValue(string) string
GetHeader(string) string
SetHeader(string, string)
IP() string
Json(any) error
Locals(interface{}, ...any) any
// get method or rewrite method
Method(string, ...string) string
MultipartForm() (*multipart.Form, error)
Param(string) string
// get path or rewrite path
Path(string, ...string) string
Next() error
Query(string) string
QueryParse(any) error
Redirect(string, ...int) error
// render html response
Render(name, layout string, data any) error
// set response status
Status(int)
SendStatus(int)
Write([]byte) (int, error)
}
type ApiHandler func(c ApiContext) error

10
pkg/interfaces/logger.go Normal file
View File

@ -0,0 +1,10 @@
package interfaces
type Logger interface {
Debug(string, ...any)
Info(string, ...any)
Warn(string, ...any)
Error(string, ...any)
Panic(string, ...any)
Fatal(string, ...any)
}

18
pkg/interfaces/upp.go Normal file
View File

@ -0,0 +1,18 @@
package interfaces
import (
"context"
"github.com/elastic/go-elasticsearch/v7"
"github.com/loveuer/uzone/pkg/cache"
"gorm.io/gorm"
)
type Uzone interface {
Debug() bool
UseCtx() context.Context
UseDB(ctx ...context.Context) *gorm.DB
UseCache() cache.Cache
UseES() *elasticsearch.Client
UseLogger(ctxs ...context.Context) Logger
}