4

新手项目组织的疑惑

 2 years ago
source link: https://www.v2ex.com/t/856138
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Go 编程语言

新手项目组织的疑惑

  dzdh · 3 小时 30 分钟前 · 1063 次点击

新手,用一个月左右看了看语法 demo 啥的各种小 demo 看了看觉得自己行了。

然后想写个项目( ssh 跳板机)练练手,在项目组织上又迷糊了,现在这么组织的

├── go.mod
├── go.sum
├── internal
│   ├── admin
│   │   ├── resources
│   │   │   └── assets
│   │   │       └── FS.go  < FS=embed *.js *.css **/*.js **/*.css
│   │   └── server.go
│   ├── bootstrap.go
│   ├── config
│   │   └── config.go
│   ├── model
│   │   ├── init.go
│   │   └── models.go
│   └── ssh
│       ├── clients.go
│       ├── error.go
│       ├── forward.go
│       ├── handler.go
│       └── server.go
├── main.go

想实现 在 main.go里 调用 bootstrap.setupadmin() bootstrap.setupsshserver() 同时也是为了方便的以后能把 admin 和 sshserver 分离开。

那我在什么时候初始化数据库连接呢?放在 model.init 里? init 方法的话,我在哪传入配置信息呢?(数据库地址啊、密码啊啥的)。现在是 model.init.go 里是 Initialize(cfg *internal.Config.Global)

所以完整的 main.go 是这样的

func main() {
    cfgfilepath = // parseflag
    
    if cfg, err = ioutil.ReadAll(cfgfilepath) err!=nil {panic
    config.Global.LoadConfig(cfg)
    
    model.Initialize(config.Global)
    
    // 支持信号退出用
    context = context.withcancel(...
    
    // 这两个里是 go func..
    model.SetupAdmin(config.Global, context)
    model.SetupSSHServer(config.Global, context)
    
    <- 等待信号
}

这样组织代码有问题吗?怎么做能让 admin 和 sshserver 两个 net.Listen 任意一个有问题就整个退出呢


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK