nest

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 14 Imported by: 0

README

NestGo

NestGo is a powerful and flexible framework for building scalable server-side applications with Golang. Inspired by NestJS, NestGo provides a modular architecture, dependency injection, and a rich set of features, making it ideal for building robust, maintainable, and production-ready applications.

Key Features

  • Modular Architecture: Organize your code in modules, services, and controllers to ensure scalability and maintainability.
  • Dependency Injection: Simplify dependency management and testing with built-in dependency injection.
  • Decorator-based Routing: Define routes using decorators for a clean and intuitive API structure.
  • Middleware Support: Easily add custom middleware for request handling.
  • Guards, Interceptors, and Validation: Enforce security, handle requests/responses, and validate input seamlessly.
  • Customizable: Extensible design allows integration with a wide range of libraries and tools.
  • Built with Go's Performance in Mind: Fast, efficient, and optimized for high-performance applications.

Installation

To get started with NestGo, install the framework using Go modules:

go get github.com/vanyastar/nest

Project Structure

A typical NestGo project is structured as follows:

my-nestgo-app/
├── main.service.go           # Main entry point for the application
├── hello/
│   ├── hello.controller.go   # Controller with route handlers
│   ├── hello.service.go      # Service containing business logic
│   ├── dto/                  # Data Transfer Objects (DTOs) for request validation
│   ├── validators/           # Validators
│   └── guards/               # Guards for route protection
├── common/
│   └── middlewares.go        # Example custom middleware
└── config/
    └── config.go             # Configuration settings

Core Concepts

Controllers

Controllers handle incoming requests and return responses to the client. NestGo uses decorators to simplify route handling and make it more readable.

Services

Services contain the business logic of your application and are injected into controllers as dependencies.

Middleware

Middleware functions are executed before the request reaches the controller, making them ideal for logging, authentication, or modifying the request object.

Guards

Guards are used to enforce authorization rules, deciding if a request should proceed to the next stage or be blocked.

Interceptors

Interceptors transform the request or response, making it easier to apply logic like response formatting or error handling globally.

DTO (Data Transfer Objects)

DTOs define the structure of data sent to and from the server. DTOs are used for validating and transforming incoming data in the controllers.

Validation

NestGo includes built-in validation using DTOs, ensuring that incoming requests adhere to the specified structure and constraints.


Quick Start

Follow these steps to create a new NestGo project:

  1. Initialize Your Project

    mkdir my-nestgo-app
    cd my-nestgo-app
    go mod init my-nestgo-app
    
  2. Install NestGo

    go get github.com/vanyastar/nest
    
  3. Create Your First Module

    Organize your application by creating modules, controllers, and services.


License

NestGo is open source and available under the MIT License.


This README includes a file structure and setup details to help you get started with NestGo. For more information, check out the documentation or reach out with any questions!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SessionStorage sync.Map

SessionStore holds all sessions

Functions

func CreateApp

func CreateApp(fn AppContextFunc, sArr ...*http.Server) *httpServers

Create NestGO App from nest factory

Types

type AppContext

type AppContext struct {
	// contains filtered or unexported fields
}

func (*AppContext) Controller

func (c *AppContext) Controller(path string, fn DefaultControllerFunc, midWares ...MidWare)

Default Controller - Here you can override the default middlewares for this controller and assign your own.

func (AppContext) Header

func (c AppContext) Header(key, value string)

func (AppContext) MaxBodySize

func (c AppContext) MaxBodySize(maxBodySize int64)

func (*AppContext) Static

func (c *AppContext) Static(path string, dir string, fn DefaultControllerFunc)

Static Server Controller

func (AppContext) Use

func (c AppContext) Use(fn ...MidWare)

Add middlewares for each level of application

func (*AppContext) UseGlobal

func (c *AppContext) UseGlobal(fn ...MidWare)

Add the middleware globally, but it will still depend on where you are going to use it. At the global level or at the level of several controllers.

type AppContextFunc

type AppContextFunc func(c *AppContext)

type ControllerHandler

type ControllerHandler func(*Ctx, *handler)

type Ctx

type Ctx struct {
	Next nextFunc
	// contains filtered or unexported fields
}

Ctx represents the context with response and request methods

func (*Ctx) BodyParser

func (c *Ctx) BodyParser(i any) error

Regular body parser based on the Content-Type header

func (*Ctx) Cookie

func (c *Ctx) Cookie(name string) (*http.Cookie, error)

func (*Ctx) DtoParser

func (c *Ctx) DtoParser(i IDto) error

Regular body parser + auto validation from struct method, based on the Content-Type header

func (*Ctx) Error

func (c *Ctx) Error(statusCode int, message any) error

Error handles errors based on content type and message type HTTP 200 don't send message from this function

func (*Ctx) Flush

func (c *Ctx) Flush() error

Flush sends any buffered data to the client.

func (*Ctx) Req

func (c *Ctx) Req() *Request

Get access to *http.Request.

func (*Ctx) Res

func (c *Ctx) Res() *Response

Get access to http.ResponseWriter.

func (*Ctx) Send

func (c *Ctx) Send(v any) error

Respond encodes the value as JSON or XML based on the Content-Type header

func (*Ctx) SendFile

func (c *Ctx) SendFile(f string) error

Sends local file contents from the given path as response body.

func (*Ctx) SendString

func (c *Ctx) SendString(s string) error

Respond as string result

func (*Ctx) Session

func (c *Ctx) Session() *Session

Session Manager TODO: Make custom storages for session, on this moment only RAM.

func (*Ctx) SetCookie

func (c *Ctx) SetCookie(name, value, path, domain string, maxAge int, secure, httpOnly bool)

Set cookie

type DefaultController

type DefaultController struct {
	// contains filtered or unexported fields
}

func (*DefaultController) Connect

func (c *DefaultController) Connect(path string, ef *EndFunc)

func (*DefaultController) Delete

func (c *DefaultController) Delete(path string, ef *EndFunc)

func (*DefaultController) Get

func (c *DefaultController) Get(path string, ef *EndFunc)

func (*DefaultController) Head

func (c *DefaultController) Head(path string, ef *EndFunc)

func (DefaultController) Header

func (c DefaultController) Header(key, value string)

func (DefaultController) MaxBodySize

func (c DefaultController) MaxBodySize(maxBodySize int64)

func (*DefaultController) Options

func (c *DefaultController) Options(path string, ef *EndFunc)

func (*DefaultController) Patch

func (c *DefaultController) Patch(path string, ef *EndFunc)

func (*DefaultController) Post

func (c *DefaultController) Post(path string, ef *EndFunc)

func (*DefaultController) Put

func (c *DefaultController) Put(path string, ef *EndFunc)

func (*DefaultController) Redirect

func (c *DefaultController) Redirect(code int, location string)

func (*DefaultController) Sse

func (c *DefaultController) Sse()

func (*DefaultController) Trace

func (c *DefaultController) Trace(path string, ef *EndFunc)

func (DefaultController) Use

func (c DefaultController) Use(fn ...MidWare)

Add middlewares for each level of application

type DefaultControllerFunc

type DefaultControllerFunc func(c *DefaultController)

type EndFunc

type EndFunc func(*Ctx) error

type IDto

type IDto interface {
	Validate() error
}

type MidWare

type MidWare func(*Ctx)

type Request

type Request struct {
	*http.Request
}

type Response

type Response struct {
	http.Flusher
	http.ResponseWriter
}

type Session

type Session struct {
	ID        string
	Values    sync.Map
	ExpiresAt time.Time
}

Session represents a user session

func (*Session) GetValue

func (s *Session) GetValue(key string) (interface{}, bool)

GetValue retrieves a value from the session

func (*Session) Save

func (s *Session) Save(c *Ctx, storageEngine ...func()) error

Save writes the session data to the response cookie

func (*Session) SetExpiration

func (s *Session) SetExpiration(duration time.Duration) *Session

SetExpiration sets the expiration time for the session

func (*Session) SetValue

func (s *Session) SetValue(key string, value interface{}) *Session

SetValue sets a value in the session

type StaticServerConfig

type StaticServerConfig struct {
	RootDir   string
	MimeTypes map[string]string
}

Directories

Path Synopsis
nestcli module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL