Skip to content

Commit 0a9c7f7

Browse files
committed
first commit
0 parents  commit 0a9c7f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2149
-0
lines changed

.air.conf

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件
2+
3+
# 工作目录
4+
# 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
5+
root = "."
6+
tmp_dir = "tmp"
7+
8+
[build]
9+
# 只需要写你平常编译使用的shell命令。你也可以使用 `make`
10+
# Windows平台示例:
11+
cmd = "go build -o tmp\main.exe ."
12+
# 由`cmd`命令得到的二进制文件名
13+
# Windows平台示例:
14+
bin = "tmp\main.exe"
15+
# 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release
16+
# Windows平台示例:
17+
full_bin = "tmp\main.exe"
18+
# 监听以下文件扩展名的文件.
19+
include_ext = ["go", "tpl", "tmpl", "html"]
20+
# 忽略这些文件扩展名或目录
21+
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
22+
# 监听以下指定目录的文件
23+
include_dir = []
24+
# 排除以下文件
25+
exclude_file = []
26+
# 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
27+
delay = 1000 # ms
28+
# 发生构建错误时,停止运行旧的二进制文件。
29+
stop_on_error = true
30+
# air的日志文件名,该日志文件放置在你的`tmp_dir`中
31+
log = "air_errors.log"
32+
33+
[log]
34+
# 显示日志时间
35+
time = true
36+
37+
[color]
38+
# 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
39+
main = "magenta"
40+
watcher = "cyan"
41+
build = "yellow"
42+
runner = "green"
43+
44+
[misc]
45+
# 退出时删除tmp目录
46+
clean_on_exit = true

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
####简介
2+
3+
一个简单的博客系统,技术栈为 Gin + Gorm + Vue。项目第一版本,可能存在bug,不推荐直接使用。代码比较易懂,适合学习参考使用。
4+
5+
项目的前端代码参考:https://github.com/SuZiquan/lemon-blog
6+
7+
####实现功能
8+
9+
* 文章发布、修改、展示
10+
* 文章评论
11+
* 标签、分类增删改
12+
* 第三方登录(Github)
13+
* Jwt鉴权
14+
15+
16+

api/front/article.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package front
2+
3+
import (
4+
"gin-blog/dao"
5+
"gin-blog/models"
6+
"net/http"
7+
"strconv"
8+
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
type Article struct{}
13+
14+
// 根据页数,标签id,分类id
15+
func (*Article) ArticlePageNum(c *gin.Context) {
16+
var pageinfo models.PageInfo
17+
pageNum := c.Query("pageNum")
18+
tagIds := c.Query("tagIds")
19+
categoryIds := c.Query("categoryIds")
20+
p, err := strconv.Atoi(pageNum)
21+
if err != nil {
22+
return
23+
}
24+
list, total := dao.GetFrontArticleListByPage(p, tagIds, categoryIds)
25+
pageinfo.Total = total
26+
pageinfo.PageNum = p
27+
pageinfo.PageSize = 10
28+
pageinfo.Data = list
29+
c.JSON(http.StatusOK, gin.H{
30+
"msg": "success",
31+
"data": pageinfo,
32+
})
33+
34+
}
35+
36+
// 归档请求
37+
func (*Article) GetArticlesAll(c *gin.Context) {
38+
list := dao.GetArticlesAll()
39+
c.JSON(http.StatusOK, gin.H{
40+
"code": 200,
41+
"msg": "success",
42+
"data": list,
43+
})
44+
}
45+
46+
// 根据id查文章细节请求
47+
48+
func (*Article) GetArticleDetailsById(c *gin.Context) {
49+
50+
i, err := strconv.Atoi(c.Param("id"))
51+
if err != nil {
52+
return
53+
}
54+
articledetails := dao.GetArticleDetails(i)
55+
articledetails.Previous = dao.GetArticlePrevious(i)
56+
articledetails.Next = dao.GetArticleNext(i)
57+
c.JSON(http.StatusOK, gin.H{
58+
"code": 200,
59+
"msg": "success",
60+
"data": articledetails,
61+
})
62+
}
63+
64+
func (*Article) GetArticleComments(c *gin.Context) {
65+
var pageinfo models.PageInfo
66+
i, err := strconv.Atoi(c.Param("id"))
67+
if err != nil {
68+
return
69+
}
70+
pageNum := c.Query("pageNum")
71+
p, err := strconv.Atoi(pageNum)
72+
if err != nil {
73+
return
74+
}
75+
totle, commentslist := dao.GetArticleCommentsById(i, p)
76+
pageinfo.Total = totle
77+
pageinfo.PageNum = p
78+
pageinfo.PageSize = 10
79+
pageinfo.Data = commentslist
80+
c.JSON(http.StatusOK, gin.H{
81+
"code": 200,
82+
"msg": "success",
83+
"data": pageinfo,
84+
})
85+
}
86+
87+
func (*Article) SearchArticle(c *gin.Context) {
88+
var pageNum, pageSize int
89+
var pageinfo models.PageInfo
90+
p := c.Query("pageNum")
91+
92+
if p == "" {
93+
pageNum = 1
94+
pageSize = 5
95+
} else {
96+
pageSize = 10
97+
pn, err := strconv.Atoi(p)
98+
if err != nil {
99+
return
100+
}
101+
pageNum = pn
102+
103+
}
104+
105+
queryString := c.Query("queryString")
106+
107+
list, i := dao.Search(pageNum, pageSize, queryString)
108+
109+
pageinfo.Total = i
110+
pageinfo.PageNum = pageNum
111+
pageinfo.PageSize = pageSize
112+
pageinfo.Data = list
113+
c.JSON(http.StatusOK, gin.H{
114+
"code": 200,
115+
"msg": "success",
116+
"data": pageinfo,
117+
})
118+
119+
}

api/front/comment.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package front
2+
3+
import (
4+
"gin-blog/dao"
5+
"gin-blog/models"
6+
"net/http"
7+
"strconv"
8+
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
type Comment struct{}
13+
14+
func (*Comment) Save(c *gin.Context) {
15+
16+
var commentreq models.CommentReq
17+
err := c.ShouldBindJSON(&commentreq)
18+
if err != nil {
19+
c.JSON(http.StatusBadRequest, gin.H{
20+
"error": err.Error(),
21+
})
22+
}
23+
i := dao.SaveComment(commentreq)
24+
if i > 0 {
25+
c.JSON(http.StatusOK, gin.H{
26+
"code": 200,
27+
"message": "success",
28+
})
29+
} else {
30+
c.JSON(http.StatusInternalServerError, gin.H{
31+
"code": 500,
32+
"message": "fail",
33+
})
34+
}
35+
36+
}
37+
38+
func (*Comment) DelectComment(c *gin.Context) {
39+
id, err := strconv.Atoi(c.Param("id"))
40+
if err != nil {
41+
return
42+
}
43+
i := dao.DelectCommentById(id)
44+
if i > 0 {
45+
c.JSON(http.StatusOK, gin.H{
46+
"code": 200,
47+
"message": "success",
48+
})
49+
} else {
50+
c.JSON(http.StatusInternalServerError, gin.H{
51+
"code": 500,
52+
"message": "fail",
53+
})
54+
}
55+
56+
}

api/front/oauth.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package front
2+
3+
import (
4+
"gin-blog/config"
5+
"gin-blog/dao"
6+
"gin-blog/models"
7+
"gin-blog/utils"
8+
"net/http"
9+
"strconv"
10+
"time"
11+
12+
"github.com/gin-gonic/gin"
13+
)
14+
15+
type Oauth struct{}
16+
17+
func (*Oauth) Login(c *gin.Context) {
18+
19+
// url := config.AuthCodeURL("state", oauth2.AccessTypeOnline)
20+
authinfo := config.Setup()
21+
22+
url := config.GetUrl(authinfo)
23+
24+
c.JSON(http.StatusOK, gin.H{
25+
"code": 200000,
26+
"message": "success",
27+
"authorizeUrl": url,
28+
})
29+
// c.Redirect(302, url)
30+
}
31+
func (*Oauth) GithubLogincallback(c *gin.Context) {
32+
33+
// code := c.Query("code")
34+
// token, err := config.Exchange(oauth2.NoContext, code)
35+
// if err != nil {
36+
// c.AbortWithError(http.StatusBadRequest, err)
37+
// return
38+
// }
39+
40+
// c.JSON(http.StatusOK, gin.H{
41+
// "access_token": token.AccessToken,
42+
// })
43+
code := c.Query("code")
44+
authinfo := config.Setup()
45+
tok, err := config.GetToken(c, authinfo, code)
46+
if err != nil {
47+
panic(err)
48+
}
49+
client := authinfo.Client(c, tok)
50+
u, err := config.GetUsers(client)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
var userinfo models.User
56+
userinfo.SocialSource = "github"
57+
userinfo.SocialUserId = strconv.Itoa(u.ID)
58+
userinfo.Username = u.Login
59+
userinfo.AvatarUrl = u.AvatarURL
60+
userinfo.Email = u.Email
61+
userinfo.Role = "1"
62+
userinfo.LastLogin = models.MyTime(time.Now())
63+
64+
err = dao.CreatedOrUpdate(&userinfo)
65+
if err != nil {
66+
c.JSON(http.StatusInternalServerError, gin.H{
67+
"code": 500000,
68+
"message": "fail",
69+
})
70+
}
71+
// database.DB.Where("social_source=? and social_user_id=?", userinfo.SocialSource, userinfo.SocialUserId).First(&userinfo)
72+
// if userinfo == {
73+
token, err := utils.GetToken(userinfo)
74+
// }
75+
if err != nil {
76+
c.JSON(http.StatusInternalServerError, gin.H{
77+
"code": 500000,
78+
"message": "fail",
79+
})
80+
}
81+
c.Header("AUTHORIZATION", "Bearer "+token)
82+
83+
// fmt.Println(token)
84+
c.JSON(http.StatusOK, gin.H{
85+
"code": 200000,
86+
"message": "success",
87+
})
88+
89+
}

0 commit comments

Comments
 (0)