作者:社会逼我爱上Money_717 | 来源:互联网 | 2023-09-25 11:44
篇首语:本文由编程笔记#小编为大家整理,主要介绍了#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()
const (
USERNAME = "root"
PASSWORD = "Admin123"
NETWORK = "tcp"
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
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"
"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(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)
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."))
var cpro_id = "u6885494";