数据库准备 postgresql

图形界面 pgAdmin

配置环境变量

PG_HOME D:\apptools\postgresql
Path %PG_HOME%\bin

psql -d chitchat -U postgres

setup.sql文件

  1. -- drop table posts;
  2. -- drop table threads;
  3. -- drop table sessions;
  4. -- drop table users;
  5. create table users (
  6. id serial primary key,
  7. uuid varchar(64) not null unique,
  8. name varchar(255),
  9. email varchar(255) not null unique,
  10. password varchar(255) not null,
  11. created_at timestamp not null
  12. );
  13. create table sessions (
  14. id serial primary key,
  15. uuid varchar(64) not null unique,
  16. email varchar(255),
  17. user_id integer references users(id),
  18. created_at timestamp not null
  19. );
  20. create table threads (
  21. id serial primary key,
  22. uuid varchar(64) not null unique,
  23. topic text,
  24. user_id integer references users(id),
  25. created_at timestamp not null
  26. );
  27. create table posts (
  28. id serial primary key,
  29. uuid varchar(64) not null unique,
  30. body text,
  31. user_id integer references users(id),
  32. thread_id integer references threads(id),
  33. created_at timestamp not null
  34. );
  1. psql -f .\setup.sql -d chitchat -U postgres

image.png

开发环境运行

  1. .gitignore
  2. build.bat
  3. chitchat.log
  4. go-chitchat.exe
  5. go.mod
  6. go.sum
  7. main.go
  8. README.en.md
  9. README.md
  10. route_auth.go
  11. route_auth_test.go
  12. route_main.go
  13. route_thread.go
  14. utils.go
  15. ├───build
  16. go-chitchat
  17. ├───conf
  18. config.json
  19. ├───data
  20. data.go
  21. data_test.go
  22. setup.sql
  23. thread.go
  24. thread_test.go
  25. user.go
  26. user_test.go
  27. ├───public
  28. ├───css
  29. bootstrap.min.css
  30. font-awesome.min.css
  31. login.css
  32. ├───fonts
  33. fontawesome-webfont.eot
  34. fontawesome-webfont.svg
  35. fontawesome-webfont.ttf
  36. fontawesome-webfont.woff
  37. FontAwesome.otf
  38. └───js
  39. bootstrap.min.js
  40. jquery-2.1.1.min.js
  41. └───templates
  42. error.html
  43. index.html
  44. layout.html
  45. login.html
  46. login.layout.html
  47. new.thread.html
  48. private.navbar.html
  49. private.thread.html
  50. public.navbar.html
  51. public.thread.html
  52. signup.html

执行命令,编译运行

  1. go run main.go

报错如下

  1. # command-line-arguments
  2. .\main.go:9:2: undefined: p
  3. .\main.go:9:16: undefined: version
  4. .\main.go:9:41: undefined: config
  5. .\main.go:13:36: undefined: config
  6. .\main.go:22:22: undefined: index
  7. .\main.go:24:25: undefined: err
  8. .\main.go:27:27: undefined: login
  9. .\main.go:28:28: undefined: logout
  10. .\main.go:29:28: undefined: signup
  11. .\main.go:30:36: undefined: signupAccount
  12. .\main.go:30:36: too many errors

问题原因及解决

main 包中的不同的文件的代码不能相互调用,其他包可以。所以其实utils.go等文件是没有被一起编译执行的。
go的多文件加载问题,采用go run命令执行的时候,需要把待加载的.go文件都包含到参数里面。

  1. go run .

linux 部署

路径: /var/goweb/testchitchat/go-chitchat
image.png
运行命令

  1. sh build.bat 生成二进制可执行文件 build/go-chitchat

    1. set CGO_ENABLED=0
    2. set GOOS=linux
    3. set GOARCH=amd64
    4. go build -ldflags "-s -w" -o build/go-chitchat .
  2. sh run.sh 守护运行程序

    1. nohup ./build/go-chitchat conf/config.json > nohup_chitchat.log 2>&1 &

https://cloud.tencent.com/developer/article/1624257

  1. server {
  2. listen 80;
  3. server_name chitchat.test www.chitchat.test;
  4. # 静态资源交由 Nginx 管理,并缓存一天
  5. location /static {
  6. root /var/www/chitchat/public;
  7. expires 1d;
  8. add_header Cache-Control public;
  9. access_log off;
  10. try_files $uri @goweb;
  11. }
  12. location / {
  13. try_files /_not_exists_ @goweb;
  14. }
  15. # 动态请求默认通过 Go Web 服务器处理
  16. location @goweb {
  17. proxy_set_header Host $http_host;
  18. proxy_set_header X-Real-IP $remote_addr;
  19. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20. proxy_set_header X-Scheme $scheme;
  21. proxy_redirect off;
  22. proxy_pass http://127.0.0.1:8080;
  23. }
  24. error_log /var/log/nginx/chitchat_error.log;
  25. access_log /var/log/nginx/chitchat_access.log;
  26. }