Skip to content

Commit

Permalink
feat: 基本完成开发
Browse files Browse the repository at this point in the history
  • Loading branch information
dokidokikoi committed Sep 24, 2023
1 parent 194cf39 commit e25e464
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
# 苹果系统的文件
.DS_Store

logs
logs
harukaze.top.crt
harukaze.top.key
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 使用一个基础的 Golang 镜像
FROM golang:alpine as build

# 为我们的镜像设置必要的环境变量
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=arm64 \
GOPROXY=https://goproxy.cn

# 设置工作目录
WORKDIR /app

# 复制 Golang 项目的源代码到容器中
COPY . /app

# 在容器中编译 Golang 项目
RUN go build -o blog cmd/main.go

# 创建最终的生产镜像
FROM alpine:latest as prod

# 设置工作目录
WORKDIR /app

# 从之前的阶段复制二进制文件
COPY --from=build /app/blog /app/
COPY --from=build /app/internal/conf/* /app/internal/conf/

# 设置环境变量等
ENV PORT=18080

# 暴露端口
EXPOSE 18080

# 启动应用
CMD ["./blog"]
39 changes: 39 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3"

services:
backend:
image: blog-backend-image
volumes:
- ./internal/conf:/app/internal/conf
depends_on:
- blog-postgres
- blog-redis
restart: always
networks:
- go-blog-net
frontend:
image: blog-frontend-image
volumes:
- ./nginx:/etc/nginx/conf.d
- ./internal/conf:/app/conf
restart: always
ports:
- 443:443
networks:
- go-blog-net
blog-postgres:
image: postgres:latest
volumes:
- ./pg/data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: 1234567
POSTGRES_USER: go-blog
POSTGRES_DB: blog
networks:
- go-blog-net
blog-redis:
image: redis:latest
networks:
- go-blog-net
networks:
go-blog-net:
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dokidokikoi/go-common v0.0.0-20230912095159-a1ebc80961f7
github.com/dokidokikoi/go-common v0.0.0-20230924114736-97b791174453
github.com/fogleman/gg v1.3.0
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
github.com/gin-gonic/gin v1.9.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
Expand All @@ -66,6 +67,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dokidokikoi/go-common v0.0.0-20230912095159-a1ebc80961f7 h1:ppONsjacHjvqge8i8PkLcoYQHGrZhiNTK/ZgVgWvkQY=
github.com/dokidokikoi/go-common v0.0.0-20230912095159-a1ebc80961f7/go.mod h1:hzKT++8ztimoTeOfeXjDh65USM2gzF+ZND1/uno7ZM4=
github.com/dokidokikoi/go-common v0.0.0-20230924114736-97b791174453 h1:omO+l9jlyq6He8EFXpdVleB6llE29w/XY4mK0zg4wzc=
github.com/dokidokikoi/go-common v0.0.0-20230924114736-97b791174453/go.mod h1:hzKT++8ztimoTeOfeXjDh65USM2gzF+ZND1/uno7ZM4=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -291,6 +294,7 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
Expand Down
24 changes: 22 additions & 2 deletions internal/app.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package application

import (
"crypto/tls"
"go-blog/internal/db/store/data"
"go-blog/internal/router"
"net/http"

"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
)

Expand All @@ -18,5 +19,24 @@ func Run() {
e.Use(gin.Recovery())
router.InstallAll(e)

endless.ListenAndServe(":9080", e)
server := &http.Server{
Addr: ":18080",
Handler: e,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{loadTLSCertificate()},
},
}

err := server.ListenAndServeTLS("", "")
if err != nil {
panic(err)
}
}

func loadTLSCertificate() tls.Certificate {
cert, err := tls.LoadX509KeyPair("./internal/conf/harukaze.top.crt", "./internal/conf/harukaze.top.key")
if err != nil {
panic(err)
}
return cert
}
12 changes: 11 additions & 1 deletion internal/controller/user/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ func (c *Controller) Get(ctx *gin.Context) {

u, err := c.srv.User().Get(ctx, &user.User{ID: uint(userID)}, nil)
if err != nil {
zaplog.L().Error("获取分类信息失败", zap.Error(err))
zaplog.L().Error("获取用户信息失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, nil)
return
}
core.WriteResponse(ctx, nil, u)
}

func (c *Controller) Host(ctx *gin.Context) {
u, err := c.srv.User().Get(ctx, &user.User{ID: uint(1)}, nil)
if err != nil {
zaplog.L().Error("获取站长信息失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, nil)
return
}
Expand Down
1 change: 1 addition & 0 deletions internal/router/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func installUser(r *gin.Engine) {
usersR.POST("/register", userController.Register)
usersR.POST("/login", userController.Login)
usersR.POST("/captcha", userController.GetCaptha)
usersR.GET("/host", userController.Host)
}
}

Expand Down
45 changes: 45 additions & 0 deletions nginx/blog.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
server {
charset utf-8;
client_max_body_size 128M;

listen 443 ssl;
server_name harukaze.top;

ssl_certificate /app/conf/harukaze.top.crt; # 证书文件的路径
ssl_certificate_key /app/conf/harukaze.top.key; # 证书私钥文件的路径

root /usr/share/nginx/html;
index index.html;

# access_log /data/logs/nginx/project.dev.com-access.log;
# error_log /data/logs/nginx/project.dev.com-error.log debug;

location / {
try_files $uri $uri/ /index.html;
}

location /api/ {
proxy_pass https://backend:18080/;
}

location ^~ /backend {
alias /data/www/wwwroot/project/backend/public/;
if (!-e $request_filename) {
rewrite ^ /backend/index.php last;
}
index index.php;
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9000;
}
}

#location @backend {
# rewrite /backend/(.*)$ /backend/index.php/$1 last;
#}
location ~* /\. {
deny all;
}
}

0 comments on commit e25e464

Please sign in to comment.