golang模块化Web开发框架插件Flamingo的使用

Golang模块化Web开发框架插件Flamingo的使用

Flamingo是一个基于Go语言的Web框架,专为构建可插拔和可维护的Web项目而设计。它已经经过生产环境测试,拥有不断增长的生态系统。

Flamingo Logo

快速入门

初始化项目

mkdir helloworld
cd helloworld
go mod init helloworld

创建主文件

创建main.go文件:

package main

import (
	"flamingo.me/dingo"
	"flamingo.me/flamingo/v3"
)

func main() {
	flamingo.App([]dingo.Module{
	})
}

运行项目:

go run main.go

这将显示可用的命令列表:

Flamingo main

Usage:
  main [command]

Examples:
Run with -h or -help to see global debug flags

Available Commands:
  config      Config dump
  handler     Dump the Handlers and its registered methods
  help        Help about any command
  routes      Routes dump
  serve       Default serve command - starts on Port 3322

Flags:
  -h, --help   help for main

Use "main [command] --help" for more information about a command.

启动服务器:

go run main.go serve

服务器将在3322端口启动。

Hello World示例

创建一个helloworld模块:

mkdir helloworld

创建helloworld/module.go文件:

package helloworld

import (
        "context"
        "net/http"
        "strings"
        
        "flamingo.me/dingo"
        "flamingo.me/flamingo/v3/framework/web"
)

type Module struct{}

func (*Module) Configure(injector *dingo.Injector) {
        web.BindRoutes(injector, new(routes))
}

type routes struct{}

func (*routes) Routes(registry *web.RouterRegistry) {
        registry.Route("/", "home")
        registry.HandleAny("home", indexHandler)
}

func indexHandler(ctx context.Context, req *web.Request) web.Result {
        return &web.Response{
            Status: http.StatusOK,
            Body:   strings.NewReader("Hello World!"),
        }
}

修改main.go以包含新模块:

package main

import (
	"flamingo.me/dingo"
	"flamingo.me/flamingo/v3"
	"helloworld/helloworld"
)

func main() {
	flamingo.App([]dingo.Module{
        new(helloworld.Module),
	})
}

再次运行服务器:

go run main.go serve

访问localhost:3322将看到"Hello World!"输出。

框架特性

Flamingo提供以下主要特性:

  • 使用Dingo的依赖注入
  • 灵活的模板引擎(gotemplates和pugtemplates)
  • 基于Cue的配置概念
  • 基于Dingo的模块化架构
  • 认证概念和安全中间件
  • 灵活的路由系统
  • Web控制器概念
  • 运维就绪:日志、追踪、指标和健康检查
  • 本地化支持
  • 事件处理
  • 会话处理和管理

生态系统

Flamingo拥有丰富的生态系统:

  • GraphQL模块
  • 缓存模块
  • pugtemplate模板引擎
  • Flamingo Commerce模块(用于构建现代电子商务应用)

获取帮助

获取帮助的最佳方式是在Gophers Slack的#flamingo频道提问。如果没有加入Gophers Slack,可以通过https://invite.slack.golangbridge.org/获取邀请。

其他联系方式:

  • 在Stack Overflow提问
  • 发送邮件至flamingo@aoe.com
  • 在GitHub提交issue

更多关于golang模块化Web开发框架插件Flamingo的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang模块化Web开发框架插件Flamingo的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Flamingo框架使用指南

Flamingo是一个基于Go语言的模块化Web开发框架,它采用依赖注入(DI)和模块化设计,非常适合构建大型、可扩展的Web应用程序。下面我将介绍Flamingo的核心概念和基本使用方法。

核心概念

  1. 模块(Module):Flamingo应用由多个模块组成,每个模块提供特定功能
  2. 依赖注入(DI):框架核心特性,自动管理组件依赖关系
  3. 路由(Routing):灵活的URL路由系统
  4. 模板引擎:内置模板支持,可扩展

快速开始

1. 安装Flamingo

go get -u github.com/i-love-flamingo/flamingo

2. 创建基本应用结构

myapp/
├── go.mod
├── go.sum
├── config/
│   └── config.yml
└── src/
    └── myapp/
        ├── module.go
        └── controller/
            └── hello.go

3. 创建主模块

src/myapp/module.go中定义主模块:

package myapp

import (
	"flamingo.me/dingo"
	"flamingo.me/flamingo/v3/framework/web"
)

// Module 是我们的主模块
type Module struct{}

// Configure 模块配置
func (m *Module) Configure(injector *dingo.Injector) {
	web.BindRoutes(injector, new(routes))
}

// routes 定义路由
type routes struct {
	helloController *controller.HelloController
}

// Inject 依赖注入
func (r *routes) Inject(helloController *controller.HelloController) {
	r.helloController = helloController
}

// Routes 配置路由
func (r *routes) Routes(registry *web.RouterRegistry) {
	registry.HandleGet("hello", r.helloController.Get)
}

4. 创建控制器

src/myapp/controller/hello.go中:

package controller

import (
	"flamingo.me/flamingo/v3/framework/web"
)

// HelloController 处理hello请求
type HelloController struct {
	Responder *web.Responder
}

// Inject 依赖注入
func (c *HelloController) Inject(responder *web.Responder) {
	c.Responder = responder
}

// Get 处理GET请求
func (c *HelloController) Get(ctx web.Context) web.Result {
	return c.Responder.Data("Hello, Flamingo!")
}

5. 创建主程序

在项目根目录创建main.go

package main

import (
	"flamingo.me/dingo"
	"flamingo.me/flamingo/v3"
	"flamingo.me/flamingo/v3/framework/config"

	"myapp/src/myapp"
)

func main() {
	flamingo.App([]dingo.Module{
		new(myapp.Module),
	}, flamingo.Configure(config.LoadConfigFile("config/config.yml")))
}

6. 运行应用

go run main.go

访问 http://localhost:3322/hello 将会看到"Hello, Flamingo!"的响应。

高级功能

1. 使用模板

修改控制器返回模板:

func (c *HelloController) Get(ctx web.Context) web.Result {
	return c.Responder.Render("hello", map[string]interface{}{
		"message": "Hello from template!",
	})
}

创建模板文件templates/hello.html

<!DOCTYPE html>
<html>
<head>
    <title>Flamingo Example</title>
</head>
<body>
    <h1>{{.message}}</h1>
</body>
</html>

2. 中间件支持

创建自定义中间件:

package middleware

import (
	"flamingo.me/flamingo/v3/framework/web"
)

type LoggingMiddleware struct {}

func (m *LoggingMiddleware) Inject() *LoggingMiddleware {
	return m
}

func (m *LoggingMiddleware) Serve(ctx web.Context, next web.Handler) web.Result {
	// 请求前处理
	log.Println("Before handling request:", ctx.Request().URL)
	
	// 调用下一个处理器
	result := next(ctx)
	
	// 请求后处理
	log.Println("After handling request")
	
	return result
}

在模块中注册中间件:

func (m *Module) Configure(injector *dingo.Injector) {
	web.BindRoutes(injector, new(routes))
	
	// 注册中间件
	web.BindMiddleware(injector, new(middleware.LoggingMiddleware))
}

3. 配置管理

config/config.yml中添加配置:

greeting:
  message: "Hello from config!"

使用配置:

type GreetingService struct {
	Message string `inject:"config:greeting.message"`
}

func (s *GreetingService) GetMessage() string {
	return s.Message
}

在控制器中使用服务:

type HelloController struct {
	Responder *web.Responder
	Greeting  *GreetingService
}

func (c *HelloController) Get(ctx web.Context) web.Result {
	return c.Responder.Data(c.Greeting.GetMessage())
}

总结

Flamingo框架通过模块化和依赖注入提供了高度可扩展的Web开发体验。它的主要优势包括:

  1. 清晰的架构设计,适合大型项目
  2. 强大的依赖注入系统
  3. 灵活的模块化组织
  4. 内置模板、路由、中间件等Web开发常用功能

通过上述示例,你可以快速开始使用Flamingo构建Web应用。随着项目复杂度增加,你可以继续探索Flamingo更高级的功能,如事件系统、领域驱动设计支持等。

回到顶部