Skip to content

Commit 4a75af7

Browse files
committed
Merge branch 'master' into sam/split-api-handlers
# Conflicts: # app/handler/api.go # app/handler/auth_test.go # app/handler/setup_test.go
2 parents 3150afb + 80a4cae commit 4a75af7

27 files changed

+395
-236
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ Icon
2929
Network Trash Folder
3030
Temporary Items
3131
.apdisk
32+
33+
# Database and key file
34+
dingo.db
35+
dingo.rsa
36+
dingo.rsa.pub
37+
38+
# Ignore upload directory
39+
upload/*
40+

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
**This project is under heavy development and is not ready for use. However, we'd love to have your contribution.**
22

3-
<img width=500px src="https://cloud.githubusercontent.com/assets/1311594/15341604/a5523cba-1c5d-11e6-91e5-ae21a5024da6.png"></img>
3+
![Dingo Logo](https://cloud.githubusercontent.com/assets/1311594/15427334/652081ae-1e62-11e6-9ae3-1dd0a667f22d.png)
4+
5+
[![CI status](https://img.shields.io/travis/dingoblog/dingo.svg)]()
6+
[![Join Dingo Gitter](https://img.shields.io/gitter/room/dingoblog/dingo.svg)](https://gitter.im/dingoblog?utm_source=share-link&utm_medium=link&utm_campaign=share-link)
47

58
Dingo is a full-featured blog engine written in Go.
69

app/handler/admin.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package handler
33
import (
44
"strconv"
55

6+
"github.com/dinever/golf"
67
"github.com/dingoblog/dingo/app/model"
78
"github.com/dingoblog/dingo/app/utils"
8-
"github.com/dinever/golf"
99
)
1010

1111
func AdminHandler(ctx *golf.Context) {
@@ -125,9 +125,15 @@ func ContentSaveHandler(ctx *golf.Context) {
125125
func AdminPostHandler(ctx *golf.Context) {
126126
userObj, _ := ctx.Session.Get("user")
127127
u := userObj.(*model.User)
128-
i, _ := strconv.Atoi(ctx.Request.FormValue("page"))
128+
p := ctx.Request.FormValue("page")
129+
var page int
130+
if p == "" {
131+
page = 1
132+
} else {
133+
page, _ = strconv.Atoi(p)
134+
}
129135
posts := new(model.Posts)
130-
pager, err := posts.GetPostList(int64(i), 10, false, false, "created_at DESC")
136+
pager, err := posts.GetPostList(int64(page), 10, false, false, "created_at DESC")
131137
if err != nil {
132138
panic(err)
133139
}
@@ -186,9 +192,15 @@ func PageCreateHandler(ctx *golf.Context) {
186192
func AdminPageHandler(ctx *golf.Context) {
187193
userObj, _ := ctx.Session.Get("user")
188194
u := userObj.(*model.User)
189-
i, _ := strconv.Atoi(ctx.Request.FormValue("page"))
195+
p := ctx.Request.FormValue("page")
196+
var page int
197+
if p == "" {
198+
page = 1
199+
} else {
200+
page, _ = strconv.Atoi(p)
201+
}
190202
posts := new(model.Posts)
191-
pager, err := posts.GetPostList(int64(i), 10, true, false, `created_at`)
203+
pager, err := posts.GetPostList(int64(page), 10, true, false, `created_at`)
192204
if err != nil {
193205
panic(err)
194206
}
@@ -234,10 +246,16 @@ func PageSaveHandler(ctx *golf.Context) {
234246
}
235247

236248
func CommentViewHandler(ctx *golf.Context) {
237-
i, _ := strconv.Atoi(ctx.Request.FormValue("page"))
238249
user, _ := ctx.Session.Get("user")
250+
p := ctx.Request.FormValue("page")
251+
var page int
252+
if p == "" {
253+
page = 1
254+
} else {
255+
page, _ = strconv.Atoi(p)
256+
}
239257
comments := new(model.Comments)
240-
pager, err := comments.GetCommentList(int64(i), 10, false)
258+
pager, err := comments.GetCommentList(int64(page), 10, false)
241259
if err != nil {
242260
panic(err)
243261
}

app/handler/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package handler
22

33
import (
4-
"github.com/dingoblog/dingo/app/model"
54
"github.com/dinever/golf"
5+
"github.com/dingoblog/dingo/app/model"
66
"regexp"
77
"strconv"
88
)

app/handler/auth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func TestUserLogInWithCorrectInformation(t *testing.T) {
156156

157157
w := httptest.NewRecorder()
158158
app := golf.New()
159-
app = Initialize(app)
159+
app := InitTestApp(app)
160160

161161
req := makeTestHTTPRequest(nil, "GET", "/admin/")
162162
req.Header = http.Header{"Cookie": rec.HeaderMap["Set-Cookie"]}
@@ -165,7 +165,7 @@ func TestUserLogInWithCorrectInformation(t *testing.T) {
165165
ctx.App.ServeHTTP(ctx.Response, ctx.Request)
166166

167167
Convey("Should not redirect to login page ", func() {
168-
So(ctx.StatusCode(), ShouldEqual, 200)
168+
So(ctx.Response.(*httptest.ResponseRecorder).Code, ShouldEqual, 200)
169169
})
170170
})
171171
})

app/handler/file.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package handler
22

33
import (
4-
"github.com/dingoblog/dingo/app/model"
54
"github.com/dinever/golf"
5+
"github.com/dingoblog/dingo/app/model"
6+
"github.com/dingoblog/dingo/app/utils"
67
"io/ioutil"
78
"os"
89
"path"
@@ -104,11 +105,26 @@ func FileUploadHandler(ctx *golf.Context) {
104105
})
105106
return
106107
}
108+
fi, err := os.Stat(Url)
109+
if err != nil {
110+
ctx.JSON(map[string]interface{}{
111+
"status": "error",
112+
"msg": e.Error(),
113+
})
114+
return
115+
}
116+
117+
fSize := utils.FileSize(fi.Size())
118+
fileModTime := fi.ModTime()
119+
fModTime := utils.DateFormat(&fileModTime, "%Y-%m-%d %H:%M")
107120
ctx.JSON(map[string]interface{}{
108121
"status": "success",
109122
"file": map[string]interface{}{
110123
"url": Url,
111124
"name": h.Filename,
125+
"size": fSize,
126+
"type": "File",
127+
"time": fModTime,
112128
},
113129
})
114130
}

app/handler/home.go

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package handler
22

33
import (
4+
"github.com/dinever/golf"
45
"github.com/dingoblog/dingo/app/model"
56
"github.com/dingoblog/dingo/app/utils"
6-
"github.com/dinever/golf"
77
"html/template"
88
"log"
99
"net/url"
@@ -20,17 +20,24 @@ func RegisterFunctions(app *golf.Application) {
2020

2121
func HomeHandler(ctx *golf.Context) {
2222
p := ctx.Param("page")
23-
page, _ := strconv.Atoi(p)
23+
24+
var page int
25+
if p == "" {
26+
page = 1
27+
} else {
28+
page, _ = strconv.Atoi(p)
29+
}
2430
posts := new(model.Posts)
2531
pager, err := posts.GetPostList(int64(page), 5, false, true, "published_at DESC")
2632
if err != nil {
27-
panic(err)
33+
ctx.Abort(404)
34+
return
2835
}
2936
// theme := model.GetSetting("site_theme")
3037
data := map[string]interface{}{
31-
"Title": "Home",
38+
"Title": "Home",
3239
"Posts": posts,
33-
"Pager": pager,
40+
"Pager": pager,
3441
}
3542
// updateSidebarData(data)
3643
ctx.Loader("theme").Render("index.html", data)
@@ -47,7 +54,7 @@ func ContentHandler(ctx *golf.Context) {
4754
post.Hits++
4855
data := map[string]interface{}{
4956
"Title": post.Title,
50-
"Post": post,
57+
"Post": post,
5158
"Content": post,
5259
"Comments": post.Comments,
5360
}
@@ -111,7 +118,14 @@ func CommentHandler(ctx *golf.Context) {
111118

112119
func TagHandler(ctx *golf.Context) {
113120
p := ctx.Param("page")
114-
page, _ := strconv.Atoi(p)
121+
122+
var page int
123+
if p == "" {
124+
page = 1
125+
} else {
126+
page, _ = strconv.Atoi(p)
127+
}
128+
115129
t := ctx.Param("tag")
116130
tagSlug, _ := url.QueryUnescape(t)
117131
tag := &model.Tag{Slug: tagSlug}
@@ -124,9 +138,9 @@ func TagHandler(ctx *golf.Context) {
124138
pager, err := posts.GetPostsByTag(tag.Id, int64(page), 5, true)
125139
data := map[string]interface{}{
126140
"Posts": posts,
127-
"Pager": pager,
128-
"Tag": tag,
129-
"Title": tag.Name,
141+
"Pager": pager,
142+
"Tag": tag,
143+
"Title": tag.Name,
130144
}
131145
ctx.Loader("theme").Render("tag.html", data)
132146
}
@@ -166,7 +180,7 @@ func SiteMapHandler(ctx *golf.Context) {
166180
"Title": model.GetSettingValue("site_title"),
167181
"Link": baseUrl,
168182
"Created": now,
169-
"Posts": articleMap,
183+
"Posts": articleMap,
170184
"Navigators": navMap,
171185
})
172186
}
@@ -188,10 +202,10 @@ func RssHandler(ctx *golf.Context) {
188202
ctx.SetHeader("Content-Type", "text/xml; charset=utf-8")
189203

190204
ctx.Loader("base").Loader("base").Render("rss.xml", map[string]interface{}{
191-
"Title": model.GetSettingValue("site_title"),
192-
"Link": baseUrl,
193-
"Desc": model.GetSettingValue("site_description"),
194-
"Created": utils.Now().Format(time.RFC822),
195-
"Posts": articleMap,
205+
"Title": model.GetSettingValue("site_title"),
206+
"Link": baseUrl,
207+
"Desc": model.GetSettingValue("site_description"),
208+
"Created": utils.Now().Format(time.RFC822),
209+
"Posts": articleMap,
196210
})
197211
}

app/handler/init.go

-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ func Initialize(app *golf.Application) *golf.Application {
3838

3939
func registerFuncMap(app *golf.Application) {
4040
app.View.FuncMap["DateFormat"] = utils.DateFormat
41-
app.View.FuncMap["DateInt64"] = utils.DateInt64
42-
app.View.FuncMap["DateString"] = utils.DateString
43-
app.View.FuncMap["DateTime"] = utils.DateTime
4441
app.View.FuncMap["Now"] = utils.Now
4542
app.View.FuncMap["Html2Str"] = utils.Html2Str
4643
app.View.FuncMap["FileSize"] = utils.FileSize

app/handler/middlewares.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"net/http"
55
"strconv"
66

7-
"github.com/dingoblog/dingo/app/model"
87
"github.com/dinever/golf"
8+
"github.com/dingoblog/dingo/app/model"
99
)
1010

1111
func AuthMiddleware(next golf.HandlerFunc) golf.HandlerFunc {

app/handler/setup_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func mockSignUpPostRequest(email, name, password, rePassword string) *golf.Conte
5151
func mockContext(form url.Values, method, path string) *golf.Context {
5252
w := httptest.NewRecorder()
5353
app := golf.New()
54-
app = Initialize(app)
54+
app = InitTestApp(app)
5555

5656
r := makeTestHTTPRequest(strings.NewReader(form.Encode()), method, path)
5757
r.PostForm = form
@@ -61,7 +61,7 @@ func mockContext(form url.Values, method, path string) *golf.Context {
6161
func mockLogInPostContext() *golf.Context {
6262
w := httptest.NewRecorder()
6363
app := golf.New()
64-
app = Initialize(app)
64+
app = InitTestApp(app)
6565

6666
form := url.Values{}
6767
form.Add("email", email)

app/model/comment.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"database/sql"
55
"time"
66

7+
"fmt"
78
"github.com/dingoblog/dingo/app/utils"
89
"github.com/russross/meddler"
9-
"fmt"
1010
)
1111

1212
type Comments []*Comment
@@ -27,7 +27,7 @@ type Comment struct {
2727
Type string `meddler:"type"`
2828
Parent int64 `meddler:"parent"`
2929
UserId int64 `meddler:"user_id"`
30-
Children *Comments `meddler:"-"`
30+
Children *Comments `meddler:"-"`
3131
}
3232

3333
func (c Comments) Len() int {
@@ -103,12 +103,16 @@ func (comments *Comments) GetCommentList(page, size int64, onlyApproved bool) (*
103103
count, err := GetNumberOfComments()
104104
pager = utils.NewPager(page, size, count)
105105

106+
if !pager.IsValid {
107+
return pager, fmt.Errorf("Page not found")
108+
}
109+
106110
var where string
107111
if onlyApproved {
108112
where = `WHERE approved = 1`
109113
}
110114

111-
err = meddler.QueryAll(db, comments, fmt.Sprintf(stmtGetCommentList, where), size, pager.Begin-1)
115+
err = meddler.QueryAll(db, comments, fmt.Sprintf(stmtGetCommentList, where), size, pager.Begin)
112116
return pager, err
113117
}
114118

@@ -129,7 +133,7 @@ func (comment *Comment) ParentComment() (*Comment, error) {
129133
return parent, parent.GetCommentById()
130134
}
131135

132-
func (comment *Comment) Post() (*Post) {
136+
func (comment *Comment) Post() *Post {
133137
post := NewPost()
134138
post.Id = comment.PostId
135139
post.GetPostById()
@@ -154,9 +158,9 @@ func buildCommentTree(p *Comment, c *Comment, level int) {
154158
}
155159
for _, c := range *childComments {
156160
if level >= 2 {
157-
buildCommentTree(p, c, level + 1)
161+
buildCommentTree(p, c, level+1)
158162
} else {
159-
buildCommentTree(c, c, level + 1)
163+
buildCommentTree(c, c, level+1)
160164
}
161165
}
162166
}

0 commit comments

Comments
 (0)