Documentation
¶
Overview ¶
Package web is the Laravel-style HTTP framework for lagodev.
Bu paket Gin/Fiber'ga muqobil sodda, framework-mustaqil API beradi. Asosiy tushunchalar:
- web.App — ilovaning tepa darajadagi konteyner (Router + Server + DB)
- web.Context — har bir so'rovga moslangan helper'lar (JSON, Bind, Param)
- web.Router — Laravel-uslubidagi marshrutlash (Get/Post/Resource/Group)
- web.ResourceController — RESTful 5 metodli interfeys
Minimal misol:
app := web.New()
app.Get("/", func(c *web.Context) { c.String(200, "hi") })
app.Resource("posts", &PostController{})
app.Run(":8080")
Index ¶
- type App
- type Context
- func (c *Context) BadRequest(msg string)
- func (c *Context) Bind(dst any) error
- func (c *Context) Created(v any) any
- func (c *Context) Ctx() context.Context
- func (c *Context) Error(err error) bool
- func (c *Context) Forbidden(msg string)
- func (c *Context) Get(key string) (any, bool)
- func (c *Context) InternalError(err error)
- func (c *Context) JSON(code int, v any)
- func (c *Context) MustBind(dst any) bool
- func (c *Context) NoContent()
- func (c *Context) NotFound(msg string)
- func (c *Context) Param(name string) string
- func (c *Context) ParamInt(name string) int
- func (c *Context) ParamUint(name string) uint64
- func (c *Context) Query(name string) string
- func (c *Context) QueryDefault(name, fallback string) string
- func (c *Context) QueryInt(name string, fallback int) int
- func (c *Context) Set(key string, value any)
- func (c *Context) Status(code int)
- func (c *Context) String(code int, body string)
- func (c *Context) Unauthorized(msg string)
- type Handler
- type Middleware
- type Option
- type ResourceController
- type Route
- type Router
- func (r *Router) APIResource(name string, ctrl ResourceController)
- func (r *Router) Any(path string, h Handler)
- func (r *Router) Delete(path string, h Handler)
- func (r *Router) Get(path string, h Handler)
- func (r *Router) Group(prefix string, fn func(g *Router))
- func (r *Router) Head(path string, h Handler)
- func (r *Router) Options(path string, h Handler)
- func (r *Router) Patch(path string, h Handler)
- func (r *Router) Post(path string, h Handler)
- func (r *Router) Put(path string, h Handler)
- func (r *Router) Resource(name string, ctrl ResourceController)
- func (r *Router) Routes() []Route
- func (r *Router) Use(mw ...Middleware)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
type App struct {
*Router // Top-level marshrutlash uchun embedded
// contains filtered or unexported fields
}
App — ilovaning tepa darajadagi konteyner. Marshrutlar, ulanish, sozlamalar va HTTP server'ni birgalikda saqlaydi. Foydalanuvchi faqat main.go'da `web.New(...).Run()` chaqiradi.
func New ¶
New — yangi App yaratadi va kerakli o'rta dasturlarni standart qilib qo'shadi (Logger + Recovery). Disable qilish uchun Reset() chaqiring.
func (*App) DB ¶
func (a *App) DB() *database.Connection
DB — biriktirilgan database.Connection'ni qaytaradi (yo'q bo'lsa nil).
type Context ¶
type Context struct {
// Writer — yozish uchun standart http.ResponseWriter.
Writer http.ResponseWriter
// Request — kelgan HTTP so'rov.
Request *http.Request
// DB — agar App.WithDatabase orqali biriktirilgan bo'lsa, ulanish.
// Hech qachon nil bo'lmasligi shart emas; nil bo'lganda foydalanuvchi
// o'zi tekshiradi.
DB *database.Connection
// contains filtered or unexported fields
}
Context bitta HTTP so'rovni ifoda etadi. U handler'lar va middleware'lar orasidan o'tadi va so'rov/javob ustida ishlash uchun helper'lar beradi.
func (*Context) Bind ¶
Bind so'rov body'sini JSON sifatida dst'ga decode qiladi. Xato bo'lsa avtomatik 400 Bad Request qaytaradi va sentinel xatoni qaytaradi.
func (*Context) Created ¶ added in v0.8.0
Created — Store handler uchun qulay yordamchi. 201 statusi'ni belgilab foydalanuvchi return value, nil yozsa, framework JSON'ga aylantiradi:
return ctx.Created(post), nil
func (*Context) Ctx ¶
Ctx so'rovning context.Context'ini qaytaradi. DB chaqiruvlariga uzating:
user, err := orm.Query[User](c.DB).Find(c.Ctx(), id)
func (*Context) Error ¶
Error err nil emas bo'lsa, mos status kodi bilan JSON javob qaytaradi. orm.ErrNotFound uchun 404, qolganlar uchun 500. Foydalanish:
if c.Error(err) { return }
func (*Context) InternalError ¶
InternalError 500 + JSON xato.
func (*Context) MustBind ¶
MustBind Bind bilan bir xil lekin xato bo'lsa true qaytaradi. Foydalanish: if !c.MustBind(&payload) { return }
func (*Context) Param ¶
Param yo'l parametri qiymatini qaytaradi (masalan "/posts/{id}" → "id"). Yo'qol bo'lsa, bo'sh qator.
func (*Context) ParamUint ¶
ParamUint yo'l parametri'ni uint64 ko'rinishida qaytaradi. Xato yoki bo'sh bo'lsa 0 qaytadi.
func (*Context) QueryDefault ¶
QueryDefault Query bilan bir xil, lekin bo'sh bo'lsa fallback qaytaradi.
func (*Context) Unauthorized ¶
Unauthorized 401 + JSON xato.
type Handler ¶
Handler — har bir route metodining imzosi.
Laravel uslubida ikki qiymat qaytaradi:
- any — JSON sifatida javob beriladi (nil bo'lsa, 204 No Content)
- error — nil emas bo'lsa, avtomat xato javobi (orm.ErrNotFound → 404, qolganlari → 500). c.Status(N) chaqirib ham qo'shimcha holat kodini majburlash mumkin.
type Middleware ¶
Middleware — Handler'ni boshqa Handler bilan o'rab oladigan funksiya. Birinchi ro'yxatga olingan middleware tashqaridagisi (so'rov birinchi uning ichidan o'tadi).
func Auth ¶
func Auth() Middleware
Auth — sodda Bearer-token middleware. Faqat sarlavha mavjudligini tekshiradi va xom token'ni context'ga yozadi. Token'ni tekshirish kerak bo'lsa AuthJWT(manager) ishlating.
func AuthJWT ¶ added in v0.9.0
func AuthJWT(m *auth.Manager) Middleware
AuthJWT — JWT'ni tekshiradigan to'liq middleware. Token noto'g'ri yoki muddati o'tgan bo'lsa 401 qaytaradi. Muvaffaqiyat holida claims context'ga yoziladi: "auth_user_id", "auth_role", "auth_claims".
func CORS ¶
func CORS(allowedOrigins ...string) Middleware
CORS — sodda CORS middleware. Origin'ni allowedOrigins ro'yxati bilan taqqoslaydi (yoki "*" — har qanday).
func Logger ¶
func Logger(l *log.Logger) Middleware
Logger — har bir so'rov uchun method, path, status kodi va davomiyligini log'ga yozadigan standart middleware. App.New() avtomat qo'shadi.
func Recovery ¶
func Recovery(l *log.Logger) Middleware
Recovery — panic'ni tutib, JSON 500 javobini qaytaradigan middleware. Stack trace stderrga yoziladi. App.New() avtomat qo'shadi.
type Option ¶
type Option func(*App)
Option — funksional sozlash usuli.
func WithDatabase ¶
func WithDatabase(conn *database.Connection) Option
WithDatabase — ochiq *database.Connection'ni ilovaga biriktiradi. Handler'lardan `c.DB` orqali foydalanish mumkin.
func WithManager ¶
WithManager — *database.Manager'ni biriktiradi. Run() chiqqach Close() chaqiriladi.
func WithMigrations ¶
func WithMigrations(reg *migrations.Registry) Option
WithMigrations — Run() vaqtida migratsiyalarni avtomat qo'llashni yoqadi. Registry nil bo'lsa, package-default ishlatiladi.
func WithShutdownTimeout ¶
WithShutdownTimeout — graceful shutdown vaqti (default 10s).
type ResourceController ¶
type ResourceController interface {
Index(c *Context) (any, error)
Show(c *Context) (any, error)
Store(c *Context) (any, error)
Update(c *Context) (any, error)
Destroy(c *Context) (any, error)
}
ResourceController — Resource() bilan ishlatish uchun interfeys. 5 ta RESTful metod (any, error) qaytaradi — Laravel uslubida.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router — marshrut, group va middleware'larni e'lon qilish vositasi.
func (*Router) APIResource ¶
func (r *Router) APIResource(name string, ctrl ResourceController)
APIResource — Resource bilan bir xil; nom Laravel konventsiyasi uchun.
func (*Router) Get ¶
HTTP verb shortcutlar — har biri prefiks va middleware bilan to'liq route ro'yxatga oladi.
func (*Router) Group ¶
Group — yangi prefiks va o'rta dasturlar to'plamiga ega bola router. Misol: app.Group("/api/v1", func(g *web.Router) { g.Resource("posts", ...) })
func (*Router) Resource ¶
func (r *Router) Resource(name string, ctrl ResourceController)
Resource — RESTful 5 ta marshrutni avtomat e'lon qiladi.
GET /posts → Index
GET /posts/{id} → Show
POST /posts → Store
PUT /posts/{id} → Update
PATCH /posts/{id} → Update
DELETE /posts/{id} → Destroy
`name` — ko'plik (`posts`, `users`).
func (*Router) Use ¶
func (r *Router) Use(mw ...Middleware)
Use — har bir keyingi marshrutga qo'llaniladigan middleware.