Skip to content

Commit

Permalink
move all api to v1 group and suport embed web ui to server
Browse files Browse the repository at this point in the history
1. move all api to v1 group
2. web ui adapt all api to v1 group
3. support embed web ui to server if needed
  • Loading branch information
alimy committed Jun 2, 2022
1 parent 047ab9f commit f9c407d
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 60 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
BUILD_VERSION := $(shell cat version)
BUILD_DATE := $(shell date +'%Y-%m-%d %H:%M:%S')
SHA_SHORT := $(shell git rev-parse --short HEAD)
TAGS = ""
all: fmt build
build:
@go mod download
@echo Build paopao-ce
bash build.sh paopao-ce
run:
@go run -ldflags "-X 'main.version=${BUILD_VERSION}' -X 'main.buildDate=${BUILD_DATE}' -X 'main.commitID=${SHA_SHORT}'" .
@go run -tags '$(TAGS)' -ldflags "-X 'main.version=${BUILD_VERSION}' -X 'main.buildDate=${BUILD_DATE}' -X 'main.commitID=${SHA_SHORT}'" .
clean:
@go clean
@find ./dist -type f -exec rm -r {} +
Expand All @@ -23,6 +24,10 @@ fmt:
@go vet -composites=false ./pkg/...
help:
@echo "make: make"
@echo "make run: start api server"
@echo "make build: build executables"
@echo "make build: build executables"
@echo "make run TAGS='embed': start api server and serve embed web frontend"
@echo "make build TAGS='embed': build executables with embed web frontend"
.EXPORT_ALL_VARIABLES:
GO111MODULE = on
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,25 @@ PaoPao主要由以下优秀的开源项目/工具构建

1. 导入项目根目录下的 `paopao.sql` 文件至MySQL数据库
2. 拷贝项目根目录下 `config.yaml.sample` 文件至 `config.yaml`,按照注释完成配置编辑
3. 编译后端

3. 编译后端
编译api服务:
```sh
go mod download
go build -o paopao-api .
make build
```
编译api服务、内嵌web前端ui; 注意此步骤需要先编译web前端。
```sh
make build TAGS='embed'
```
编译后在`dist`目录可以找到对应可执行文件。

4. 启动后端

4. 启动后端
运行api服务:
```sh
make run
```
运行api服务、web前端ui服务:
```sh
chmod +x paopao-api
./paopao-api
make run TAGS='embed'
```

#### 前端
Expand Down
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ for pl in ${PLATFORMS}; do

echo "build => ${TARGET}"
if [ "${DEBUG_MODE}" == "debug" ]; then
go build -trimpath -gcflags "all=-N -l" -o "${TARGET}" \
go build -trimpath -gcflags "all=-N -l" -o "${TARGET}" -tags "${TAGS}" \
-ldflags "-X 'main.version=${BUILD_VERSION}' \
-X 'main.buildDate=${BUILD_DATE}' \
-X 'main.commitID=${SHA_SHORT}'\
-w -s"
else
go build -trimpath -o "${TARGET}" \
go build -trimpath -o "${TARGET}" -tags "${TAGS}" \
-ldflags "-X 'main.version=${BUILD_VERSION}' \
-X 'main.buildDate=${BUILD_DATE}' \
-X 'main.commitID=${SHA_SHORT}'\
Expand Down
22 changes: 14 additions & 8 deletions internal/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import (
)

func NewRouter() *gin.Engine {
r := gin.New()
r.HandleMethodNotAllowed = true
r.Use(gin.Logger())
r.Use(gin.Recovery())
e := gin.New()
e.HandleMethodNotAllowed = true
e.Use(gin.Logger())
e.Use(gin.Recovery())

// 跨域配置
corsConfig := cors.DefaultConfig()
corsConfig.AllowAllOrigins = true
corsConfig.AddAllowHeaders("Authorization")
r.Use(cors.New(corsConfig))
e.Use(cors.New(corsConfig))

// 按需注册静态资源路由
registerStatick(e)

// v1 group api
r := e.Group("/v1")

// 获取version
r.GET("/", api.Version)
Expand Down Expand Up @@ -163,18 +169,18 @@ func NewRouter() *gin.Engine {

}
// 默认404
r.NoRoute(func(c *gin.Context) {
e.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"msg": "Not Found",
})
})
// 默认405
r.NoMethod(func(c *gin.Context) {
e.NoMethod(func(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, gin.H{
"code": 405,
"msg": "Method Not Allowed",
})
})
return r
return e
}
13 changes: 13 additions & 0 deletions internal/routers/statick.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !embed
// +build !embed

package routers

import (
"github.com/gin-gonic/gin"
)

// registerStatick stub function for register static asset route
func registerStatick(e *gin.Engine) {
// empty
}
27 changes: 27 additions & 0 deletions internal/routers/statick_embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build embed
// +build embed

package routers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/web"
)

// registerStatick register static assets route
func registerStatick(e *gin.Engine) {
routeStatic(e, "/", "/index.html", "/favicon.ico", "/assets/*filepath")
}

func routeStatic(e *gin.Engine, paths ...string) {
staticHandler := http.FileServer(web.NewFileSystem())
handler := func(c *gin.Context) {
staticHandler.ServeHTTP(c.Writer, c.Request)
}
for _, path := range paths {
e.GET(path, handler)
e.HEAD(path, handler)
}
}
1 change: 1 addition & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
dist
dist-ssr
*.local
.env

# Editor directories and files
.vscode/*
Expand Down
19 changes: 19 additions & 0 deletions web/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build embed
// +build embed

package web

import (
"embed"
"io/fs"
"net/http"
)

//go:embed dist/*
var files embed.FS

// NewFileSystem get an embed static assets http.FileSystem instance.
func NewFileSystem() http.FileSystem {
subfs, _ := fs.Sub(files, "dist")
return http.FS(subfs)
}
8 changes: 4 additions & 4 deletions web/src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { request } from '@/utils/request';
export const userLogin = (params: NetParams.AuthUserLogin): Promise<NetReq.AuthUserLogin> => {
return request({
method: 'post',
url: '/auth/login',
url: '/v1/auth/login',
data: params,
});
};
Expand All @@ -13,7 +13,7 @@ export const userLogin = (params: NetParams.AuthUserLogin): Promise<NetReq.AuthU
export const userRegister = (params: NetParams.AuthUserRegister): Promise<NetReq.AuthUserRegister> => {
return request({
method: 'post',
url: '/auth/register',
url: '/v1/auth/register',
data: params,
});
};
Expand All @@ -22,7 +22,7 @@ export const userRegister = (params: NetParams.AuthUserRegister): Promise<NetReq
export const userInfo = (token: NetParams.AuthUserInfo = ""): Promise<NetReq.AuthUserInfo> => {
return request({
method: 'get',
url: '/user/info',
url: '/v1/user/info',
headers: {
Authorization: `Bearer ${token}`,
},
Expand All @@ -33,7 +33,7 @@ export const userInfo = (token: NetParams.AuthUserInfo = ""): Promise<NetReq.Aut
export const updateUserPassword = (data: NetParams.AuthUpdateUserPassword): Promise<NetReq.AuthUpdateUserPassword> => {
return request({
method: 'post',
url: '/api/user/password',
url: '/v1/api/user/password',
data,
});
};
32 changes: 16 additions & 16 deletions web/src/api/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { request } from '@/utils/request';
export const getPosts = (params: NetParams.PostGetPosts): Promise<NetReq.PostGetPosts> => {
return request({
method: 'get',
url: '/posts',
url: '/v1/posts',
params
});
};
Expand All @@ -13,7 +13,7 @@ export const getPosts = (params: NetParams.PostGetPosts): Promise<NetReq.PostGet
export const getTags = (params: NetParams.PostGetTags): Promise<NetReq.PostGetTags> => {
return request({
method: 'get',
url: '/tags',
url: '/v1/tags',
params
});
};
Expand All @@ -22,7 +22,7 @@ export const getTags = (params: NetParams.PostGetTags): Promise<NetReq.PostGetTa
export const getPost = (params: NetParams.PostGetPost): Promise<NetReq.PostGetPost> => {
return request({
method: 'get',
url: '/post',
url: '/v1/post',
params
});
};
Expand All @@ -31,7 +31,7 @@ export const getPost = (params: NetParams.PostGetPost): Promise<NetReq.PostGetPo
export const getPostStar = (params: NetParams.PostPostStar): Promise<NetReq.PostGetPostStar> => {
return request({
method: 'get',
url: '/post/star',
url: '/v1/post/star',
params
});
};
Expand All @@ -40,7 +40,7 @@ export const getPostStar = (params: NetParams.PostPostStar): Promise<NetReq.Post
export const postStar = (data: NetParams.PostPostStar): Promise<NetReq.PostPostStar> => {
return request({
method: 'post',
url: '/post/star',
url: '/v1/post/star',
data
});
};
Expand All @@ -49,7 +49,7 @@ export const postStar = (data: NetParams.PostPostStar): Promise<NetReq.PostPostS
export const getPostCollection = (params: NetParams.PostGetPostCollection): Promise<NetReq.PostGetPostCollection> => {
return request({
method: 'get',
url: '/post/collection',
url: '/v1/post/collection',
params
});
};
Expand All @@ -58,7 +58,7 @@ export const getPostCollection = (params: NetParams.PostGetPostCollection): Prom
export const postCollection = (data: NetParams.PostPostCollection): Promise<NetReq.PostPostCollection> => {
return request({
method: 'post',
url: '/post/collection',
url: '/v1/post/collection',
data
});
};
Expand All @@ -67,7 +67,7 @@ export const postCollection = (data: NetParams.PostPostCollection): Promise<NetR
export const getPostComments = (params: NetParams.PostGetPostComments): Promise<NetReq.PostGetPostComments> => {
return request({
method: 'get',
url: '/post/comments',
url: '/v1/post/comments',
params
});
};
Expand All @@ -76,7 +76,7 @@ export const getPostComments = (params: NetParams.PostGetPostComments): Promise<
export const createPost = (data: NetParams.PostCreatePost): Promise<NetReq.PostCreatePost> => {
return request({
method: 'post',
url: '/post',
url: '/v1/post',
data
});
};
Expand All @@ -85,7 +85,7 @@ export const createPost = (data: NetParams.PostCreatePost): Promise<NetReq.PostC
export const deletePost = (data: NetParams.PostDeletePost): Promise<NetReq.PostDeletePost> => {
return request({
method: 'delete',
url: '/post',
url: '/v1/post',
data
});
};
Expand All @@ -94,7 +94,7 @@ export const deletePost = (data: NetParams.PostDeletePost): Promise<NetReq.PostD
export const lockPost = (data: NetParams.PostLockPost): Promise<NetReq.PostLockPost> => {
return request({
method: 'post',
url: '/post/lock',
url: '/v1/post/lock',
data
});
};
Expand All @@ -103,7 +103,7 @@ export const lockPost = (data: NetParams.PostLockPost): Promise<NetReq.PostLockP
export const stickPost = (data: NetParams.PostStickPost): Promise<NetReq.PostStickPost> => {
return request({
method: 'post',
url: '/post/stick',
url: '/v1/post/stick',
data
});
};
Expand All @@ -112,7 +112,7 @@ export const stickPost = (data: NetParams.PostStickPost): Promise<NetReq.PostSti
export const createComment = (data: NetParams.PostCreateComment): Promise<NetReq.PostCreateComment> => {
return request({
method: 'post',
url: '/post/comment',
url: '/v1/post/comment',
data
});
};
Expand All @@ -121,7 +121,7 @@ export const createComment = (data: NetParams.PostCreateComment): Promise<NetReq
export const deleteComment = (data: NetParams.PostDeleteComment): Promise<NetReq.PostDeleteComment> => {
return request({
method: 'delete',
url: '/post/comment',
url: '/v1/post/comment',
data
});
};
Expand All @@ -130,7 +130,7 @@ export const deleteComment = (data: NetParams.PostDeleteComment): Promise<NetReq
export const createCommentReply = (data: NetParams.PostCreateCommentReply): Promise<NetReq.PostCreateCommentReply> => {
return request({
method: 'post',
url: '/post/comment/reply',
url: '/v1/post/comment/reply',
data
});
};
Expand All @@ -139,7 +139,7 @@ export const createCommentReply = (data: NetParams.PostCreateCommentReply): Prom
export const deleteCommentReply = (data: NetParams.PostDeleteCommentReply): Promise<NetReq.PostDeleteCommentReply> => {
return request({
method: 'delete',
url: '/post/comment/reply',
url: '/v1/post/comment/reply',
data
});
};
Loading

0 comments on commit f9c407d

Please sign in to comment.