第一版本
每个资源的每个方法都是分开的
资源和资源是分开的
资源的方法和资源的方法也是不相干的
示例中涉及的资源是 task和service
package main
import (
"github.com/gin-gonic/gin"
)
// task结构体
type Task struct {
Name string
Description string
}
// 定义task需要的方法
type TaskRegistry struct {
}
func (t *TaskRegistry) ListTasks() ([]Task, error) {
return []Task{{
Name: "task",
Description: "task test",
}}, nil
}
func (t *TaskRegistry) CreateTask(task Task) error {
return nil
}
// service结构体
type Service struct {
Name string
Description string
}
// 定义service需要的方法
type ServiceRegistry struct {
}
func (s *ServiceRegistry) ListServices() ([]Service, error) {
return []Service{{
Name: "service",
Description: "service test",
}}, nil
}
func (s *ServiceRegistry) CreateService(task Service) error {
return nil
}
// task API 处理函数
func createTaskHandler(c *gin.Context) {
var task Task
if err := c.ShouldBindJSON(&task); err != nil {
c.JSON(400, gin.H{"error": "Bad Request"})
return
}
taskRegistry := TaskRegistry{}
err := taskRegistry.CreateTask(task)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to create task"})
return
}
c.JSON(201, gin.H{"message": "Task created successfully"})
}
func listTasksHandler(c *gin.Context) {
taskRegistry := TaskRegistry{}
tasks, err := taskRegistry.ListTasks()
if err != nil {
c.JSON(500, gin.H{"error": "Failed to list tasks"})
return
}
c.JSON(200, tasks)
}
// service API 处理函数
func listServicesHandler(c *gin.Context) {
serviceRegistry := ServiceRegistry{}
services, err := serviceRegistry.ListServices()
if err != nil {
c.JSON(500, gin.H{"error": "Failed to list tasks"})
return
}
c.JSON(200, services)
}
func createServicesHandler(c *gin.Context) {
var service Service
if err := c.ShouldBindJSON(&service); err != nil {
c.JSON(400, gin.H{"error": "Bad Request"})
return
}
ServiceRegistry := ServiceRegistry{}
err := ServiceRegistry.CreateService(service)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to create service"})
return
}
c.JSON(201, gin.H{"message": "Service created successfully"})
}
func main() {
// 初始化 Gin Web 框架
r := gin.Default()
// 注册 API 路由
r.POST("/tasks", createTaskHandler)
r.GET("/tasks", listTasksHandler)
r.GET("/services", listServicesHandler)
r.POST("/services", createServicesHandler)
// 启动 Web 服务器
r.Run(":8080")
}
第二版本
抽象资源
task和service都属于资源,那么对待资源应该有统一的处理方式,只需要区分不同的资源执行相应资源的方法就行
package main
import (
"github.com/gin-gonic/gin"
)
// task结构体
type Task struct {
Name string
Description string
}
// 定义task需要的方法
type TaskRegistry struct {
}
func (t *TaskRegistry) ListTasks() ([]Task, error) {
return []Task{{
Name: "task",
Description: "task test",
}}, nil
}
func (t *TaskRegistry) CreateTask(task Task) error {
return nil
}
// service结构体
type Service struct {
Name string
Description string
}
// 定义service需要的方法
type ServiceRegistry struct {
}
func (s *ServiceRegistry) ListServices() ([]Service, error) {
return []Service{{
Name: "service",
Description: "service test",
}}, nil
}
func (s *ServiceRegistry) CreateService(task Service) error {
return nil
}
func main() {
// 初始化 Gin Web 框架
r := gin.Default()
// 注册 API 路由
//r.POST("/tasks", createTaskHandler)
//r.GET("/tasks", listTasksHandler)
//r.GET("/services", listServicesHandler)
//r.POST("/services", createServicesHandler)
r.Any("/resource/:type", restHandler)
// 启动 Web 服务器
r.Run(":8080")
}
func restHandler(c *gin.Context) {
resourceType := c.Param("type")
switch resourceType {
case "task":
if c.Request.Method == "GET" {
taskRegistry := TaskRegistry{}
tasks, err := taskRegistry.ListTasks()
if err != nil {
c.JSON(500, gin.H{"error": "Failed to list tasks"})
return
}
c.JSON(200, tasks)
} else if c.Request.Method == "POST" {
var task Task
if err := c.ShouldBindJSON(&task); err != nil {
c.JSON(400, gin.H{"error": "Bad Request"})
return
}
taskRegistry := TaskRegistry{}
err := taskRegistry.CreateTask(task)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to create task"})
return
}
c.JSON(201, gin.H{"message": "Task created successfully"})
}
case "service":
if c.Request.Method == "GET" {
serviceRegistry := ServiceRegistry{}
services, err := serviceRegistry.ListServices()
if err != nil {
c.JSON(500, gin.H{"error": "Failed to list tasks"})
return
}
c.JSON(200, services)
} else if c.Request.Method == "POST" {
var service Service
if err := c.ShouldBindJSON(&service); err != nil {
c.JSON(400, gin.H{"error": "Bad Request"})
return
}
ServiceRegistry := ServiceRegistry{}
err := ServiceRegistry.CreateService(service)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to create service"})
return
}
c.JSON(201, gin.H{"message": "Service created successfully"})
}
}
}