数据库准备 postgresql
图形界面 pgAdmin
配置环境变量
| PG_HOME | D:\apptools\postgresql |
|---|---|
| Path | %PG_HOME%\bin |
psql -d chitchat -U postgres
setup.sql文件
-- drop table posts;-- drop table threads;-- drop table sessions;-- drop table users;create table users (id serial primary key,uuid varchar(64) not null unique,name varchar(255),email varchar(255) not null unique,password varchar(255) not null,created_at timestamp not null);create table sessions (id serial primary key,uuid varchar(64) not null unique,email varchar(255),user_id integer references users(id),created_at timestamp not null);create table threads (id serial primary key,uuid varchar(64) not null unique,topic text,user_id integer references users(id),created_at timestamp not null);create table posts (id serial primary key,uuid varchar(64) not null unique,body text,user_id integer references users(id),thread_id integer references threads(id),created_at timestamp not null);
psql -f .\setup.sql -d chitchat -U postgres
开发环境运行
│ .gitignore│ build.bat│ chitchat.log│ go-chitchat.exe│ go.mod│ go.sum│ main.go│ README.en.md│ README.md│ route_auth.go│ route_auth_test.go│ route_main.go│ route_thread.go│ utils.go│├───build│ go-chitchat│├───conf│ config.json│├───data│ data.go│ data_test.go│ setup.sql│ thread.go│ thread_test.go│ user.go│ user_test.go│├───public│ ├───css│ │ bootstrap.min.css│ │ font-awesome.min.css│ │ login.css│ ││ ├───fonts│ │ fontawesome-webfont.eot│ │ fontawesome-webfont.svg│ │ fontawesome-webfont.ttf│ │ fontawesome-webfont.woff│ │ FontAwesome.otf│ ││ └───js│ bootstrap.min.js│ jquery-2.1.1.min.js│└───templateserror.htmlindex.htmllayout.htmllogin.htmllogin.layout.htmlnew.thread.htmlprivate.navbar.htmlprivate.thread.htmlpublic.navbar.htmlpublic.thread.htmlsignup.html
执行命令,编译运行
go run main.go
报错如下
# command-line-arguments.\main.go:9:2: undefined: p.\main.go:9:16: undefined: version.\main.go:9:41: undefined: config.\main.go:13:36: undefined: config.\main.go:22:22: undefined: index.\main.go:24:25: undefined: err.\main.go:27:27: undefined: login.\main.go:28:28: undefined: logout.\main.go:29:28: undefined: signup.\main.go:30:36: undefined: signupAccount.\main.go:30:36: too many errors
问题原因及解决
main 包中的不同的文件的代码不能相互调用,其他包可以。所以其实utils.go等文件是没有被一起编译执行的。
go的多文件加载问题,采用go run命令执行的时候,需要把待加载的.go文件都包含到参数里面。
go run .
linux 部署
路径: /var/goweb/testchitchat/go-chitchat
运行命令
sh build.bat 生成二进制可执行文件 build/go-chitchat
set CGO_ENABLED=0set GOOS=linuxset GOARCH=amd64go build -ldflags "-s -w" -o build/go-chitchat .
sh run.sh 守护运行程序
nohup ./build/go-chitchat conf/config.json > nohup_chitchat.log 2>&1 &
https://cloud.tencent.com/developer/article/1624257
server {listen 80;server_name chitchat.test www.chitchat.test;# 静态资源交由 Nginx 管理,并缓存一天location /static {root /var/www/chitchat/public;expires 1d;add_header Cache-Control public;access_log off;try_files $uri @goweb;}location / {try_files /_not_exists_ @goweb;}# 动态请求默认通过 Go Web 服务器处理location @goweb {proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Scheme $scheme;proxy_redirect off;proxy_pass http://127.0.0.1:8080;}error_log /var/log/nginx/chitchat_error.log;access_log /var/log/nginx/chitchat_access.log;}
