golang在AWS上构建无服务器应用的专用框架插件Mantil的使用

Golang在AWS上构建无服务器应用的专用框架插件Mantil的使用

Mantil Logo

关于Mantil

Mantil是一个用于在Go中编写无服务器应用的现代开源框架。它允许您通过命令行界面快速创建和部署使用AWS Lambda的应用程序。

专为开发者设计

Mantil让开发者编写纯Go代码,无需了解AWS或Lambda的任何概念。因此,您不需要预先了解复杂的AWS生态系统和工具。

Mantil项目结构

云环境

在使用云中的真实服务时获得本地开发的感觉。通过内置对多个开发阶段和并行部署线的支持,您仍然可以保留私人开发沙箱。

持续部署

使用mantil watch命令,应用会在每次保存时自动部署。此外,它在几秒钟内完成,因此保留了流畅的开发工作流程。

Mantil Watch

Mantil还支持通过标准go测试进行代码测试,或在函数执行期间调用特定函数并立即获取日志(不是在函数完成后)。

快速开始

要开始使用Mantil,您需要安装Go并访问AWS账户。按照这些简单的步骤,您将在几分钟内启动并运行。

# 安装Mantil CLI
$ brew tap mantil-io/mantil
$ brew install mantil

# 在AWS上安装Mantil节点
$ mantil aws install --aws-profile=my-named-profile

# 创建您的第一个Mantil项目(仅创建项目结构)
$ mantil new my-project
$ cd my-project

# 将项目部署到开发阶段
$ mantil stage new development

就是这样,您已经使用Mantil创建了第一个无服务器应用。

示例代码

以下是一个简单的Mantil项目示例,展示如何创建一个基本的API端点:

// api/hello/handler.go
package hello

import (
    "github.com/mantil-io/mantil.go"
)

type Request struct {
    Name string
}

type Response struct {
    Message string
}

func Hello(req Request) (Response, error) {
    return Response{
        Message: "Hello " + req.Name,
    }, nil
}

func init() {
    mantil.Expose(&Hello)
}

这个示例创建了一个简单的Hello World API端点,可以通过HTTP请求调用。

项目结构

Mantil项目通常具有以下结构:

my-project/
├── api/              # API函数目录
│   └── hello/        # 示例hello函数
│       ├── handler.go # 函数实现
│       └── go.mod     # 模块文件
├── resources/        # 基础设施资源定义
│   └── aws/          # AWS特定资源
├── go.mod           # 主模块文件
└── mantil.json      # Mantil配置文件

Mantil CLI也可通过直接下载在Windows和Linux上使用。


更多关于golang在AWS上构建无服务器应用的专用框架插件Mantil的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang在AWS上构建无服务器应用的专用框架插件Mantil的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用Mantil在AWS上构建Golang无服务器应用

Mantil是一个专为Golang设计的无服务器框架,它简化了在AWS Lambda上构建、部署和管理无服务器应用的过程。下面我将详细介绍Mantil的使用方法。

Mantil核心特性

  • 专为Golang优化
  • 内置CI/CD流水线
  • 本地开发环境
  • 自动基础设施管理
  • 简洁的CLI工具

安装Mantil

首先安装Mantil CLI:

# MacOS/Linux
brew tap mantil-io/mantil
brew install mantil

# 或者直接下载
curl -sL https://install.mantil.com | sh

验证安装:

mantil --version

初始化项目

# 创建新项目
mantil new my-serverless-app
cd my-serverless-app

# 初始化AWS部署环境
mantil env new --aws-profile=your-profile-name

项目结构

典型的Mantil项目结构:

my-serverless-app/
├── api/          # API定义
├── functions/    # Lambda函数
├── store/        # 数据存储定义
└── stages/       # 部署环境配置

创建API端点

  1. 创建新函数:
mantil generate function hello
  1. 编辑生成的函数代码 (functions/hello/handler.go):
package hello

import (
	"context"
	
	"github.com/mantil-io/mantil.go"
)

type Request struct {
	Name string
}

type Response struct {
	Message string
}

func Handler(ctx context.Context, req Request) (Response, error) {
	return Response{
		Message: "Hello " + req.Name,
	}, nil
}

func init() {
	mantil.RegisterHandler(Handler)
}
  1. 定义API路由 (api/api.go):
package api

import (
	"github.com/mantil-io/mantil.go"
	"github.com/mantil-io/mantil/domain"
	
	// 导入函数包
	_ "github.com/your-username/my-serverless-app/functions/hello"
)

func API() *domain.API {
	return mantil.NewAPI(
		mantil.GET("/hello", hello.Handler),
	)
}

本地测试

# 启动本地开发服务器
mantil dev

# 在另一个终端测试
curl "http://localhost:3000/hello?name=World"

部署到AWS

# 部署到默认stage
mantil deploy

# 创建新的部署环境
mantil stage new production

# 部署到特定环境
mantil deploy --stage=production

查看部署状态

mantil status

调用已部署的API

# 获取API端点URL
mantil api

# 调用API
curl "https://your-api-id.execute-api.region.amazonaws.com/hello?name=World"

高级功能

使用DynamoDB

  1. 创建数据存储:
mantil generate store my-store
  1. 编辑生成的存储定义 (store/my-store.go):
package store

import "github.com/mantil-io/mantil.go"

type Item struct {
	ID    string
	Value string
}

type MyStore struct {
	mantil.Store
}

func (s *MyStore) Put(item Item) error {
	return s.Store.Put(item)
}

func (s *MyStore) Get(id string) (Item, error) {
	var item Item
	err := s.Store.Get(id, &item)
	return item, err
}
  1. 在函数中使用存储:
func Handler(ctx context.Context, req Request) (Response, error) {
	store := new(store.MyStore)
	if err := store.Put(store.Item{
		ID:    "123",
		Value: "test",
	}); err != nil {
		return Response{}, err
	}
	
	item, err := store.Get("123")
	if err != nil {
		return Response{}, err
	}
	
	return Response{
		Message: fmt.Sprintf("Stored value: %s", item.Value),
	}, nil
}

环境变量配置

# 设置环境变量
mantil env set DB_CONNECTION_STRING "your-db-conn-str"

# 在代码中访问
connectionStr := os.Getenv("DB_CONNECTION_STRING")

监控和日志

# 查看日志
mantil logs --function=hello

# 跟踪实时日志
mantil logs --function=hello --follow

清理资源

# 删除部署环境
mantil stage destroy production

# 完全删除项目
mantil env destroy

Mantil为Golang开发者提供了简单高效的无服务器开发体验,自动处理了大部分AWS基础设施的配置工作,让开发者可以专注于业务逻辑的实现。

回到顶部