#yyds干货盘点#golang快速部署restful项目
Posted 峰啊疯了
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#yyds干货盘点#golang快速部署restful项目相关的知识,希望对你有一定的参考价值。
1.开发环境
golang编译器下载链接
安装go开发环境请看官网
go安装
2.新建项目
3.项目结构
cmd:主函数启动
main.go
package main
import (
_ "container_cloud/pkg/config"
"container_cloud/pkg/utils/httputil"
"container_cloud/routers"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/http"
"time"
)
func init()
httputil.InitHttpTool()
// mysql
const (
USERNAME = "root"
PASSWORD = "Admin123"
NETWORK = "tcp"
// TODO 本地调试时放开
/*SERVER = "192.168.103.48"
PORT = 43306*/
// TODO 部署到环境时放开
SERVER = "192.168.66.4"
PORT = 3306
DATABASE = "container_cloud"
)
func main()
var err error
dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s?parseTime=1&multiStatements=1&charset=utf8mb4&collation=utf8mb4_unicode_ci", USERNAME, PASSWORD, NETWORK, SERVER, PORT, DATABASE)
db, err := sql.Open("mysql", dsn)
if err != nil
fmt.Printf("Open mysql failed,err:%v\\n", err)
return
//最大连接周期,超过时间的连接就close
db.SetConnMaxLifetime(100 * time.Second)
//设置最大连接数
db.SetMaxOpenConns(100)
//设置闲置连接数
db.SetMaxIdleConns(16)
defer db.Close()
container := routers.InitApiRouter(db)
server := &http.ServerAddr: ":8090", Handler: container
server.ListenAndServe()
conf:配置文件
routers:路由注册
package routers
import (
clusterv1alpha1 "container_cloud/pkg/apis/cluster/v1alpha1"
configv1alpha1 "container_cloud/pkg/apis/config/v1alpha1"
deployv1alpha1 "container_cloud/pkg/apis/deployment/v1alpha1"
image_v1alpha1 "container_cloud/pkg/apis/image/v1alpha1"
job_v1alpha1 "container_cloud/pkg/apis/job/v1alpha1"
nsv1alpha1 "container_cloud/pkg/apis/namespace/v1alpha1"
orderv1alpha1 "container_cloud/pkg/apis/order/v1alpha1"
podv1alpha1 "container_cloud/pkg/apis/pod/v1alpha1"
resource_v1alpha1 "container_cloud/pkg/apis/resource"
svcv1 "container_cloud/pkg/apis/service/v1alpha1"
// pvc_v1alpha1 "container_cloud/pkg/apis/storage/persistentvolumeclaim/v1alpha1"
"container_cloud/pkg/apiserver/filters"
"container_cloud/pkg/domain/resources/v1alpha3/resource"
"database/sql"
"github.com/emicklei/go-restful"
)
func InitApiRouter(db *sql.DB) *restful.Container
container := restful.NewContainer()
resourceGetter := resource.NewResourceGetter()
container.Add(deployv1alpha1.AddToContainer())
// container.Add(pvc_v1alpha1.AddToContainer())
container.Add(svcv1.AddToContainer())
container.Add(clusterv1alpha1.AddToContainer())
container.Add(nsv1alpha1.AddToContainer())
container.Add(image_v1alpha1.AddToContainer(db))
container.Add(orderv1alpha1.AddToContainer(db))
container.Add(resource_v1alpha1.AddToContainer(resourceGetter))
container.Add(configv1alpha1.AddToContainer())
container.Add(podv1alpha1.AddToContainer())
container.Add(job_v1alpha1.AddToContainer())
container.Filter(filters.Authorization)
return container
把每个模块注册进来
访问入口对应的register.go(带数据库)
package v1alpha1
import (
"container_cloud/pkg/api"
"container_cloud/pkg/apiserver/query"
"container_cloud/pkg/apiserver/runtime"
"container_cloud/pkg/controller"
"container_cloud/pkg/domain"
"database/sql"
"github.com/emicklei/go-restful"
"k8s.io/apimachinery/pkg/runtime/schema"
"net/http"
)
const (
GroupName = "order.ictnj.io"
Version = "v1alpha1"
)
var GroupVersion = schema.GroupVersionGroup: GroupName, Version: Version
func AddToContainer(db *sql.DB) *restful.WebService
ws := runtime.NewWebService(GroupVersion)
orderController := controller.NewOrderController(db)
// 创建订单接口,pvc创建、负载创建的时候,是在特定命名空间下。(其实请求入参中也有命名空间字段,资源创建的时候也可以从入参中获取)
ws.Route(ws.POST("/namespaces/namespace/orders").
To(orderController.CreateOrder).
Param(ws.PathParameter("namespace", "namespace name")).
Returns(http.StatusOK, api.StatusOK, map[string]string).
Doc("create order."))
#yyds干货盘点#如何进行快速进行yapi迁移#yyds干货盘点#Golang 模板(text/template)
#yyds干货盘点#Golang 模板(text/template)