go-admin示例运行文档

最近有些朋友对之前写的小demo感兴趣,这里介绍下实际的启动步骤。

go-admin示例运行文档

前言

这个系统干嘛的?就是刚开始我按照自己的想法,并且在学习go的时候,参考 vue-admin项目编写的一个运维的cmdb系统,你也可以参考里面的一些设计,虽 然现在看来并不优雅。

后端部分

首先要把后端部分运行起来,这个是基于 gin 的,之所以选择它是因为它比较 简单,也比较全,该有的功能都满足,用的人也比较多。

clone代码

1
2
3
4
5
6
7
8
test git clone git@github.com:liuliancao/go-admin.git
正克隆到 'go-admin'...
remote: Enumerating objects: 102, done.
remote: Counting objects: 100% (102/102), done.
remote: Compressing objects: 100% (67/67), done.
remote: Total 102 (delta 13), reused 101 (delta 12), pack-reused 0 (from 0)
接收对象中: 100% (102/102), 76.24 KiB | 252.00 KiB/s, 完成.
处理 delta 中: 100% (13/13), 完成.

修改配置

注意修改下你要访问的mysql配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
test cd go-admin
➜  go-admin git:(main) cat conf/app.ini 
[database]
Type = mysql
User = op
Password = liuliancao
Host = 127.0.0.1:3306
Name = op
[app]
PageSize = 10
JwtSecret = 111
PrefixUrl = http://localhost:5120

RuntimeRootPath = runtime/

[log]
LogPath = logs/op.log
Development = true
Level = atom
;console orjson
Encoding = console
ServiceName = "op"

导入数据库

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
➜  go-admin git:(main) mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.11.6-MariaDB-0+deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> source op.sql;
Query OK, 1 row affected (0.003 sec)

Database changed
Query OK, 0 rows affected (0.003 sec)

Query OK, 0 rows affected (0.015 sec)

Query OK, 0 rows affected (0.012 sec)

Query OK, 0 rows affected (0.011 sec)

Query OK, 0 rows affected (0.012 sec)

Query OK, 0 rows affected (0.011 sec)

Query OK, 0 rows affected (0.012 sec)

Query OK, 0 rows affected (0.012 sec)

Query OK, 0 rows affected (0.011 sec)

Query OK, 0 rows affected (0.011 sec)

Query OK, 0 rows affected (0.011 sec)

Query OK, 0 rows affected (0.014 sec)

Query OK, 0 rows affected (0.017 sec)

Query OK, 0 rows affected (0.017 sec)

Query OK, 0 rows affected (0.017 sec)

这个时候运行下 go run main.go 生成下基础表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
➜  go-admin git:(main) mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.11.6-MariaDB-0+deb12u1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use op;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [op]> show tables;
+---------------+
| Tables_in_op  |
+---------------+
| app           |
| app_cluster   |
| app_env       |
| app_user      |
| cluster       |
| cluster_host  |
| cluster_user  |
| department    |
| guarder       |
| host          |
| host_deploy   |
| host_ip       |
| macro         |
| product       |
| product_user  |
| resource      |
| resource_type |
| role          |
| role_user     |
| ssh_key       |
| sshkey        |
| tag           |
| tag_app       |
| tag_cluster   |
| tag_host      |
| tag_user      |
| user          |
| user_type     |
+---------------+
28 rows in set (0.000 sec)

MariaDB [op]> source op-init.sql;
Query OK, 1 row affected (0.019 sec)

Query OK, 1 row affected (0.011 sec)

Query OK, 1 row affected (0.020 sec)

Query OK, 1 row affected (0.010 sec)

Query OK, 1 row affected (0.014 sec)

Query OK, 1 row affected (0.023 sec)

Query OK, 1 row affected (0.010 sec)

ERROR 1146 (42S02) at line 32 in file: 'op-init.sql': Table 'op.host_type' doesn't exist
Query OK, 1 row affected (0.010 sec)

Query OK, 1 row affected (0.013 sec)

Query OK, 1 row affected (0.027 sec)

这个时候运行 go run main.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
➜  go-admin git:(main) go run main.go
initing go-admin[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] POST   /auth                     --> go-admin/api/v1.GetAuth (3 handlers)
[GIN-debug] GET    /api/v1/swagger/*any      --> github.com/swaggo/gin-swagger.CustomWrapHandler.func1 (3 handlers)
[GIN-debug] GET    /api/v1/hello             --> go-admin/routers.sayHello (4 handlers)
[GIN-debug] GET    /api/v1/product/:id       --> go-admin/api/v1.GetProduct (4 handlers)
[GIN-debug] POST   /api/v1/product           --> go-admin/api/v1.AddProduct (4 handlers)
[GIN-debug] GET    /api/v1/products          --> go-admin/api/v1.GetProducts (4 handlers)
[GIN-debug] PUT    /api/v1/product/:id       --> go-admin/api/v1.EditProduct (4 handlers)
[GIN-debug] DELETE /api/v1/product/:id       --> go-admin/api/v1.DeleteProduct (4 handlers)
[GIN-debug] GET    /api/v1/productuser       --> go-admin/api/v1.GetProductUsers (4 handlers)
[GIN-debug] POST   /api/v1/productuser       --> go-admin/api/v1.AddProductUser (4 handlers)
[GIN-debug] GET    /api/v1/app/:id           --> go-admin/api/v1.GetApp (4 handlers)
[GIN-debug] POST   /api/v1/app               --> go-admin/api/v1.AddApp (4 handlers)
[GIN-debug] GET    /api/v1/apps              --> go-admin/api/v1.GetApps (4 handlers)
[GIN-debug] PUT    /api/v1/app/:id           --> go-admin/api/v1.EditApp (4 handlers)
[GIN-debug] DELETE /api/v1/app/:id           --> go-admin/api/v1.DeleteApp (4 handlers)
[GIN-debug] GET    /api/v1/appuser           --> go-admin/api/v1.GetAppUsers (4 handlers)
[GIN-debug] POST   /api/v1/appuser           --> go-admin/api/v1.AddAppUser (4 handlers)
[GIN-debug] POST   /api/v1/appcluster        --> go-admin/api/v1.AddAppCluster (4 handlers)
[GIN-debug] GET    /api/v1/appenv/:id        --> go-admin/api/v1.GetAppEnv (4 handlers)
[GIN-debug] POST   /api/v1/appenv            --> go-admin/api/v1.AddAppEnv (4 handlers)
[GIN-debug] GET    /api/v1/appenvs           --> go-admin/api/v1.GetAppEnvs (4 handlers)
[GIN-debug] PUT    /api/v1/appenv/:id        --> go-admin/api/v1.EditAppEnv (4 handlers)
[GIN-debug] DELETE /api/v1/appenv/:id        --> go-admin/api/v1.DeleteAppEnv (4 handlers)
[GIN-debug] GET    /api/v1/department/:id    --> go-admin/api/v1.GetDepartment (4 handlers)
[GIN-debug] POST   /api/v1/department        --> go-admin/api/v1.AddDepartment (4 handlers)
[GIN-debug] GET    /api/v1/departments       --> go-admin/api/v1.GetDepartments (4 handlers)
[GIN-debug] PUT    /api/v1/department/:id    --> go-admin/api/v1.EditDepartment (4 handlers)
[GIN-debug] DELETE /api/v1/department/:id    --> go-admin/api/v1.DeleteDepartment (4 handlers)
[GIN-debug] GET    /api/v1/role/:id          --> go-admin/api/v1.GetRole (4 handlers)
[GIN-debug] POST   /api/v1/role              --> go-admin/api/v1.AddRole (4 handlers)
[GIN-debug] GET    /api/v1/roles             --> go-admin/api/v1.GetRoles (4 handlers)
[GIN-debug] PUT    /api/v1/role/:id          --> go-admin/api/v1.EditRole (4 handlers)
[GIN-debug] DELETE /api/v1/role/:id          --> go-admin/api/v1.DeleteRole (4 handlers)
[GIN-debug] GET    /api/v1/cluster/:id       --> go-admin/api/v1.GetCluster (4 handlers)
[GIN-debug] POST   /api/v1/cluster           --> go-admin/api/v1.AddCluster (4 handlers)
[GIN-debug] GET    /api/v1/clusters          --> go-admin/api/v1.GetClusters (4 handlers)
[GIN-debug] PUT    /api/v1/cluster/:id       --> go-admin/api/v1.EditCluster (4 handlers)
[GIN-debug] DELETE /api/v1/cluster/:id       --> go-admin/api/v1.DeleteCluster (4 handlers)
[GIN-debug] GET    /api/v1/clusteruser       --> go-admin/api/v1.GetClusterUsers (4 handlers)
[GIN-debug] POST   /api/v1/clusteruser       --> go-admin/api/v1.AddClusterUser (4 handlers)
[GIN-debug] POST   /api/v1/cluster/host      --> go-admin/api/v1.AddClusterHost (4 handlers)
[GIN-debug] GET    /api/v1/host/:id          --> go-admin/api/v1.GetHost (4 handlers)
[GIN-debug] POST   /api/v1/host              --> go-admin/api/v1.AddHost (4 handlers)
[GIN-debug] GET    /api/v1/hosts             --> go-admin/api/v1.GetHosts (4 handlers)
[GIN-debug] PUT    /api/v1/host/:id          --> go-admin/api/v1.EditHost (4 handlers)
[GIN-debug] DELETE /api/v1/host/:id          --> go-admin/api/v1.DeleteHost (4 handlers)
[GIN-debug] GET    /api/v1/guarders          --> go-admin/api/v1.GetGuarders (4 handlers)
[GIN-debug] POST   /api/v1/guarder           --> go-admin/api/v1.AddGuarder (4 handlers)
[GIN-debug] PUT    /api/v1/guarder/:id       --> go-admin/api/v1.EditGuarder (4 handlers)
[GIN-debug] DELETE /api/v1/guarder/:id       --> go-admin/api/v1.DeleteGuarder (4 handlers)
[GIN-debug] GET    /api/v1/tag/:id           --> go-admin/api/v1.GetTag (4 handlers)
[GIN-debug] POST   /api/v1/tag               --> go-admin/api/v1.AddTag (4 handlers)
[GIN-debug] GET    /api/v1/tags              --> go-admin/api/v1.GetTags (4 handlers)
[GIN-debug] PUT    /api/v1/tag/:id           --> go-admin/api/v1.EditTag (4 handlers)
[GIN-debug] DELETE /api/v1/tag/:id           --> go-admin/api/v1.DeleteTag (4 handlers)
[GIN-debug] GET    /api/v1/user/:id          --> go-admin/api/v1.GetUser (4 handlers)
[GIN-debug] POST   /api/v1/user              --> go-admin/api/v1.AddUser (4 handlers)
[GIN-debug] GET    /api/v1/users             --> go-admin/api/v1.GetUsers (4 handlers)
[GIN-debug] PUT    /api/v1/user/:id          --> go-admin/api/v1.EditUser (4 handlers)
[GIN-debug] DELETE /api/v1/user/:id          --> go-admin/api/v1.DeleteUser (4 handlers)
[GIN-debug] GET    /api/v1/resource/:id      --> go-admin/api/v1.GetResource (4 handlers)
[GIN-debug] POST   /api/v1/resource          --> go-admin/api/v1.AddResource (4 handlers)
[GIN-debug] GET    /api/v1/resources         --> go-admin/api/v1.GetResources (4 handlers)
[GIN-debug] PUT    /api/v1/resource/:id      --> go-admin/api/v1.EditResource (4 handlers)
[GIN-debug] DELETE /api/v1/resource/:id      --> go-admin/api/v1.DeleteResource (4 handlers)
[GIN-debug] GET    /api/v1/resourcetype/:id  --> go-admin/api/v1.GetResourceType (4 handlers)
[GIN-debug] POST   /api/v1/resourcetype      --> go-admin/api/v1.AddResourceType (4 handlers)
[GIN-debug] GET    /api/v1/resourcetypes     --> go-admin/api/v1.GetResourceTypes (4 handlers)
[GIN-debug] PUT    /api/v1/resourcetype/:id  --> go-admin/api/v1.EditResourceType (4 handlers)
[GIN-debug] DELETE /api/v1/resourcetype/:id  --> go-admin/api/v1.DeleteResourceType (4 handlers)
[GIN-debug] GET    /api/v1/macro/:id         --> go-admin/api/v1.GetMacro (4 handlers)
[GIN-debug] POST   /api/v1/macro             --> go-admin/api/v1.AddMacro (4 handlers)
[GIN-debug] POST   /api/v1/macro/addorupdate --> go-admin/api/v1.AddOrUpdateMacro (4 handlers)
[GIN-debug] GET    /api/v1/macros            --> go-admin/api/v1.GetMacros (4 handlers)
[GIN-debug] PUT    /api/v1/macro/:id         --> go-admin/api/v1.EditMacro (4 handlers)
[GIN-debug] DELETE /api/v1/macro/:id         --> go-admin/api/v1.DeleteMacro (4 handlers)
[GIN-debug] GET    /api/v1/host/deploy/:id   --> go-admin/api/v1.GetHostDeploy (4 handlers)
[GIN-debug] POST   /api/v1/host/deploy       --> go-admin/api/v1.AddHostDeploy (4 handlers)
[GIN-debug] GET    /api/v1/host/deploys      --> go-admin/api/v1.GetHostDeploys (4 handlers)
[GIN-debug] PUT    /api/v1/host/deploy/:id   --> go-admin/api/v1.EditHostDeploy (4 handlers)
[GIN-debug] DELETE /api/v1/host/deploy/:id   --> go-admin/api/v1.DeleteHostDeploy (4 handlers)
2024/09/10 13:33:54 [info] start http server listening 0.0.0.0:5120

登陆测试

1
2
➜  go-admin git:(main) curl -XPOST localhost:5120/auth -d '{"username":"admin","password":"admin"}'
{"code":200,"msg":"ok","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjIxMjMyZjI5N2E1N2E1YTc0Mzg5NGEwZTRhODAxZmMzIiwicGFzc3dvcmQiOiIyMTIzMmYyOTdhNTdhNWE3NDM4OTRhMGU0YTgwMWZjMyIsImV4cCI6MTcyNzAyNjY1MCwiaXNzIjoiZ2luLWJsb2cifQ.6qKfR219aIND-pcpWRe1gXcP3IAej2cJCUoKX5-nblM"}}

ok这个时候你拿到token了,就可以参考 https://github.com/liuliancao/go-admin/blob/main/tests/test.org 这个里 面的测试用例测试了

以sayhello举例

1
2
➜  go-admin git:(main) curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjIxMjMyZjI5N2E1N2E1YTc0Mzg5NGEwZTRhODAxZmMzIiwicGFzc3dvcmQiOiIyMTIzMmYyOTdhNTdhNWE3NDM4OTRhMGU0YTgwMWZjMyIsImV4cCI6MTcyNzAyNjY1MCwiaXNzIjoiZ2luLWJsb2cifQ.6qKfR219aIND-pcpWRe1gXcP3IAej2cJCUoKX5-nblM" -XGET 'localhost:5120/api/v1/hello'
{"message":"hello, golang"}

做到这一步,进行前端的部署

前端部分

前端是用angular写的,如果你不熟悉也不要慌张, typescript 真的很好用, 建议你了解一下,我也是很快就上手了,不过我有一点点angularjs的基础。

clone代码

1
2
3
4
5
6
7
8
test git clone git@github.com:liuliancao/go-admin-angular.git
正克隆到 'go-admin-angular'...
remote: Enumerating objects: 462, done.
remote: Counting objects: 100% (462/462), done.
remote: Compressing objects: 100% (301/301), done.
remote: Total 462 (delta 148), reused 462 (delta 148), pack-reused 0 (from 0)
接收对象中: 100% (462/462), 823.57 KiB | 1.01 MiB/s, 完成.
处理 delta 中: 100% (148/148), 完成.

下载依赖

由于npm这些前端的东西可能有各种各样的问题,并且我这个版本可能比较老, 所以可能多数要google啥的了。

1
2
➜  go-admin-angular git:(main) ✗ npm config set strict-ssl false
➜  go-admin-angular git:(main) ✗ npm install --registry=https://registry.npmmirror.com

如果你懂前端,你也可以考虑自己新建一个前端的代码模块,按需copy过去就好 了。

如果报错 npm ERR! request to https://registry.npm.taobao.org/zrender/download/zrender-5.1.0.tgz failed, reason: certificate has expired 这个了,可以手动下载处理下,或者设置strict-ssl false 后面再改回来

如果报错npm ERR! network request to https://registry.nlark.com/tslib/download/tslib-2.0.3.tgz?cache=0&sync_timestamp=1618847097275&other_urls=https%3A%2F%2Fregistry.nlark.com%2Ftslib%2Fdownload%2Ftslib-2.0.3.tgz failed, reason: getaddrinfo ENOTFOUND registry.nlark.com npm ERR! network This is a problem related to network connectivity. npm ERR! network In most cases you are behind a proxy or have bad network settings. npm ERR! network

去package-lock.json里面,把registry.nlark.com换成 registry.npmmirror.com 再执行npm install

修改部分代码

这个时候假设域名是http://op.test.com

这个时候你需要配置 文件 src/environment/environment.ts 里面的 opBackendUrlhttp://op.test.com

配置nginx

你需要配置一个nginx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
server {
    server_name op.test.com;

    location / {
	    proxy_pass http://localhost:4120;
    }
    location /api {
	    proxy_pass http://localhost:5120;
    }
}

修改hosts

1
127.0.0.1 op.test.com

运行

1
➜  go-admin-angular git:(main) ✗ npm start

打开浏览器

http://op.test.com 这个时候输入admin/admin就可以访问了

这个时候是没有资产的,如果需要,你还需要运行 https://github.com/liuliancao/wagent 这个组件才可以注册

中间我也写过一个guarder, 用于中继wagent的,不过我暂时没放进来

总结

这次的go-admin和go-admin-angular建议大家作为参考可以,如果实际用的话, 由于我这个没有完全开发完,所以需要你去完善剩下的部分