在查找golang的学习资料时发现了一个教程。 [techschool/simplebank](https://github.com/techschool/simplebank)。 从开发工具到业务代码都讲得非常详细,学到很多实战技巧。 比如DBML的[**dbdiagram.io**](https://dbdiagram.io/home) postgres数据库设计神器。 如果有写好的sql可以直接导入,调整好外键再一键导出。 也因此放弃了使用[supabase](https://app.supabase.io/) 的打算,因为设计schema的时候很麻烦。 (但是自动生成的api文档又特别棒) 教程中推荐了**TablePlus**,一个mac端的postgres客户端。 一直觉得对比MySQL的workbench,pgadmin的web使用体验很不错。 TablePlus刚好弥补了这个遗憾,而且可以直接在查询结果的数据表格中进行修改/新增,Ctrl+S保存,完全可以当个简易后台了。 ※免费版本只能开两个tab。 另外有推荐 [sqlc](https://github.com/kyleconroy/sqlc),从sql直接生成Go的接口代码。 大概试了下,感觉简单的逻辑手写也差不多。 推测object和field很多的情况下用起来会比较方便吧。 以前只在做C++测试的时候接触过`Makefile` 没想到还可以当作部署脚本 用起来很方便 ## Dockerize 教程中只把postgres的部分容器化了。 研究了下迁移用的库[golang-migrate] 在容器里的安装实现,发现原来有提供镜像可以直接新建容器。 - postgres:`docker run --name postgres12 -p 5432:5432 -e POSTGRES_USER=$(DB_USER) -e POSTGRES_PASSWORD=$(DB_PASS) -d postgres:12-alpine` - gin - Go Dockerfile 参考: https://tutorialedge.net/golang/go-docker-tutorial/ - migration: ↓ ```bash docker run --rm -d -v $(current_dir)/db/migration:/migrations migrate/migrate:v4.14.1 \ -path=/migrations/ -database "postgresql://$(DB_USER):$(DB_PASS)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=disable" -verbose up ``` 关于docker的一些规则这篇文章讲得浅显易懂[Best Practices for Writing a Dockerfile](https://blog.bitsrc.io/best-practices-for-writing-a-dockerfile-68893706c3) 1. Identify Cacheable Units: Dockerfile里的`RUN`尽量写一起,否则可能生成多个缓存层。 2. Reduce Image Size ✂: `RUN apt-get update && apt-get -y install --no-install-recommends`, 用 [bit](https://bit.dev/) 复用component 3. Image Maintainability: 用官方image,指定Tags 不用latest,有alpine就上alpine, 另外学会用Multi Staged Builds ```Dockerfile # Stage 0, "build-stage", based on Node.js, to build and compile the frontend FROM node:13.12.0 as build-stage WORKDIR /app COPY package*.json /app/ RUN npm install COPY ./ /app/ RUN npm run build # Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx FROM nginx:1.15 COPY --from=build-stage /app/build/ /usr/share/nginx/html ``` ## GitOps - [Go github Actions](https://sosedoff.com/2019/02/12/go-github-actions.html) 利用github action来实现自动测试。要实现自动部署则需要在目标服务器运行github action runner。