server

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 13 Imported by: 0

README

gin-multi-server

gin-multi-server lets the same handlers run on AWS Lambda or a traditional local Gin server. Lambda mode supports both API Gateway REST API (payload v1) and HTTP API (payload v2) events.

Features

  • Dual Mode Support: Runs on AWS Lambda or as a local server without modification.
  • Dynamic Routing: Easily define and mount routes with custom handlers.
  • CORS Configuration: Automatically sets up CORS headers based on registered routes.
  • Lightweight and Extensible: Designed as a blueprint for extensibility.
  • Simple API: Minimal learning curve for integrating into your project.

Installation

Install the package using go get:

go get github.com/GEMSDEV-mx/gin-multi-server

Usage

Example

Here's a quick example of how to use gin-lambda-server:

package main

import (
	"context"
	"net/http"

	"github.com/aws/aws-lambda-go/events"
	server "github.com/GEMSDEV-mx/gin-multi-server"
)

func main() {
	s := server.NewServer()

	// Define a handler
	handler := func(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
		return events.APIGatewayProxyResponse{
			StatusCode: http.StatusOK,
			Body:       "Hello, World!",
		}, nil
	}

	// Mount routes
	s.MountEndpoint(server.GET, "/hello", handler)

	// Start the server
	s.Serve("8080")
}
Running Locally

Set the AWS_LAMBDA_FUNCTION_NAME environment variable to an empty string or leave it unset to run the server in local mode. Pass an empty port to use the default, 8080.

go run main.go
Running on AWS Lambda

Deploy the application as an AWS Lambda function. The server automatically switches to Lambda mode if AWS_LAMBDA_FUNCTION_NAME is set in the environment.

API

NewServer()

Creates a new instance of the server.

MountEndpoint(method Method, path string, handler HandlerFunction)

Mounts a new route to the server.

  • method: The HTTP method (e.g., GET, POST).
  • path: The URL path (e.g., /example).
  • handler: A function matching the HandlerFunction signature.
Serve(port string)

Starts the server. Automatically detects whether to run in Lambda mode or as a local server.

Extensibility

This package is designed as a blueprint. You can extend its functionality to support other platforms by modifying the Serve() method or adding new integrations.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

Built with love using the Gin framework and AWS Lambda Go SDK.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunction

HandlerFunction defines the handler signature for Lambda

type Method

type Method string

Method defines the supported HTTP methods

const (
	GET     Method = "GET"
	POST    Method = "POST"
	PUT     Method = "PUT"
	DELETE  Method = "DELETE"
	PATCH   Method = "PATCH"
	OPTIONS Method = "OPTIONS"
)

Enum values for Method

type Route

type Route struct {
	Method  Method
	Path    string
	Handler HandlerFunction
}

Route represents a single route

type Server

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

Server encapsulates both Lambda and local server behavior

func NewServer

func NewServer() *Server

NewServer creates a new server instance

func (*Server) MountEndpoint

func (s *Server) MountEndpoint(method Method, path string, handler HandlerFunction)

MountEndpoint adds an endpoint with a specified handler

func (*Server) Router

func (s *Server) Router() *gin.Engine

Router exposes the internal Gin engine for raw access.

func (*Server) Serve

func (s *Server) Serve(port string)

Serve starts the server

Jump to

Keyboard shortcuts

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