-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (113 loc) · 3.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"database/sql"
"net/http"
"os"
"strings"
"github.com/Wuvist/goblog/models"
"github.com/Wuvist/goblog/static"
"github.com/Wuvist/goblog/tpl"
"github.com/Wuvist/goblog/tpl/skins"
"github.com/volatiletech/sqlboiler/queries/qm"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo"
"github.com/volatiletech/sqlboiler/boil"
)
// run manually, not sure why go generate failed
// esc -o static/css.go -pkg static -include="(.+).css" -prefix="oldweb/" oldweb/Template
//go:generate gorazor skins tpl/skins
func main() {
// db
connstr := os.Getenv("connstr")
db, err := sql.Open("mysql", connstr+"?parseTime=true")
if err != nil {
panic(err)
}
boil.SetDB(db)
// web
e := echo.New()
e.GET("/", home)
e.GET("/blogger.go", blogger)
e.GET("/cate.go", cate)
e.GET("/blog.go", blog)
// static
e.GET("/Template/*", echo.WrapHandler(http.FileServer(static.FS(false))))
// Start server
e.Logger.Fatal(e.Start(":8080"))
}
// Handler
func home(c echo.Context) error {
objs, err := models.BloggersG(qm.Select("blogname", "id", "nick"),
qm.Where("reveal = 1 and blogs > ?", 0),
qm.OrderBy("last_post desc")).All()
if err != nil {
println(err.Error())
}
bloggers := make([]*tpl.Blogger, len(objs))
for i := 0; i < len(objs); i++ {
b := &tpl.Blogger{}
obj := objs[i]
b.Username = obj.ID
b.BlogName = obj.Blogname
b.Nick = obj.Nick.String
bloggers[i] = b
}
page := skins.Home(bloggers)
return c.HTML(http.StatusOK, page)
}
func blogger(c echo.Context) error {
blogerUsername := c.QueryParam("blogger")
bloggerData, err := models.BloggersG(qm.Where("id = ?", blogerUsername)).One()
if err != nil {
println(err.Error())
}
if bloggerData == nil {
return c.String(http.StatusNotFound, "找不到博客")
}
blogger := tpl.NewBloggerFromDb(bloggerData)
blogs := tpl.GetBlogSummariesFromBlogger(bloggerData.Index)
page := skins.Skin5_default(blogger, blogs)
return c.HTML(http.StatusOK, page)
}
func cate(c echo.Context) error {
blogerUsername := strings.ToLower(c.QueryParam("blogger"))
cateID := c.QueryParam("cate_id")
cateData, _ := models.UserdefinecategoriesG(qm.Where("`index` = ?", cateID)).One()
if cateData == nil {
return c.String(http.StatusNotFound, "找不到网志分类")
}
bloggerData, _ := models.BloggersG(qm.Where("`index` = ?", cateData.Blogger)).One()
if bloggerData == nil {
return c.String(http.StatusNotFound, "找不到博客")
}
blogger := tpl.NewBloggerFromDb(bloggerData)
if strings.ToLower(blogger.Username) != blogerUsername {
return c.String(http.StatusNotFound, "找不到分类")
}
cate := &tpl.Cate{}
cate.CateID = cateData.Index
cate.CateName = cateData.Cate
blogs := tpl.GetBlogSummariesFromCate(cate.CateID)
page := skins.Skin5_UserCate(blogger, cate, blogs)
return c.HTML(http.StatusOK, page)
}
func blog(c echo.Context) error {
blogerUsername := strings.ToLower(c.QueryParam("blogger"))
blogID := c.QueryParam("article_id")
blogData, err := models.ArticlesG(qm.Where("`index` = ?", blogID)).One()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
bloggerData, _ := models.BloggersG(qm.Where("`index` = ?", blogData.Blogger)).One()
if bloggerData == nil {
return c.String(http.StatusNotFound, "找不到博客")
}
blogger := tpl.NewBloggerFromDb(bloggerData)
if strings.ToLower(blogger.Username) != blogerUsername {
return c.String(http.StatusNotFound, "找不到网志")
}
blog := tpl.NewBlogFromDb(blogData)
comments := tpl.GetBlogComments(blogData.Index)
page := skins.Skin5_comment(blogger, blog, comments)
return c.HTML(http.StatusOK, page)
}