Skip to content

Commit 4741553

Browse files
committed
Go 1.18
1 parent 7e8cb61 commit 4741553

Some content is hidden

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

49 files changed

+218
-202
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.17-alpine3.15 as buildbase
1+
FROM golang:1.18-alpine3.15 as buildbase
22

33
WORKDIR /app
44
RUN apk add --no-cache git gcc musl-dev

activityPub.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (a *goBlog) apHandleWebfinger(w http.ResponseWriter, r *http.Request) {
8989
return
9090
}
9191
apIri := a.apIri(blog)
92-
b, _ := json.Marshal(map[string]interface{}{
92+
b, _ := json.Marshal(map[string]any{
9393
"subject": a.webfingerAccts[apIri],
9494
"aliases": []string{
9595
a.webfingerAccts[apIri],
@@ -142,7 +142,7 @@ func (a *goBlog) apHandleInbox(w http.ResponseWriter, r *http.Request) {
142142
return
143143
}
144144
// Parse activity
145-
activity := map[string]interface{}{}
145+
activity := map[string]any{}
146146
err = json.NewDecoder(r.Body).Decode(&activity)
147147
_ = r.Body.Close()
148148
if err != nil {
@@ -164,15 +164,15 @@ func (a *goBlog) apHandleInbox(w http.ResponseWriter, r *http.Request) {
164164
case "Follow":
165165
a.apAccept(blogName, blog, activity)
166166
case "Undo":
167-
if object, ok := activity["object"].(map[string]interface{}); ok {
167+
if object, ok := activity["object"].(map[string]any); ok {
168168
ot := cast.ToString(object["type"])
169169
actor := cast.ToString(object["actor"])
170170
if ot == "Follow" && actor == activityActor {
171171
_ = a.db.apRemoveFollower(blogName, activityActor)
172172
}
173173
}
174174
case "Create":
175-
if object, ok := activity["object"].(map[string]interface{}); ok {
175+
if object, ok := activity["object"].(map[string]any); ok {
176176
baseUrl := cast.ToString(object["id"])
177177
if ou := cast.ToString(object["url"]); ou != "" {
178178
baseUrl = ou
@@ -297,7 +297,7 @@ func (db *database) apRemoveInbox(inbox string) error {
297297

298298
func (a *goBlog) apPost(p *post) {
299299
n := a.toASNote(p)
300-
a.apSendToAllFollowers(p.Blog, map[string]interface{}{
300+
a.apSendToAllFollowers(p.Blog, map[string]any{
301301
"@context": []string{asContext},
302302
"actor": a.apIri(a.cfg.Blogs[p.Blog]),
303303
"id": a.fullPostURL(p),
@@ -308,7 +308,7 @@ func (a *goBlog) apPost(p *post) {
308308
}
309309

310310
func (a *goBlog) apUpdate(p *post) {
311-
a.apSendToAllFollowers(p.Blog, map[string]interface{}{
311+
a.apSendToAllFollowers(p.Blog, map[string]any{
312312
"@context": []string{asContext},
313313
"actor": a.apIri(a.cfg.Blogs[p.Blog]),
314314
"id": a.fullPostURL(p),
@@ -319,7 +319,7 @@ func (a *goBlog) apUpdate(p *post) {
319319
}
320320

321321
func (a *goBlog) apDelete(p *post) {
322-
a.apSendToAllFollowers(p.Blog, map[string]interface{}{
322+
a.apSendToAllFollowers(p.Blog, map[string]any{
323323
"@context": []string{asContext},
324324
"actor": a.apIri(a.cfg.Blogs[p.Blog]),
325325
"type": "Delete",
@@ -328,11 +328,11 @@ func (a *goBlog) apDelete(p *post) {
328328
}
329329

330330
func (a *goBlog) apUndelete(p *post) {
331-
a.apSendToAllFollowers(p.Blog, map[string]interface{}{
331+
a.apSendToAllFollowers(p.Blog, map[string]any{
332332
"@context": []string{asContext},
333333
"actor": a.apIri(a.cfg.Blogs[p.Blog]),
334334
"type": "Undo",
335-
"object": map[string]interface{}{
335+
"object": map[string]any{
336336
"@context": []string{asContext},
337337
"actor": a.apIri(a.cfg.Blogs[p.Blog]),
338338
"type": "Delete",
@@ -341,7 +341,7 @@ func (a *goBlog) apUndelete(p *post) {
341341
})
342342
}
343343

344-
func (a *goBlog) apAccept(blogName string, blog *configBlog, follow map[string]interface{}) {
344+
func (a *goBlog) apAccept(blogName string, blog *configBlog, follow map[string]any) {
345345
// it's a follow, write it down
346346
newFollower := follow["actor"].(string)
347347
log.Println("New follow request:", newFollower)
@@ -365,7 +365,7 @@ func (a *goBlog) apAccept(blogName string, blog *configBlog, follow map[string]i
365365
return
366366
}
367367
// Send accept response to the new follower
368-
accept := map[string]interface{}{
368+
accept := map[string]any{
369369
"@context": []string{asContext},
370370
"type": "Accept",
371371
"to": follow["actor"],
@@ -376,7 +376,7 @@ func (a *goBlog) apAccept(blogName string, blog *configBlog, follow map[string]i
376376
_ = a.db.apQueueSendSigned(a.apIri(blog), inbox, accept)
377377
}
378378

379-
func (a *goBlog) apSendToAllFollowers(blog string, activity interface{}) {
379+
func (a *goBlog) apSendToAllFollowers(blog string, activity any) {
380380
inboxes, err := a.db.apGetAllInboxes(blog)
381381
if err != nil {
382382
log.Println("Failed to retrieve inboxes:", err.Error())
@@ -385,7 +385,7 @@ func (a *goBlog) apSendToAllFollowers(blog string, activity interface{}) {
385385
a.db.apSendTo(a.apIri(a.cfg.Blogs[blog]), activity, inboxes)
386386
}
387387

388-
func (db *database) apSendTo(blogIri string, activity interface{}, inboxes []string) {
388+
func (db *database) apSendTo(blogIri string, activity any, inboxes []string) {
389389
for _, i := range inboxes {
390390
go func(inbox string) {
391391
_ = db.apQueueSendSigned(blogIri, inbox, activity)

activityPubSending.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (a *goBlog) initAPSendQueue() {
4747
})
4848
}
4949

50-
func (db *database) apQueueSendSigned(blogIri, to string, activity interface{}) error {
50+
func (db *database) apQueueSendSigned(blogIri, to string, activity any) error {
5151
body, err := json.Marshal(activity)
5252
if err != nil {
5353
return err

activityStreams.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (a *goBlog) checkActivityStreamsRequest(next http.Handler) http.Handler {
3838
}
3939

4040
type asNote struct {
41-
Context interface{} `json:"@context,omitempty"`
41+
Context any `json:"@context,omitempty"`
4242
To []string `json:"to,omitempty"`
4343
InReplyTo string `json:"inReplyTo,omitempty"`
4444
Name string `json:"name,omitempty"`
@@ -55,7 +55,7 @@ type asNote struct {
5555
}
5656

5757
type asPerson struct {
58-
Context interface{} `json:"@context,omitempty"`
58+
Context any `json:"@context,omitempty"`
5959
ID string `json:"id,omitempty"`
6060
URL string `json:"url,omitempty"`
6161
Type string `json:"type,omitempty"`

blogroll.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/carlmjohnson/requests"
1313
"github.com/kaorimatz/go-opml"
14-
"github.com/thoas/go-funk"
14+
"github.com/samber/lo"
1515
"go.goblog.app/app/pkgs/bufferpool"
1616
"go.goblog.app/app/pkgs/contenttype"
1717
)
@@ -20,7 +20,7 @@ const defaultBlogrollPath = "/blogroll"
2020

2121
func (a *goBlog) serveBlogroll(w http.ResponseWriter, r *http.Request) {
2222
blog, bc := a.getBlog(r)
23-
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (interface{}, error) {
23+
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (any, error) {
2424
return a.getBlogrollOutlines(blog)
2525
})
2626
if err != nil {
@@ -43,7 +43,7 @@ func (a *goBlog) serveBlogroll(w http.ResponseWriter, r *http.Request) {
4343

4444
func (a *goBlog) serveBlogrollExport(w http.ResponseWriter, r *http.Request) {
4545
blog, _ := a.getBlog(r)
46-
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (interface{}, error) {
46+
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (any, error) {
4747
return a.getBlogrollOutlines(blog)
4848
})
4949
if err != nil {
@@ -91,9 +91,9 @@ func (a *goBlog) getBlogrollOutlines(blog string) ([]*opml.Outline, error) {
9191
if len(config.Categories) > 0 {
9292
filtered := []*opml.Outline{}
9393
for _, category := range config.Categories {
94-
if outline, ok := funk.Find(outlines, func(outline *opml.Outline) bool {
94+
if outline, ok := lo.Find(outlines, func(outline *opml.Outline) bool {
9595
return outline.Title == category || outline.Text == category
96-
}).(*opml.Outline); ok && outline != nil {
96+
}); ok && outline != nil {
9797
outline.Outlines = sortOutlines(outline.Outlines)
9898
filtered = append(filtered, outline)
9999
}

blogstats.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (a *goBlog) serveBlogStats(w http.ResponseWriter, r *http.Request) {
3737

3838
func (a *goBlog) serveBlogStatsTable(w http.ResponseWriter, r *http.Request) {
3939
blog, _ := a.getBlog(r)
40-
data, err, _ := a.blogStatsCacheGroup.Do(blog, func() (interface{}, error) {
40+
data, err, _ := a.blogStatsCacheGroup.Do(blog, func() (any, error) {
4141
return a.db.getBlogStats(blog)
4242
})
4343
if err != nil {

cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (a *goBlog) cacheMiddleware(next http.Handler) http.Handler {
7676
// Search and serve cache
7777
key := cacheKey(r)
7878
// Get cache or render it
79-
cacheInterface, _, _ := a.cache.g.Do(key, func() (interface{}, error) {
79+
cacheInterface, _, _ := a.cache.g.Do(key, func() (any, error) {
8080
return a.cache.getCache(key, next, r), nil
8181
})
8282
ci := cacheInterface.(*cacheItem)

check.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/klauspost/compress/gzhttp"
15+
"github.com/samber/lo"
1516
"go.goblog.app/app/pkgs/bufferpool"
1617
"golang.org/x/sync/singleflight"
1718
)
@@ -79,7 +80,7 @@ func (a *goBlog) checkLinks(w io.Writer, posts ...*post) error {
7980
return
8081
}
8182
// Process link
82-
r, err, _ := sg.Do(link.Second, func() (interface{}, error) {
83+
r, err, _ := sg.Do(link.Second, func() (any, error) {
8384
// Check if already cached
8485
if mr, ok := sm.Load(link.Second); ok {
8586
return mr, nil
@@ -131,9 +132,9 @@ func (a *goBlog) allLinks(posts ...*post) (allLinks []*stringPair, err error) {
131132
if err != nil {
132133
return nil, err
133134
}
134-
for _, link := range links {
135-
allLinks = append(allLinks, &stringPair{a.fullPostURL(p), link})
136-
}
135+
allLinks = lo.Map(links, func(s string, _ int) *stringPair {
136+
return &stringPair{a.fullPostURL(p), s}
137+
})
137138
}
138139
return allLinks, nil
139140
}

comments.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type commentsRequestConfig struct {
102102
offset, limit int
103103
}
104104

105-
func buildCommentsQuery(config *commentsRequestConfig) (query string, args []interface{}) {
105+
func buildCommentsQuery(config *commentsRequestConfig) (query string, args []any) {
106106
queryBuilder := bufferpool.Get()
107107
defer bufferpool.Put(queryBuilder)
108108
queryBuilder.WriteString("select id, target, name, website, comment from comments order by id desc")

commentsAdmin.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ import (
55
"net/http"
66
"reflect"
77
"strconv"
8+
"sync"
89

910
"github.com/go-chi/chi/v5"
1011
"github.com/vcraescu/go-paginator"
1112
)
1213

1314
type commentsPaginationAdapter struct {
14-
config *commentsRequestConfig
15-
nums int64
16-
db *database
15+
config *commentsRequestConfig
16+
nums int64
17+
getNums sync.Once
18+
db *database
1719
}
1820

1921
func (p *commentsPaginationAdapter) Nums() (int64, error) {
20-
if p.nums == 0 {
22+
p.getNums.Do(func() {
2123
nums, _ := p.db.countComments(p.config)
2224
p.nums = int64(nums)
23-
}
25+
})
2426
return p.nums, nil
2527
}
2628

27-
func (p *commentsPaginationAdapter) Slice(offset, length int, data interface{}) error {
29+
func (p *commentsPaginationAdapter) Slice(offset, length int, data any) error {
2830
modifiedConfig := *p.config
2931
modifiedConfig.offset = offset
3032
modifiedConfig.limit = length

config.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/url"
88
"strings"
99

10+
"github.com/samber/lo"
1011
"github.com/spf13/viper"
1112
)
1213

@@ -370,9 +371,7 @@ func (a *goBlog) initConfig() error {
370371
if a.cfg.DefaultBlog == "" {
371372
if len(a.cfg.Blogs) == 1 {
372373
// Set default blog to the only blog that is configured
373-
for k := range a.cfg.Blogs {
374-
a.cfg.DefaultBlog = k
375-
}
374+
a.cfg.DefaultBlog = lo.Keys(a.cfg.Blogs)[0]
376375
} else {
377376
return errors.New("no default blog configured")
378377
}

database.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
6666
sql.Register(dbDriverName, &sqlite.SQLiteDriver{
6767
ConnectHook: func(c *sqlite.SQLiteConn) error {
6868
// Register functions
69-
for n, f := range map[string]interface{}{
69+
for n, f := range map[string]any{
7070
"mdtext": a.renderText,
7171
"tolocal": toLocalSafe,
7272
"toutc": toUTCSafe,
@@ -174,14 +174,14 @@ func (db *database) close() error {
174174
return db.db.Close()
175175
}
176176

177-
func (db *database) prepare(query string, args ...interface{}) (*sql.Stmt, []interface{}, error) {
177+
func (db *database) prepare(query string, args ...any) (*sql.Stmt, []any, error) {
178178
if db == nil || db.db == nil {
179179
return nil, nil, errors.New("database not initialized")
180180
}
181181
if len(args) > 0 && args[0] == dbNoCache {
182182
return nil, args[1:], nil
183183
}
184-
stmt, err, _ := db.sg.Do(query, func() (interface{}, error) {
184+
stmt, err, _ := db.sg.Do(query, func() (any, error) {
185185
// Look if statement already exists
186186
st, ok := db.psc.Get(query)
187187
if ok {
@@ -207,11 +207,11 @@ func (db *database) prepare(query string, args ...interface{}) (*sql.Stmt, []int
207207

208208
const dbNoCache = "nocache"
209209

210-
func (db *database) exec(query string, args ...interface{}) (sql.Result, error) {
210+
func (db *database) exec(query string, args ...any) (sql.Result, error) {
211211
return db.execContext(context.Background(), query, args...)
212212
}
213213

214-
func (db *database) execContext(c context.Context, query string, args ...interface{}) (sql.Result, error) {
214+
func (db *database) execContext(c context.Context, query string, args ...any) (sql.Result, error) {
215215
if db == nil || db.db == nil {
216216
return nil, errors.New("database not initialized")
217217
}
@@ -230,11 +230,11 @@ func (db *database) execContext(c context.Context, query string, args ...interfa
230230
return db.db.ExecContext(ctx, query, args...)
231231
}
232232

233-
func (db *database) query(query string, args ...interface{}) (*sql.Rows, error) {
233+
func (db *database) query(query string, args ...any) (*sql.Rows, error) {
234234
return db.queryContext(context.Background(), query, args...)
235235
}
236236

237-
func (db *database) queryContext(c context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
237+
func (db *database) queryContext(c context.Context, query string, args ...any) (rows *sql.Rows, err error) {
238238
if db == nil || db.db == nil {
239239
return nil, errors.New("database not initialized")
240240
}
@@ -253,11 +253,11 @@ func (db *database) queryContext(c context.Context, query string, args ...interf
253253
return
254254
}
255255

256-
func (db *database) queryRow(query string, args ...interface{}) (*sql.Row, error) {
256+
func (db *database) queryRow(query string, args ...any) (*sql.Row, error) {
257257
return db.queryRowContext(context.Background(), query, args...)
258258
}
259259

260-
func (db *database) queryRowContext(c context.Context, query string, args ...interface{}) (row *sql.Row, err error) {
260+
func (db *database) queryRowContext(c context.Context, query string, args ...any) (row *sql.Row, err error) {
261261
if db == nil || db.db == nil {
262262
return nil, errors.New("database not initialized")
263263
}

databaseHooks.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313

1414
const dbHooksBegin contextKey = "begin"
1515

16-
func (db *database) dbBefore(ctx context.Context, _ string, _ ...interface{}) context.Context {
16+
func (db *database) dbBefore(ctx context.Context, _ string, _ ...any) context.Context {
1717
if !db.debug {
1818
return ctx
1919
}
2020
return context.WithValue(ctx, dbHooksBegin, time.Now())
2121
}
2222

23-
func (db *database) dbAfter(ctx context.Context, query string, args ...interface{}) {
23+
func (db *database) dbAfter(ctx context.Context, query string, args ...any) {
2424
if !db.debug {
2525
return
2626
}
@@ -55,7 +55,7 @@ func (db *database) dbAfter(ctx context.Context, query string, args ...interface
5555
bufferpool.Put(logBuilder)
5656
}
5757

58-
func argToString(arg interface{}) string {
58+
func argToString(arg any) string {
5959
val := cast.ToString(arg)
6060
if val == "" {
6161
val = fmt.Sprintf("%v", arg)

0 commit comments

Comments
 (0)