最佳体验请使用Chrome67及以上版本、火狐、Edge、Safari浏览器 ×

创建银行
创建开票

    GOWEB一键生成神器之模板模式

    作者:雷建明@芯速配 阅读25 2024/09/29 07:06:22 文章 原创 公开


    编辑


    uml:

    编辑

    生成工程

    cd api 

    goweb inst config

    goweb inst api

    cd rpc 

    goweb inst config

    goweb inst rpc

    cd admin

    goweb inst config

    goweb inst admin

    cd web 

    goweb inst config

    goweb inst web

    编辑

    代码:

    基础接口
    
    package icmdface
    
    import "github.com/spf13/cobra"
    
    type IcmdInst interface {
        Setup(cmd *cobra.Command, args []string)
        Do(cmd *cobra.Command, args []string)
        TearDown()
    
        Inst(cmd *cobra.Command, args []string)
    }

    基础结构

    package templatemethod
    
    import (
        "fmt"
        "gitee.com/ichub/goconfig/common/base/basedto"
        "gitee.com/ichub/goconfig/common/base/baseutils/fileutils"
        "gitee.com/ichub/goweb/cmd/goweb/internal/menucmd/instcmd"
        "gitee.com/ichub/goweb/cmd/goweb/internal/menucmd/instcmd/icmdface"
        "github.com/spf13/cobra"
    )
    
    type CmdInst struct {
        basedto.BaseEntitySingle
        Cmdtype string
        Ifrmweb bool
    
    }
    
    func (c CmdInst) Setup(cmd *cobra.Command, args []string) {
        var file = fileutils.FindRootDir() + "/config/env.yml"
        if !fileutils.TryFileExist(file) {
           instcmd.InstallConfigServer(cmd, args)
        }
    }
    func (c CmdInst) Do(cmd *cobra.Command, args []string) {
        fmt.Println("do...")
    }
    
    func (c CmdInst) TearDown() {
        if c.Ifrmweb {
           instcmd.RmWebsample()
        }
    }func (c CmdInst) Inst(cmd *cobra.Command, args []string) {
        c.Setup(cmd, args)
        if icmdinst, ok := c.Proxy().Some.(icmdface.IcmdInst); ok {
           icmdinst.Do(cmd, args)
        } else {
           c.Do(cmd, args)
        }
        c.TearDown()
    }

    CmdInstApi工程生成

    package templatemethod
    
    import (
        "fmt"
        "gitee.com/ichub/goweb/cmd/goweb/internal/menucmd/instcmd"
        "github.com/spf13/cobra"
    )
    
    type CmdInstApi struct {
        CmdInst
    }
    
    func NewCmdInstApi() *CmdInstApi {
        var ca = &CmdInstApi{}
        ca.InitProxy(ca)
        return ca
    }
    
    func (c CmdInstApi) Do(cmd *cobra.Command, args []string) {
        fmt.Println("do...")
        instcmd.InstallWebServer(cmd, args)
        instcmd.InstallCmdServer(cmd, args)
        instcmd.InstallCode(cmd, args)
        instcmd.InstallDocker(cmd, args)
        instcmd.InstallDocx(cmd, args)
        instcmd.InstallDomain(cmd, args)
        instcmd.InstallPbin(cmd, args)
        instcmd.InstallGorpc(cmd, args)
        //installNatsSample(cmd, args)
    
        instcmd.InstallTest(cmd, args)
    }

    CmdInstRpc工程生成

    package templatemethod
    
    import (
        "gitee.com/ichub/goweb/cmd/goweb/internal/menucmd/instcmd"
        "github.com/spf13/cobra"
    )
    
    type CmdInstRpc struct {
        CmdInst
    }
    
    func NewCmdInstRpc() *CmdInstRpc {
        var ca = &CmdInstRpc{}
        ca.InitProxy(ca)
        return ca
    }
    
    func (c CmdInstRpc) Do(cmd *cobra.Command, args []string) {
        instcmd.InstallWebServer(cmd, args)
        instcmd.InstallCmdServer(cmd, args)
        instcmd.InstallDocker(cmd, args)
        instcmd.InstallDomain(cmd, args)
        instcmd.InstallDocx(cmd, args)
        instcmd.InstallTest(cmd, args)
        instcmd.InstallGorpc(cmd, args)
        instcmd.InstallPbin(cmd, args)
        instcmd.InstallNatsSample(cmd, args)
    }

    CmdInstWeb工程生成

    package templatemethod
    
    import (
        "gitee.com/ichub/goweb/cmd/goweb/internal/menucmd/instcmd"
        "github.com/spf13/cobra"
    )
    
    type CmdInstWeb struct {
        CmdInst
    }
    
    func NewCmdInstWeb() *CmdInstWeb {
        var ca = &CmdInstWeb{}
        ca.InitProxy(ca)
        return ca
    }
    
    func (c CmdInstWeb) Do(cmd *cobra.Command, args []string) {
        instcmd.InstallWebServer(cmd, args)
        instcmd.InstallCmdServer(cmd, args)
        instcmd.InstallDocker(cmd, args)
        instcmd.InstallDocx(cmd, args)
        instcmd.InstallTest(cmd, args)
    }

    CmdInstAdmin工程生成

    package templatemethod
    
    import (
        "gitee.com/ichub/goweb/cmd/goweb/internal/menucmd/instcmd"
        "github.com/spf13/cobra"
    )
    
    type CmdInstAdmin struct {
        CmdInst
    }
    
    func NewCmdInstAdmin() *CmdInstAdmin {
        var ca = &CmdInstAdmin{}
        ca.InitProxy(ca)
        return ca
    }
    
    func (c CmdInstAdmin) Do(cmd *cobra.Command, args []string) {
        instcmd.InstallWebServer(cmd, args)
        instcmd.InstallCmdServer(cmd, args)
        instcmd.InstallDocker(cmd, args)
        instcmd.InstallDocx(cmd, args)
        instcmd.InstallTest(cmd, args)
    }


    声明:本网站部分内容来源于网络,版权归原权利人所有,其观点不代表本网站立场;本网站视频或图片制作权归当前商户及其作者,涉及未经授权的制作均须标记“样稿”。如内容侵犯了您相关权利,请及时通过邮箱service@ichub.com与我们联系。
     0  0

    微信扫一扫:分享

    微信里点“+”,扫一扫二维码

    便可将本文分享至朋友圈。

      
    
    
    分享
     0
      验证