mattermost的server启动流程分为发布方式以及研发方式启动.这里将研发的方式启动.
先来熟悉一下mattermost的一些
Some useful
make
commands:
- make run will run the server, symlink your mattermost-webapp folder and start a watcher for the web app
- make stop stops the server and the web app watcher
- make run-server will run only the server and not the client
- make debug-server will run the server in the delve debugger
- make stop-server stops only the server
- make clean-docker stops and removes your Docker images and is a good way to wipe your database
- make clean cleans your local environment of temporary files
- make nuke wipes your local environment back to a completely fresh start
- make package creates packages for distributing your builds and puts them in the ~/go/src/github.com/mattermost/mattermost-server/dist directory. First you will need to run make build and make build-client.
- make megacheck runs the tool megacheck against the code base to find potential issues in the code. Please note the results are guidelines, and not mandatory in all cases. If in doubt, ask in the Developers community channel.
从官方的说明中可以看出,使用make run-server可以去启动服务端的运行.那我们先看一下make文件的写法:
run-server: start-docker ## Starts the server.
@echo Running mattermost for development
@echo goflags-----$(GOFLAGS)
@echo golinkerfile-------$(GO_LINKER_FLAGS)
@echo goplatformfile-------$(PLATFORM_FILES)
mkdir -p $(BUILD_WEBAPP_DIR)/dist/files
$(GO) run $(GOFLAGS) $(GO_LINKER_FLAGS) $(PLATFORM_FILES) --disableconfigwatch &
这个是run-server的目标,可以看出在做了一系列的准备工作之后使用go run的方式启动
那么真正执行的是那个文件呢,经过打印翻译过来其实是下面的命令:
go run -ldflags "-X github.com/mattermost/mattermost-server/model.BuildNumber=dev -X 'github.com/mattermost/mattermost-server/model.BuildDate=Mon Feb 18 07:46:00 UTC 2019' -X github.com/mattermost/mattermost-server/model.BuildHash=10f4a0fde307f594fb3eb7a3d4ebd8ec2c948f00 -X github.com/mattermost/mattermost-server/model.BuildHashEnterprise=none -X github.com/mattermost/mattermost-server/model.BuildEnterpriseReady=false" "./cmd/mattermost/main.go" --disableconfigwatch &
加了一大串的dflags 然后就是执行的文件了.可以看出执行的文件为:./cmd/mattermost/main.go执行的是这个文件.
再看这个文件的实现:
func
main
() {
if
err
:= commands.
Run
(os.Args[
1
:]); err !=
nil
{
os.
Exit
(
1
)
}
}
其实很简单就是执行了Run 方法,这个方法是在root.go中实现的,代码如下:
func
Run
(args []
string
)
error
{
RootCmd.
SetArgs
(args)
return
RootCmd.
Execute
()
}
这个执行RootCmd这个命令,命令的定义如下:
var
RootCmd
= &cobra.Command{
Use:
"mattermost"
,
Short:
"Open source, self-hosted Slack-alternative"
,
Long:
`Mattermost offers workplace messaging across web, PC and phones with archiving, search and integration with your existing systems. Documentation available at https://docs.mattermost.com`
,
}
大家可以看看cobra.Command的实现,可以发现这个其实去执行他的RunE这个方法,但是这里并没有给它赋值,这个其实是在另外一个地方做的,这个是在server.go中赋值的:
func
init
() {
mlog.
Info
(
"--------------------server command"
)
RootCmd.
AddCommand
(serverCmd)
RootCmd.RunE
= serverCmdF
}
这个init是在程序运行时进行初始化的,所以在Run执行前就以及初始化好了的.所以当程序执行Run之后就会执行这个serverCmdF这个方法.
后面就是server的初始化了,在下一篇再进行分析