repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
b128cb9
parent
b77aa87
author
Eric Bower
date
2022-07-29 13:50:49 +0000 UTC
chore: cleanup imports
27 files changed,  +80, -76
M cmd/lists/ssh/main.go
+6, -6
 1@@ -26,7 +26,7 @@ func (me *SSHServer) authHandler(ctx ssh.Context, key ssh.PublicKey) bool {
 2 	return true
 3 }
 4 
 5-func createRouter(handler *internal.DbHandler) proxy.Router {
 6+func createRouter(handler *lists.DbHandler) proxy.Router {
 7 	return func(sh ssh.Handler, s ssh.Session) []wish.Middleware {
 8 		cmd := s.Command()
 9 		mdw := []wish.Middleware{}
10@@ -44,7 +44,7 @@ func createRouter(handler *internal.DbHandler) proxy.Router {
11 	}
12 }
13 
14-func withProxy(handler *internal.DbHandler) ssh.Option {
15+func withProxy(handler *lists.DbHandler) ssh.Option {
16 	return func(server *ssh.Server) error {
17 		err := sftp.SSHOption(handler)(server)
18 		if err != nil {
19@@ -56,13 +56,13 @@ func withProxy(handler *internal.DbHandler) ssh.Option {
20 }
21 
22 func main() {
23-	host := internal.GetEnv("PROSE_HOST", "0.0.0.0")
24-	port := internal.GetEnv("PROSE_SSH_PORT", "2222")
25-	cfg := internal.NewConfigSite()
26+	host := lists.GetEnv("PROSE_HOST", "0.0.0.0")
27+	port := lists.GetEnv("PROSE_SSH_PORT", "2222")
28+	cfg := lists.NewConfigSite()
29 	logger := cfg.Logger
30 	dbh := postgres.NewDB(&cfg.ConfigCms)
31 	defer dbh.Close()
32-	handler := internal.NewDbHandler(dbh, cfg)
33+	handler := lists.NewDbHandler(dbh, cfg)
34 
35 	sshServer := &SSHServer{}
36 	s, err := wish.NewServer(
M cmd/lists/web/main.go
+1, -1
1@@ -3,5 +3,5 @@ package main
2 import "git.sr.ht/~erock/pico/lists"
3 
4 func main() {
5-	internal.StartApiServer()
6+	lists.StartApiServer()
7 }
M cmd/migrate/migrate.go
+3, -5
 1@@ -131,11 +131,9 @@ func main() {
 2 	if err != nil {
 3 		panic(err)
 4 	}
 5-	rollback := func() {
 6-		// Defer a rollback in case anything fails.
 7-		defer tx.Rollback()
 8-	}
 9-	defer rollback()
10+	defer func() {
11+		err = tx.Rollback()
12+	}()
13 
14 	logger.Info("Finding prose users")
15 	proseUsers, err := proseDb.FindUsers()
M cmd/pastes/ssh/main.go
+6, -6
 1@@ -26,7 +26,7 @@ func (me *SSHServer) authHandler(ctx ssh.Context, key ssh.PublicKey) bool {
 2 	return true
 3 }
 4 
 5-func createRouter(handler *internal.DbHandler) proxy.Router {
 6+func createRouter(handler *pastes.DbHandler) proxy.Router {
 7 	return func(sh ssh.Handler, s ssh.Session) []wish.Middleware {
 8 		cmd := s.Command()
 9 		mdw := []wish.Middleware{}
10@@ -44,7 +44,7 @@ func createRouter(handler *internal.DbHandler) proxy.Router {
11 	}
12 }
13 
14-func withProxy(handler *internal.DbHandler) ssh.Option {
15+func withProxy(handler *pastes.DbHandler) ssh.Option {
16 	return func(server *ssh.Server) error {
17 		err := sftp.SSHOption(handler)(server)
18 		if err != nil {
19@@ -56,13 +56,13 @@ func withProxy(handler *internal.DbHandler) ssh.Option {
20 }
21 
22 func main() {
23-	host := internal.GetEnv("PROSE_HOST", "0.0.0.0")
24-	port := internal.GetEnv("PROSE_SSH_PORT", "2222")
25-	cfg := internal.NewConfigSite()
26+	host := pastes.GetEnv("PROSE_HOST", "0.0.0.0")
27+	port := pastes.GetEnv("PROSE_SSH_PORT", "2222")
28+	cfg := pastes.NewConfigSite()
29 	logger := cfg.Logger
30 	dbh := postgres.NewDB(&cfg.ConfigCms)
31 	defer dbh.Close()
32-	handler := internal.NewDbHandler(dbh, cfg)
33+	handler := pastes.NewDbHandler(dbh, cfg)
34 
35 	sshServer := &SSHServer{}
36 	s, err := wish.NewServer(
M cmd/pastes/web/main.go
+1, -1
1@@ -3,5 +3,5 @@ package main
2 import "git.sr.ht/~erock/pico/pastes"
3 
4 func main() {
5-	internal.StartApiServer()
6+	pastes.StartApiServer()
7 }
M cmd/prose/ssh/main.go
+6, -6
 1@@ -26,7 +26,7 @@ func (me *SSHServer) authHandler(ctx ssh.Context, key ssh.PublicKey) bool {
 2 	return true
 3 }
 4 
 5-func createRouter(handler *internal.DbHandler) proxy.Router {
 6+func createRouter(handler *prose.DbHandler) proxy.Router {
 7 	return func(sh ssh.Handler, s ssh.Session) []wish.Middleware {
 8 		cmd := s.Command()
 9 		mdw := []wish.Middleware{}
10@@ -44,7 +44,7 @@ func createRouter(handler *internal.DbHandler) proxy.Router {
11 	}
12 }
13 
14-func withProxy(handler *internal.DbHandler) ssh.Option {
15+func withProxy(handler *prose.DbHandler) ssh.Option {
16 	return func(server *ssh.Server) error {
17 		err := sftp.SSHOption(handler)(server)
18 		if err != nil {
19@@ -56,13 +56,13 @@ func withProxy(handler *internal.DbHandler) ssh.Option {
20 }
21 
22 func main() {
23-	host := internal.GetEnv("PROSE_HOST", "0.0.0.0")
24-	port := internal.GetEnv("PROSE_SSH_PORT", "2222")
25-	cfg := internal.NewConfigSite()
26+	host := prose.GetEnv("PROSE_HOST", "0.0.0.0")
27+	port := prose.GetEnv("PROSE_SSH_PORT", "2222")
28+	cfg := prose.NewConfigSite()
29 	logger := cfg.Logger
30 	dbh := postgres.NewDB(&cfg.ConfigCms)
31 	defer dbh.Close()
32-	handler := internal.NewDbHandler(dbh, cfg)
33+	handler := prose.NewDbHandler(dbh, cfg)
34 
35 	sshServer := &SSHServer{}
36 	s, err := wish.NewServer(
M cmd/prose/web/main.go
+1, -1
1@@ -3,5 +3,5 @@ package main
2 import "git.sr.ht/~erock/pico/prose"
3 
4 func main() {
5-	internal.StartApiServer()
6+	prose.StartApiServer()
7 }
M lists/api.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package lists
4 
5 import (
6 	"bytes"
M lists/config.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package lists
4 
5 import (
6 	"fmt"
M lists/db_handler.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package lists
4 
5 import (
6 	"fmt"
M lists/gemini/gemini.go
+27, -27
  1@@ -23,7 +23,7 @@ import (
  2 	"golang.org/x/exp/slices"
  3 )
  4 
  5-func renderTemplate(cfg *internal.ConfigSite, templates []string) (*template.Template, error) {
  6+func renderTemplate(cfg *lists.ConfigSite, templates []string) (*template.Template, error) {
  7 	files := make([]string, len(templates))
  8 	copy(files, templates)
  9 	files = append(
 10@@ -52,7 +52,7 @@ func createPageHandler(fname string) gemini.HandlerFunc {
 11 			return
 12 		}
 13 
 14-		data := internal.PageData{
 15+		data := lists.PageData{
 16 			Site: *cfg.GetSiteData(),
 17 		}
 18 		err = ts.Execute(w, data)
 19@@ -93,13 +93,13 @@ func blogHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request
 20 		return
 21 	}
 22 
 23-	headerTxt := &internal.HeaderTxt{
 24-		Title: internal.GetBlogName(username),
 25+	headerTxt := &lists.HeaderTxt{
 26+		Title: lists.GetBlogName(username),
 27 		Bio:   "",
 28 	}
 29-	readmeTxt := &internal.ReadmeTxt{}
 30+	readmeTxt := &lists.ReadmeTxt{}
 31 
 32-	postCollection := make([]internal.PostItemData, 0, len(posts))
 33+	postCollection := make([]lists.PostItemData, 0, len(posts))
 34 	for _, post := range posts {
 35 		if post.Filename == "_header" {
 36 			parsedText := pkg.ParseText(post.Text)
 37@@ -123,20 +123,20 @@ func blogHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request
 38 				readmeTxt.HasItems = true
 39 			}
 40 		} else {
 41-			p := internal.PostItemData{
 42+			p := lists.PostItemData{
 43 				URL:            html.URL(cfg.PostURL(post.Username, post.Filename)),
 44 				BlogURL:        html.URL(cfg.BlogURL(post.Username)),
 45-				Title:          internal.FilenameToTitle(post.Filename, post.Title),
 46+				Title:          lists.FilenameToTitle(post.Filename, post.Title),
 47 				PublishAt:      post.PublishAt.Format("02 Jan, 2006"),
 48 				PublishAtISO:   post.PublishAt.Format(time.RFC3339),
 49-				UpdatedTimeAgo: internal.TimeAgo(post.UpdatedAt),
 50+				UpdatedTimeAgo: lists.TimeAgo(post.UpdatedAt),
 51 				UpdatedAtISO:   post.UpdatedAt.Format(time.RFC3339),
 52 			}
 53 			postCollection = append(postCollection, p)
 54 		}
 55 	}
 56 
 57-	data := internal.BlogPageData{
 58+	data := lists.BlogPageData{
 59 		Site:      *cfg.GetSiteData(),
 60 		PageTitle: headerTxt.Title,
 61 		URL:       html.URL(cfg.BlogURL(username)),
 62@@ -186,7 +186,7 @@ func readHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request
 63 		prevPage = fmt.Sprintf("/read?page=%d", page-1)
 64 	}
 65 
 66-	data := internal.ReadPageData{
 67+	data := lists.ReadPageData{
 68 		Site:     *cfg.GetSiteData(),
 69 		NextPage: nextPage,
 70 		PrevPage: prevPage,
 71@@ -194,22 +194,22 @@ func readHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request
 72 
 73 	longest := 0
 74 	for _, post := range pager.Data {
 75-		size := len(internal.TimeAgo(post.UpdatedAt))
 76+		size := len(lists.TimeAgo(post.UpdatedAt))
 77 		if size > longest {
 78 			longest = size
 79 		}
 80 	}
 81 
 82 	for _, post := range pager.Data {
 83-		item := internal.PostItemData{
 84+		item := lists.PostItemData{
 85 			URL:            html.URL(cfg.PostURL(post.Username, post.Filename)),
 86 			BlogURL:        html.URL(cfg.BlogURL(post.Username)),
 87-			Title:          internal.FilenameToTitle(post.Filename, post.Title),
 88+			Title:          lists.FilenameToTitle(post.Filename, post.Title),
 89 			Description:    post.Description,
 90 			Username:       post.Username,
 91 			PublishAt:      post.PublishAt.Format("02 Jan, 2006"),
 92 			PublishAtISO:   post.PublishAt.Format(time.RFC3339),
 93-			UpdatedTimeAgo: internal.TimeAgo(post.UpdatedAt),
 94+			UpdatedTimeAgo: lists.TimeAgo(post.UpdatedAt),
 95 			UpdatedAtISO:   post.UpdatedAt.Format(time.RFC3339),
 96 		}
 97 
 98@@ -240,7 +240,7 @@ func postHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request
 99 	}
100 
101 	header, _ := dbpool.FindPostWithFilename("_header", user.ID, cfg.Space)
102-	blogName := internal.GetBlogName(username)
103+	blogName := lists.GetBlogName(username)
104 	if header != nil {
105 		headerParsed := pkg.ParseText(header.Text)
106 		if headerParsed.MetaData.Title != "" {
107@@ -271,14 +271,14 @@ func postHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request
108 		logger.Error(err)
109 	}
110 
111-	data := internal.PostPageData{
112+	data := lists.PostPageData{
113 		Site:         *cfg.GetSiteData(),
114-		PageTitle:    internal.GetPostTitle(post),
115+		PageTitle:    lists.GetPostTitle(post),
116 		URL:          html.URL(cfg.PostURL(post.Username, post.Filename)),
117 		BlogURL:      html.URL(cfg.BlogURL(username)),
118 		Description:  post.Description,
119 		ListType:     parsedText.MetaData.ListType,
120-		Title:        internal.FilenameToTitle(post.Filename, post.Title),
121+		Title:        lists.FilenameToTitle(post.Filename, post.Title),
122 		PublishAt:    post.PublishAt.Format("02 Jan, 2006"),
123 		PublishAtISO: post.PublishAt.Format(time.RFC3339),
124 		Username:     username,
125@@ -327,7 +327,7 @@ func transparencyHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini
126 		return
127 	}
128 
129-	data := internal.TransparencyPageData{
130+	data := lists.TransparencyPageData{
131 		Site:      *cfg.GetSiteData(),
132 		Analytics: analytics,
133 	}
134@@ -367,8 +367,8 @@ func rssBlogHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Requ
135 		return
136 	}
137 
138-	headerTxt := &internal.HeaderTxt{
139-		Title: internal.GetBlogName(username),
140+	headerTxt := &lists.HeaderTxt{
141+		Title: lists.GetBlogName(username),
142 	}
143 
144 	for _, post := range posts {
145@@ -396,12 +396,12 @@ func rssBlogHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Requ
146 
147 	var feedItems []*feeds.Item
148 	for _, post := range posts {
149-		if slices.Contains(internal.HiddenPosts, post.Filename) {
150+		if slices.Contains(lists.HiddenPosts, post.Filename) {
151 			continue
152 		}
153 		parsed := pkg.ParseText(post.Text)
154 		var tpl bytes.Buffer
155-		data := &internal.PostPageData{
156+		data := &lists.PostPageData{
157 			ListType: parsed.MetaData.ListType,
158 			Items:    parsed.Items,
159 		}
160@@ -411,7 +411,7 @@ func rssBlogHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Requ
161 
162 		item := &feeds.Item{
163 			Id:      cfg.PostURL(post.Username, post.Filename),
164-			Title:   internal.FilenameToTitle(post.Filename, post.Title),
165+			Title:   lists.FilenameToTitle(post.Filename, post.Title),
166 			Link:    &feeds.Link{Href: cfg.PostURL(post.Username, post.Filename)},
167 			Content: tpl.String(),
168 			Created: *post.PublishAt,
169@@ -473,7 +473,7 @@ func rssHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request)
170 	for _, post := range pager.Data {
171 		parsed := pkg.ParseText(post.Text)
172 		var tpl bytes.Buffer
173-		data := &internal.PostPageData{
174+		data := &lists.PostPageData{
175 			ListType: parsed.MetaData.ListType,
176 			Items:    parsed.Items,
177 		}
178@@ -511,7 +511,7 @@ func rssHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request)
179 }
180 
181 func StartServer() {
182-	cfg := internal.NewConfigSite()
183+	cfg := lists.NewConfigSite()
184 	db := postgres.NewDB(&cfg.ConfigCms)
185 	logger := cfg.Logger
186 
M lists/gemini/router.go
+3, -3
 1@@ -19,8 +19,8 @@ func GetLogger(ctx context.Context) *zap.SugaredLogger {
 2 	return ctx.Value(ctxLoggerKey{}).(*zap.SugaredLogger)
 3 }
 4 
 5-func GetCfg(ctx context.Context) *internal.ConfigSite {
 6-	return ctx.Value(ctxCfgKey{}).(*internal.ConfigSite)
 7+func GetCfg(ctx context.Context) *lists.ConfigSite {
 8+	return ctx.Value(ctxCfgKey{}).(*lists.ConfigSite)
 9 }
10 
11 func GetDB(ctx context.Context) db.DB {
12@@ -46,7 +46,7 @@ func NewRoute(pattern string, handler gemini.HandlerFunc) Route {
13 
14 type ServeFn func(context.Context, gemini.ResponseWriter, *gemini.Request)
15 
16-func CreateServe(routes []Route, cfg *internal.ConfigSite, dbpool db.DB, logger *zap.SugaredLogger) ServeFn {
17+func CreateServe(routes []Route, cfg *lists.ConfigSite, dbpool db.DB, logger *zap.SugaredLogger) ServeFn {
18 	return func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
19 		curRoutes := routes
20 
M lists/router.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package lists
4 
5 import (
6 	"context"
M lists/util.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package lists
4 
5 import (
6 	"encoding/base64"
M pastes/api.go
+5, -2
 1@@ -1,4 +1,4 @@
 2-package internal
 3+package pastes
 4 
 5 import (
 6 	"fmt"
 7@@ -305,7 +305,10 @@ func postHandlerRaw(w http.ResponseWriter, r *http.Request) {
 8 	}
 9 
10 	w.Header().Set("Content-Type", "text/plain")
11-	w.Write([]byte(post.Text))
12+	_, err = w.Write([]byte(post.Text))
13+	if err != nil {
14+		logger.Error(err)
15+	}
16 }
17 
18 func transparencyHandler(w http.ResponseWriter, r *http.Request) {
M pastes/config.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package pastes
4 
5 import (
6 	"fmt"
M pastes/cron.go
+5, -2
 1@@ -1,4 +1,4 @@
 2-package internal
 3+package pastes
 4 
 5 import (
 6 	"time"
 7@@ -32,7 +32,10 @@ func deleteExpiredPosts(cfg *ConfigSite, dbpool db.DB) error {
 8 
 9 func CronDeleteExpiredPosts(cfg *ConfigSite, dbpool db.DB) {
10 	for {
11-		deleteExpiredPosts(cfg, dbpool)
12+		err := deleteExpiredPosts(cfg, dbpool)
13+		if err != nil {
14+			cfg.Logger.Error(err)
15+		}
16 		time.Sleep(1 * time.Hour)
17 	}
18 }
M pastes/db_handler.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package pastes
4 
5 import (
6 	"fmt"
M pastes/parser.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package pastes
4 
5 import (
6 	"bytes"
M pastes/router.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package pastes
4 
5 import (
6 	"context"
M pastes/util.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package pastes
4 
5 import (
6 	"encoding/base64"
M prose/api.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package prose
4 
5 import (
6 	"bytes"
M prose/config.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package prose
4 
5 import (
6 	"fmt"
M prose/db_handler.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package prose
4 
5 import (
6 	"fmt"
M prose/parser.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package prose
4 
5 import (
6 	"bytes"
M prose/router.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package prose
4 
5 import (
6 	"context"
M prose/util.go
+1, -1
1@@ -1,4 +1,4 @@
2-package internal
3+package prose
4 
5 import (
6 	"encoding/base64"