Compare commits

...

5 Commits

Author SHA1 Message Date
Eric Bower d53ceba7ee
docs: update readme 2022-07-13 23:40:10 -04:00
Eric Bower 23e186d46c
chore: update wish
supply protocol from environment variable (http or https)
2022-07-13 23:37:17 -04:00
Eric Bower 407f38cd7c
chore: update wish 2022-07-13 23:14:50 -04:00
Eric Bower 865f5c2054
chore(cron): add some logging 2022-07-13 23:05:05 -04:00
Eric Bower c867fb74e9
feat: pseudo cron to delete posts after 3 days 2022-07-13 22:58:50 -04:00
7 changed files with 66 additions and 5 deletions

8
.env.example Normal file
View File

@ -0,0 +1,8 @@
POSTGRES_PASSWORD="secret"
DATABASE_URL="postgresql://postgres:secret@localhost/pastes?sslmode=disable"
PASTES_SSH_PORT="2222"
PASTES_WEB_PORT="3000"
PASTES_DOMAIN="pastes.test:3000"
PASTES_EMAIL="me@pastey.com"
PASTES_SUBDOMAINS=1
PASTES_PROTOCOL="http"

View File

@ -23,6 +23,7 @@ export PASTES_SSH_PORT=2222
export PASTES_WEB_PORT=3000
export PASTES_DOMAIN="pastes.sh"
export PASTES_EMAIL="hello@pastes.sh"
export PASTES_PROTOCOL="http"
```
I just use `direnv` which will load my `.env` file.

4
go.mod
View File

@ -2,8 +2,10 @@ module git.sr.ht/~erock/pastes.sh
go 1.18
// replace git.sr.ht/~erock/wish => /home/erock/pico/wish
require (
git.sr.ht/~erock/wish v0.0.0-20220713141740-64595ee518ac
git.sr.ht/~erock/wish v0.0.0-20220714033621-620d4ba1aa5d
github.com/alecthomas/chroma v0.10.0
github.com/charmbracelet/wish v0.5.0
github.com/gliderlabs/ssh v0.3.4

8
go.sum
View File

@ -2,6 +2,14 @@ git.sr.ht/~erock/wish v0.0.0-20220707194507-66e938674d95 h1:q3G01ELBZt3EZEOiYWfw
git.sr.ht/~erock/wish v0.0.0-20220707194507-66e938674d95/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
git.sr.ht/~erock/wish v0.0.0-20220713141740-64595ee518ac h1:d+q9VPi+kaZpYpZOoXPSEx2KtZ3Gcmkz+x/lpb/V6bU=
git.sr.ht/~erock/wish v0.0.0-20220713141740-64595ee518ac/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
git.sr.ht/~erock/wish v0.0.0-20220714010731-7947bf10c388 h1:goL6fa09OPdg+LB2ceJGlx4QkPO+42QI/zqB6tbaKAA=
git.sr.ht/~erock/wish v0.0.0-20220714010731-7947bf10c388/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
git.sr.ht/~erock/wish v0.0.0-20220714031334-670ce9b92be6 h1:MXCqjuHgZ081oVyBb4JO6jMyKLA3HTsvOVJJBFF3L/Y=
git.sr.ht/~erock/wish v0.0.0-20220714031334-670ce9b92be6/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
git.sr.ht/~erock/wish v0.0.0-20220714031736-47b0eaae6231 h1:o5oGYrwl3Qpug5DnjPa3+m/EsWhN2zZveYqHQVvbUe4=
git.sr.ht/~erock/wish v0.0.0-20220714031736-47b0eaae6231/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
git.sr.ht/~erock/wish v0.0.0-20220714033621-620d4ba1aa5d h1:M/Ub98kMgfAZ/4jjbSLsc58TLqYGYzmMr0bxsSyUvgc=
git.sr.ht/~erock/wish v0.0.0-20220714033621-620d4ba1aa5d/go.mod h1:QZKk7m9jc9iXah90daPGhQkSfNfxSVvpb6nfVeI+MM0=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=

View File

@ -482,6 +482,8 @@ func StartApiServer() {
defer db.Close()
logger := cfg.Logger
go CronDeleteExpiredPosts(cfg, db)
staticRoutes := createStaticRoutes()
mainRoutes := createMainRoutes(staticRoutes)
subdomainRoutes := createSubdomainRoutes(staticRoutes)

View File

@ -28,6 +28,7 @@ func NewConfigSite() *ConfigSite {
subdomains := GetEnv("PASTES_SUBDOMAINS", "0")
port := GetEnv("PASTES_WEB_PORT", "3000")
dbURL := GetEnv("DATABASE_URL", "")
protocol := GetEnv("PASTES_PROTOCOL", "https")
subdomainsEnabled := false
if subdomains == "1" {
subdomainsEnabled = true
@ -44,6 +45,7 @@ func NewConfigSite() *ConfigSite {
ConfigCms: config.ConfigCms{
Domain: domain,
Port: port,
Protocol: protocol,
Email: email,
DbURL: dbURL,
Description: "a pastebin for hackers.",
@ -63,7 +65,7 @@ func (c *ConfigSite) GetSiteData() *SitePageData {
func (c *ConfigSite) BlogURL(username string) string {
if c.IsSubdomains() {
return fmt.Sprintf("//%s.%s", username, c.Domain)
return fmt.Sprintf("%s://%s.%s", c.Protocol, username, c.Domain)
}
return fmt.Sprintf("/%s", username)
@ -72,7 +74,7 @@ func (c *ConfigSite) BlogURL(username string) string {
func (c *ConfigSite) PostURL(username, filename string) string {
fname := url.PathEscape(filename)
if c.IsSubdomains() {
return fmt.Sprintf("//%s.%s/%s", username, c.Domain, fname)
return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
}
return fmt.Sprintf("/%s/%s", username, fname)
@ -84,7 +86,7 @@ func (c *ConfigSite) IsSubdomains() bool {
func (c *ConfigSite) RssBlogURL(username string) string {
if c.IsSubdomains() {
return fmt.Sprintf("//%s.%s/rss", username, c.Domain)
return fmt.Sprintf("%s://%s.%s/rss", c.Protocol, username, c.Domain)
}
return fmt.Sprintf("/%s/rss", username)
@ -100,7 +102,7 @@ func (c *ConfigSite) HomeURL() string {
func (c *ConfigSite) ReadURL() string {
if c.IsSubdomains() {
return fmt.Sprintf("https://%s/read", c.Domain)
return fmt.Sprintf("%s://%s/read", c.Protocol, c.Domain)
}
return "/read"

38
internal/cron.go Normal file
View File

@ -0,0 +1,38 @@
package internal
import (
"time"
"git.sr.ht/~erock/wish/cms/db"
)
func deleteExpiredPosts(cfg *ConfigSite, dbpool db.DB) error {
cfg.Logger.Infof("checking for expired posts")
now := time.Now()
// delete posts that are older than three days
expired := now.AddDate(0, 0, -3)
posts, err := dbpool.FindPostsBeforeDate(&expired)
if err != nil {
return err
}
postIds := []string{}
for _, post := range posts {
postIds = append(postIds, post.ID)
}
cfg.Logger.Infof("deleteing (%d) expired posts", len(postIds))
err = dbpool.RemovePosts(postIds)
if err != nil {
return err
}
return nil
}
func CronDeleteExpiredPosts(cfg *ConfigSite, dbpool db.DB) {
for {
deleteExpiredPosts(cfg, dbpool)
time.Sleep(1 * time.Hour)
}
}