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的初始化了,在下一篇再進行分析