Commit eb3b3bb
Eric Bower
·
2026-01-25 09:55:59 -0500 EST
parent bccde8d
refactor: fix issues with imports and create shared/router pkg
55 files changed,
+479,
-482
+3,
-5
| ... | ... | @@ -13,14 +13,12 @@ import ( | |
| 13 | 13 | "github.com/darkweak/souin/pkg/middleware" | |
| 14 | 14 | "github.com/hashicorp/golang-lru/v2/expirable" | |
| 15 | 15 | "github.com/picosh/pico/pkg/apps/pgs" | |
| 16 | - | "github.com/picosh/pico/pkg/cache" | |
| 17 | 16 | "github.com/picosh/pico/pkg/shared" | |
| 18 | - | "github.com/picosh/utils" | |
| 19 | 17 | "github.com/prometheus/client_golang/prometheus/promhttp" | |
| 20 | 18 | ) | |
| 21 | 19 | ||
| 22 | 20 | func main() { | |
| 23 | - | withPipe := strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 21 | + | withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 24 | 22 | logger := shared.CreateLogger("pgs-cdn", withPipe) | |
| 25 | 23 | ctx := context.Background() | |
| 26 | 24 | drain := pgs.CreateSubCacheDrain(ctx, logger) |
| ... | ... | @@ -32,8 +30,8 @@ func main() { | |
| 32 | 30 | httpCache := pgs.SetupCache(cfg) | |
| 33 | 31 | router := &pgs.WebRouter{ | |
| 34 | 32 | Cfg: cfg, | |
| 35 | - | RedirectsCache: expirable.NewLRU[string, []*pgs.RedirectRule](2048, nil, cache.CacheTimeout), | |
| 36 | - | HeadersCache: expirable.NewLRU[string, []*pgs.HeaderRule](2048, nil, cache.CacheTimeout), | |
| 33 | + | RedirectsCache: expirable.NewLRU[string, []*pgs.RedirectRule](2048, nil, shared.CacheTimeout), | |
| 34 | + | HeadersCache: expirable.NewLRU[string, []*pgs.HeaderRule](2048, nil, shared.CacheTimeout), | |
| 37 | 35 | } | |
| 38 | 36 | cacher := &cachedHttp{ | |
| 39 | 37 | handler: httpCache, |
+2,
-3
| ... | ... | @@ -8,12 +8,11 @@ import ( | |
| 8 | 8 | pgsdb "github.com/picosh/pico/pkg/apps/pgs/db" | |
| 9 | 9 | "github.com/picosh/pico/pkg/shared" | |
| 10 | 10 | "github.com/picosh/pico/pkg/shared/storage" | |
| 11 | - | "github.com/picosh/utils" | |
| 12 | 11 | "golang.org/x/crypto/ssh" | |
| 13 | 12 | ) | |
| 14 | 13 | ||
| 15 | 14 | func main() { | |
| 16 | - | dbURL := utils.GetEnv("DATABASE_URL", "./data/pgs.sqlite3") | |
| 15 | + | dbURL := shared.GetEnv("DATABASE_URL", "./data/pgs.sqlite3") | |
| 17 | 16 | logger := shared.CreateLogger("pgs-standalone", false) | |
| 18 | 17 | dbpool, err := pgsdb.NewSqliteDB(dbURL, logger) | |
| 19 | 18 | if err != nil { |
| ... | ... | @@ -45,7 +44,7 @@ func main() { | |
| 45 | 44 | logger.Error("parse pubkey", "err", err) | |
| 46 | 45 | return | |
| 47 | 46 | } | |
| 48 | - | pubkey := utils.KeyForKeyText(key) | |
| 47 | + | pubkey := shared.KeyForKeyText(key) | |
| 49 | 48 | logger.Info("init cli", "userName", userName, "pubkey", pubkey) | |
| 50 | 49 | ||
| 51 | 50 | err = dbpool.RegisterAdmin(userName, pubkey, comment) |
+2,
-2
| ... | ... | @@ -6,7 +6,7 @@ import ( | |
| 6 | 6 | ||
| 7 | 7 | "github.com/picosh/pico/pkg/db" | |
| 8 | 8 | "github.com/picosh/pico/pkg/db/postgres" | |
| 9 | - | "github.com/picosh/utils" | |
| 9 | + | "github.com/picosh/pico/pkg/shared" | |
| 10 | 10 | ) | |
| 11 | 11 | ||
| 12 | 12 | func main() { |
| ... | ... | @@ -19,7 +19,7 @@ func main() { | |
| 19 | 19 | ||
| 20 | 20 | stats, err := dbpool.VisitSummary( | |
| 21 | 21 | &db.SummaryOpts{ | |
| 22 | - | Origin: utils.StartOfMonth(), | |
| 22 | + | Origin: shared.StartOfMonth(), | |
| 23 | 23 | Host: host, | |
| 24 | 24 | }, | |
| 25 | 25 | ) |
+3,
-3
| ... | ... | @@ -6,7 +6,7 @@ import ( | |
| 6 | 6 | "os" | |
| 7 | 7 | ||
| 8 | 8 | "github.com/picosh/pico/pkg/db/postgres" | |
| 9 | - | "github.com/picosh/pico/pkg/shared" | |
| 9 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 10 | 10 | ) | |
| 11 | 11 | ||
| 12 | 12 | func main() { |
| ... | ... | @@ -47,7 +47,7 @@ func main() { | |
| 47 | 47 | ||
| 48 | 48 | update := false | |
| 49 | 49 | ||
| 50 | - | host, err := shared.CleanHost(origHost) | |
| 50 | + | host, err := router.CleanHost(origHost) | |
| 51 | 51 | if err != nil { | |
| 52 | 52 | fmt.Println(err) | |
| 53 | 53 | } |
| ... | ... | @@ -60,7 +60,7 @@ func main() { | |
| 60 | 60 | ) | |
| 61 | 61 | } | |
| 62 | 62 | ||
| 63 | - | ref, err := shared.CleanReferer(origRef) | |
| 63 | + | ref, err := router.CleanReferer(origRef) | |
| 64 | 64 | if err != nil { | |
| 65 | 65 | fmt.Println(err) | |
| 66 | 66 | } |
+1,
-2
| ... | ... | @@ -6,7 +6,6 @@ import ( | |
| 6 | 6 | ||
| 7 | 7 | "github.com/picosh/pico/pkg/db/postgres" | |
| 8 | 8 | "github.com/picosh/pico/pkg/shared" | |
| 9 | - | "github.com/picosh/utils" | |
| 10 | 9 | ) | |
| 11 | 10 | ||
| 12 | 11 | func main() { |
| ... | ... | @@ -25,7 +24,7 @@ func main() { | |
| 25 | 24 | empty := 0 | |
| 26 | 25 | diff := 0 | |
| 27 | 26 | for _, post := range posts { | |
| 28 | - | nextShasum := utils.Shasum([]byte(post.Text)) | |
| 27 | + | nextShasum := shared.Shasum([]byte(post.Text)) | |
| 29 | 28 | if post.Shasum == "" { | |
| 30 | 29 | empty += 1 | |
| 31 | 30 | } else if post.Shasum != nextShasum { |
+28,
-28
| ... | ... | @@ -19,7 +19,7 @@ import ( | |
| 19 | 19 | "github.com/picosh/pico/pkg/db" | |
| 20 | 20 | "github.com/picosh/pico/pkg/db/postgres" | |
| 21 | 21 | "github.com/picosh/pico/pkg/shared" | |
| 22 | - | "github.com/picosh/utils" | |
| 22 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 23 | 23 | "github.com/picosh/utils/pipe" | |
| 24 | 24 | "github.com/picosh/utils/pipe/metrics" | |
| 25 | 25 | "github.com/prometheus/client_golang/prometheus/promhttp" |
| ... | ... | @@ -48,7 +48,7 @@ func generateURL(cfg *shared.ConfigSite, path string, space string) string { | |
| 48 | 48 | return fmt.Sprintf("%s/%s%s", cfg.Domain, path, query) | |
| 49 | 49 | } | |
| 50 | 50 | ||
| 51 | - | func wellKnownHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 51 | + | func wellKnownHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 52 | 52 | return func(w http.ResponseWriter, r *http.Request) { | |
| 53 | 53 | space := r.PathValue("space") | |
| 54 | 54 | if space == "" { |
| ... | ... | @@ -80,7 +80,7 @@ type oauth2Introspection struct { | |
| 80 | 80 | Username string `json:"username"` | |
| 81 | 81 | } | |
| 82 | 82 | ||
| 83 | - | func introspectHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 83 | + | func introspectHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 84 | 84 | return func(w http.ResponseWriter, r *http.Request) { | |
| 85 | 85 | token := r.FormValue("token") | |
| 86 | 86 | apiConfig.Cfg.Logger.Info("introspect token", "token", token) |
| ... | ... | @@ -114,7 +114,7 @@ func introspectHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 114 | 114 | } | |
| 115 | 115 | } | |
| 116 | 116 | ||
| 117 | - | func authorizeHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 117 | + | func authorizeHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 118 | 118 | return func(w http.ResponseWriter, r *http.Request) { | |
| 119 | 119 | responseType := r.URL.Query().Get("response_type") | |
| 120 | 120 | clientID := r.URL.Query().Get("client_id") |
| ... | ... | @@ -158,7 +158,7 @@ func authorizeHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 158 | 158 | } | |
| 159 | 159 | } | |
| 160 | 160 | ||
| 161 | - | func redirectHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 161 | + | func redirectHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 162 | 162 | return func(w http.ResponseWriter, r *http.Request) { | |
| 163 | 163 | token := r.FormValue("token") | |
| 164 | 164 | redirectURI := r.FormValue("redirect_uri") |
| ... | ... | @@ -194,7 +194,7 @@ type oauth2Token struct { | |
| 194 | 194 | AccessToken string `json:"access_token"` | |
| 195 | 195 | } | |
| 196 | 196 | ||
| 197 | - | func tokenHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 197 | + | func tokenHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 198 | 198 | return func(w http.ResponseWriter, r *http.Request) { | |
| 199 | 199 | token := r.FormValue("code") | |
| 200 | 200 | redirectURI := r.FormValue("redirect_uri") |
| ... | ... | @@ -233,7 +233,7 @@ type sishData struct { | |
| 233 | 233 | RemoteAddress string `json:"remote_addr"` | |
| 234 | 234 | } | |
| 235 | 235 | ||
| 236 | - | func keyHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 236 | + | func keyHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 237 | 237 | return func(w http.ResponseWriter, r *http.Request) { | |
| 238 | 238 | var data sishData | |
| 239 | 239 |
| ... | ... | @@ -292,7 +292,7 @@ func keyHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 292 | 292 | log.Error("cannot insert access log", "err", err) | |
| 293 | 293 | } | |
| 294 | 294 | ||
| 295 | - | if !apiConfig.HasPrivilegedAccess(shared.GetApiToken(r)) { | |
| 295 | + | if !apiConfig.HasPrivilegedAccess(router.GetApiToken(r)) { | |
| 296 | 296 | w.WriteHeader(http.StatusOK) | |
| 297 | 297 | return | |
| 298 | 298 | } |
| ... | ... | @@ -307,9 +307,9 @@ func keyHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 307 | 307 | } | |
| 308 | 308 | } | |
| 309 | 309 | ||
| 310 | - | func userHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 310 | + | func userHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 311 | 311 | return func(w http.ResponseWriter, r *http.Request) { | |
| 312 | - | if !apiConfig.HasPrivilegedAccess(shared.GetApiToken(r)) { | |
| 312 | + | if !apiConfig.HasPrivilegedAccess(router.GetApiToken(r)) { | |
| 313 | 313 | w.WriteHeader(http.StatusForbidden) | |
| 314 | 314 | return | |
| 315 | 315 | } |
| ... | ... | @@ -354,7 +354,7 @@ func userHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 354 | 354 | } | |
| 355 | 355 | } | |
| 356 | 356 | ||
| 357 | - | func rssHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 357 | + | func rssHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 358 | 358 | return func(w http.ResponseWriter, r *http.Request) { | |
| 359 | 359 | apiToken := r.PathValue("token") | |
| 360 | 360 | user, err := apiConfig.Dbpool.FindUserByToken(apiToken) |
| ... | ... | @@ -387,7 +387,7 @@ func rssHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 387 | 387 | } | |
| 388 | 388 | } | |
| 389 | 389 | ||
| 390 | - | func pubkeysHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 390 | + | func pubkeysHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 391 | 391 | return func(w http.ResponseWriter, r *http.Request) { | |
| 392 | 392 | userName := r.PathValue("user") | |
| 393 | 393 | user, err := apiConfig.Dbpool.FindUserByName(userName) |
| ... | ... | @@ -456,7 +456,7 @@ type OrderEvent struct { | |
| 456 | 456 | ||
| 457 | 457 | // Status code must be 200 or else lemonsqueezy will keep retrying | |
| 458 | 458 | // https://docs.lemonsqueezy.com/help/webhooks | |
| 459 | - | func paymentWebhookHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 459 | + | func paymentWebhookHandler(apiConfig *router.ApiConfig) http.HandlerFunc { | |
| 460 | 460 | return func(w http.ResponseWriter, r *http.Request) { | |
| 461 | 461 | dbpool := apiConfig.Dbpool | |
| 462 | 462 | logger := apiConfig.Cfg.Logger |
| ... | ... | @@ -482,7 +482,7 @@ func paymentWebhookHandler(apiConfig *shared.ApiConfig) http.HandlerFunc { | |
| 482 | 482 | return | |
| 483 | 483 | } | |
| 484 | 484 | ||
| 485 | - | hash := shared.HmacString(apiConfig.Cfg.SecretWebhook, string(payload)) | |
| 485 | + | hash := router.HmacString(apiConfig.Cfg.SecretWebhook, string(payload)) | |
| 486 | 486 | sig := r.Header.Get("X-Signature") | |
| 487 | 487 | if !hmac.Equal([]byte(hash), []byte(sig)) { | |
| 488 | 488 | logger.Error("invalid signature X-Signature") |
| ... | ... | @@ -678,14 +678,14 @@ func deserializeCaddyAccessLog(dbpool db.DB, access *AccessLog) (*db.AnalyticsVi | |
| 678 | 678 | } else if strings.HasSuffix(host, "prose.sh") { | |
| 679 | 679 | subdomain = strings.TrimSuffix(host, ".prose.sh") | |
| 680 | 680 | } else { | |
| 681 | - | subdomain = shared.GetCustomDomain(host, space) | |
| 681 | + | subdomain = router.GetCustomDomain(host, space) | |
| 682 | 682 | } | |
| 683 | 683 | ||
| 684 | 684 | subdomain = strings.TrimSuffix(subdomain, ".nue") | |
| 685 | 685 | subdomain = strings.TrimSuffix(subdomain, ".ash") | |
| 686 | 686 | ||
| 687 | 687 | // get user and namespace details from subdomain | |
| 688 | - | props, err := shared.GetProjectFromSubdomain(subdomain) | |
| 688 | + | props, err := router.GetProjectFromSubdomain(subdomain) | |
| 689 | 689 | if err != nil { | |
| 690 | 690 | return nil, fmt.Errorf("could not get project from subdomain %s: %w", subdomain, err) | |
| 691 | 691 | } |
| ... | ... | @@ -772,7 +772,7 @@ func metricDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger, secr | |
| 772 | 772 | } | |
| 773 | 773 | ||
| 774 | 774 | logger.Info("received visit", "visit", visit) | |
| 775 | - | err = shared.AnalyticsVisitFromVisit(visit, dbpool, secret) | |
| 775 | + | err = router.AnalyticsVisitFromVisit(visit, dbpool, secret) | |
| 776 | 776 | if err != nil { | |
| 777 | 777 | logger.Info("could not record analytics visit", "err", err) | |
| 778 | 778 | continue |
| ... | ... | @@ -834,7 +834,7 @@ func tunsEventLogDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger | |
| 834 | 834 | } | |
| 835 | 835 | } | |
| 836 | 836 | ||
| 837 | - | func authMux(apiConfig *shared.ApiConfig) *http.ServeMux { | |
| 837 | + | func authMux(apiConfig *router.ApiConfig) *http.ServeMux { | |
| 838 | 838 | serverRoot, err := fs.Sub(embedFS, "public") | |
| 839 | 839 | if err != nil { | |
| 840 | 840 | panic(err) |
| ... | ... | @@ -866,24 +866,24 @@ func authMux(apiConfig *shared.ApiConfig) *http.ServeMux { | |
| 866 | 866 | mux.HandleFunc("GET /_metrics", promhttp.Handler().ServeHTTP) | |
| 867 | 867 | ||
| 868 | 868 | if apiConfig.Cfg.Debug { | |
| 869 | - | shared.CreatePProfRoutesMux(mux) | |
| 869 | + | router.CreatePProfRoutesMux(mux) | |
| 870 | 870 | } | |
| 871 | 871 | ||
| 872 | 872 | return mux | |
| 873 | 873 | } | |
| 874 | 874 | ||
| 875 | 875 | func StartApiServer() { | |
| 876 | - | debug := utils.GetEnv("AUTH_DEBUG", "0") | |
| 877 | - | withPipe := strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 876 | + | debug := shared.GetEnv("AUTH_DEBUG", "0") | |
| 877 | + | withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 878 | 878 | ||
| 879 | 879 | cfg := &shared.ConfigSite{ | |
| 880 | - | DbURL: utils.GetEnv("DATABASE_URL", ""), | |
| 880 | + | DbURL: shared.GetEnv("DATABASE_URL", ""), | |
| 881 | 881 | Debug: debug == "1", | |
| 882 | - | Issuer: utils.GetEnv("AUTH_ISSUER", "pico.sh"), | |
| 883 | - | Domain: utils.GetEnv("AUTH_DOMAIN", "http://0.0.0.0:3000"), | |
| 884 | - | Port: utils.GetEnv("AUTH_WEB_PORT", "3000"), | |
| 885 | - | Secret: utils.GetEnv("PICO_SECRET", ""), | |
| 886 | - | SecretWebhook: utils.GetEnv("PICO_SECRET_WEBHOOK", ""), | |
| 882 | + | Issuer: shared.GetEnv("AUTH_ISSUER", "pico.sh"), | |
| 883 | + | Domain: shared.GetEnv("AUTH_DOMAIN", "http://0.0.0.0:3000"), | |
| 884 | + | Port: shared.GetEnv("AUTH_WEB_PORT", "3000"), | |
| 885 | + | Secret: shared.GetEnv("PICO_SECRET", ""), | |
| 886 | + | SecretWebhook: shared.GetEnv("PICO_SECRET_WEBHOOK", ""), | |
| 887 | 887 | } | |
| 888 | 888 | ||
| 889 | 889 | if cfg.SecretWebhook == "" { |
| ... | ... | @@ -911,7 +911,7 @@ func StartApiServer() { | |
| 911 | 911 | // gather connect/disconnect logs from tuns | |
| 912 | 912 | go tunsEventLogDrainSub(ctx, db, logger, cfg.Secret) | |
| 913 | 913 | ||
| 914 | - | apiConfig := &shared.ApiConfig{ | |
| 914 | + | apiConfig := &router.ApiConfig{ | |
| 915 | 915 | Cfg: cfg, | |
| 916 | 916 | Dbpool: db, | |
| 917 | 917 | } |
+4,
-3
| ... | ... | @@ -15,6 +15,7 @@ import ( | |
| 15 | 15 | "github.com/picosh/pico/pkg/db" | |
| 16 | 16 | "github.com/picosh/pico/pkg/db/stub" | |
| 17 | 17 | "github.com/picosh/pico/pkg/shared" | |
| 18 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 18 | 19 | ) | |
| 19 | 20 | ||
| 20 | 21 | var testUserID = "user-1" |
| ... | ... | @@ -41,7 +42,7 @@ func TestPaymentWebhook(t *testing.T) { | |
| 41 | 42 | } | |
| 42 | 43 | jso, err := json.Marshal(event) | |
| 43 | 44 | bail(err) | |
| 44 | - | hash := shared.HmacString(apiConfig.Cfg.SecretWebhook, string(jso)) | |
| 45 | + | hash := router.HmacString(apiConfig.Cfg.SecretWebhook, string(jso)) | |
| 45 | 46 | body := bytes.NewReader(jso) | |
| 46 | 47 | ||
| 47 | 48 | request := httptest.NewRequest("POST", mkpath("/webhook"), body) |
| ... | ... | @@ -275,7 +276,7 @@ func mkpath(path string) string { | |
| 275 | 276 | return fmt.Sprintf("https://auth.pico.test%s", path) | |
| 276 | 277 | } | |
| 277 | 278 | ||
| 278 | - | func setupTest() *shared.ApiConfig { | |
| 279 | + | func setupTest() *router.ApiConfig { | |
| 279 | 280 | logger := shared.CreateLogger("auth-test", false) | |
| 280 | 281 | cfg := &shared.ConfigSite{ | |
| 281 | 282 | Issuer: "auth.pico.test", |
| ... | ... | @@ -286,7 +287,7 @@ func setupTest() *shared.ApiConfig { | |
| 286 | 287 | } | |
| 287 | 288 | cfg.Logger = logger | |
| 288 | 289 | db := NewAuthDb(cfg.Logger) | |
| 289 | - | apiConfig := &shared.ApiConfig{ | |
| 290 | + | apiConfig := &router.ApiConfig{ | |
| 290 | 291 | Cfg: cfg, | |
| 291 | 292 | Dbpool: db, | |
| 292 | 293 | } |
+25,
-24
| ... | ... | @@ -8,13 +8,14 @@ import ( | |
| 8 | 8 | ||
| 9 | 9 | "github.com/picosh/pico/pkg/db/postgres" | |
| 10 | 10 | "github.com/picosh/pico/pkg/shared" | |
| 11 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 11 | 12 | "github.com/prometheus/client_golang/prometheus/promhttp" | |
| 12 | 13 | ) | |
| 13 | 14 | ||
| 14 | 15 | func keepAliveHandler(w http.ResponseWriter, r *http.Request) { | |
| 15 | - | dbpool := shared.GetDB(r) | |
| 16 | - | logger := shared.GetLogger(r) | |
| 17 | - | postID, _ := url.PathUnescape(shared.GetField(r, 0)) | |
| 16 | + | dbpool := router.GetDB(r) | |
| 17 | + | logger := router.GetLogger(r) | |
| 18 | + | postID, _ := url.PathUnescape(router.GetField(r, 0)) | |
| 18 | 19 | ||
| 19 | 20 | post, err := dbpool.FindPost(postID) | |
| 20 | 21 | if err != nil { |
| ... | ... | @@ -60,9 +61,9 @@ func keepAliveHandler(w http.ResponseWriter, r *http.Request) { | |
| 60 | 61 | } | |
| 61 | 62 | ||
| 62 | 63 | func unsubHandler(w http.ResponseWriter, r *http.Request) { | |
| 63 | - | dbpool := shared.GetDB(r) | |
| 64 | - | logger := shared.GetLogger(r) | |
| 65 | - | postID, _ := url.PathUnescape(shared.GetField(r, 0)) | |
| 64 | + | dbpool := router.GetDB(r) | |
| 65 | + | logger := router.GetLogger(r) | |
| 66 | + | postID, _ := url.PathUnescape(router.GetField(r, 0)) | |
| 66 | 67 | ||
| 67 | 68 | post, err := dbpool.FindPost(postID) | |
| 68 | 69 | if err != nil { |
| ... | ... | @@ -96,12 +97,12 @@ func unsubHandler(w http.ResponseWriter, r *http.Request) { | |
| 96 | 97 | } | |
| 97 | 98 | } | |
| 98 | 99 | ||
| 99 | - | func createMainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 100 | - | routes := []shared.Route{ | |
| 101 | - | shared.NewRoute("GET", "/", shared.CreatePageHandler("html/marketing.page.tmpl")), | |
| 102 | - | shared.NewRoute("GET", "/keep-alive/(.+)", keepAliveHandler), | |
| 103 | - | shared.NewRoute("GET", "/unsub/(.+)", unsubHandler), | |
| 104 | - | shared.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 100 | + | func createMainRoutes(staticRoutes []router.Route) []router.Route { | |
| 101 | + | routes := []router.Route{ | |
| 102 | + | router.NewRoute("GET", "/", router.CreatePageHandler("html/marketing.page.tmpl")), | |
| 103 | + | router.NewRoute("GET", "/keep-alive/(.+)", keepAliveHandler), | |
| 104 | + | router.NewRoute("GET", "/unsub/(.+)", unsubHandler), | |
| 105 | + | router.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 105 | 106 | } | |
| 106 | 107 | ||
| 107 | 108 | routes = append( |
| ... | ... | @@ -112,15 +113,15 @@ func createMainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 112 | 113 | return routes | |
| 113 | 114 | } | |
| 114 | 115 | ||
| 115 | - | func createStaticRoutes() []shared.Route { | |
| 116 | - | return []shared.Route{ | |
| 117 | - | shared.NewRoute("GET", "/main.css", shared.ServeFile("main.css", "text/css")), | |
| 118 | - | shared.NewRoute("GET", "/card.png", shared.ServeFile("card.png", "image/png")), | |
| 119 | - | shared.NewRoute("GET", "/favicon-16x16.png", shared.ServeFile("favicon-16x16.png", "image/png")), | |
| 120 | - | shared.NewRoute("GET", "/favicon-32x32.png", shared.ServeFile("favicon-32x32.png", "image/png")), | |
| 121 | - | shared.NewRoute("GET", "/apple-touch-icon.png", shared.ServeFile("apple-touch-icon.png", "image/png")), | |
| 122 | - | shared.NewRoute("GET", "/favicon.ico", shared.ServeFile("favicon.ico", "image/x-icon")), | |
| 123 | - | shared.NewRoute("GET", "/robots.txt", shared.ServeFile("robots.txt", "text/plain")), | |
| 116 | + | func createStaticRoutes() []router.Route { | |
| 117 | + | return []router.Route{ | |
| 118 | + | router.NewRoute("GET", "/main.css", router.ServeFile("main.css", "text/css")), | |
| 119 | + | router.NewRoute("GET", "/card.png", router.ServeFile("card.png", "image/png")), | |
| 120 | + | router.NewRoute("GET", "/favicon-16x16.png", router.ServeFile("favicon-16x16.png", "image/png")), | |
| 121 | + | router.NewRoute("GET", "/favicon-32x32.png", router.ServeFile("favicon-32x32.png", "image/png")), | |
| 122 | + | router.NewRoute("GET", "/apple-touch-icon.png", router.ServeFile("apple-touch-icon.png", "image/png")), | |
| 123 | + | router.NewRoute("GET", "/favicon.ico", router.ServeFile("favicon.ico", "image/x-icon")), | |
| 124 | + | router.NewRoute("GET", "/robots.txt", router.ServeFile("robots.txt", "text/plain")), | |
| 124 | 125 | } | |
| 125 | 126 | } | |
| 126 | 127 |
| ... | ... | @@ -139,16 +140,16 @@ func StartApiServer() { | |
| 139 | 140 | staticRoutes := createStaticRoutes() | |
| 140 | 141 | ||
| 141 | 142 | if cfg.Debug { | |
| 142 | - | staticRoutes = shared.CreatePProfRoutes(staticRoutes) | |
| 143 | + | staticRoutes = router.CreatePProfRoutes(staticRoutes) | |
| 143 | 144 | } | |
| 144 | 145 | ||
| 145 | 146 | mainRoutes := createMainRoutes(staticRoutes) | |
| 146 | 147 | ||
| 147 | - | apiConfig := &shared.ApiConfig{ | |
| 148 | + | apiConfig := &router.ApiConfig{ | |
| 148 | 149 | Cfg: cfg, | |
| 149 | 150 | Dbpool: db, | |
| 150 | 151 | } | |
| 151 | - | handler := shared.CreateServe(mainRoutes, []shared.Route{}, apiConfig) | |
| 152 | + | handler := router.CreateServe(mainRoutes, []router.Route{}, apiConfig) | |
| 152 | 153 | router := http.HandlerFunc(handler) | |
| 153 | 154 | ||
| 154 | 155 | portStr := fmt.Sprintf(":%s", cfg.Port) |
+7,
-8
| ... | ... | @@ -4,17 +4,16 @@ import ( | |
| 4 | 4 | "strings" | |
| 5 | 5 | ||
| 6 | 6 | "github.com/picosh/pico/pkg/shared" | |
| 7 | - | "github.com/picosh/utils" | |
| 8 | 7 | ) | |
| 9 | 8 | ||
| 10 | 9 | func NewConfigSite(service string) *shared.ConfigSite { | |
| 11 | - | debug := utils.GetEnv("FEEDS_DEBUG", "0") | |
| 12 | - | domain := utils.GetEnv("FEEDS_DOMAIN", "feeds.pico.sh") | |
| 13 | - | port := utils.GetEnv("FEEDS_WEB_PORT", "3000") | |
| 14 | - | protocol := utils.GetEnv("FEEDS_PROTOCOL", "https") | |
| 15 | - | dbURL := utils.GetEnv("DATABASE_URL", "") | |
| 16 | - | sendgridKey := utils.GetEnv("SENDGRID_API_KEY", "") | |
| 17 | - | withPipe := strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 10 | + | debug := shared.GetEnv("FEEDS_DEBUG", "0") | |
| 11 | + | domain := shared.GetEnv("FEEDS_DOMAIN", "feeds.pico.sh") | |
| 12 | + | port := shared.GetEnv("FEEDS_WEB_PORT", "3000") | |
| 13 | + | protocol := shared.GetEnv("FEEDS_PROTOCOL", "https") | |
| 14 | + | dbURL := shared.GetEnv("DATABASE_URL", "") | |
| 15 | + | sendgridKey := shared.GetEnv("SENDGRID_API_KEY", "") | |
| 16 | + | withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 18 | 17 | ||
| 19 | 18 | return &shared.ConfigSite{ | |
| 20 | 19 | Debug: debug == "1", |
+1,
-2
| ... | ... | @@ -21,7 +21,6 @@ import ( | |
| 21 | 21 | "github.com/mmcdole/gofeed" | |
| 22 | 22 | "github.com/picosh/pico/pkg/db" | |
| 23 | 23 | "github.com/picosh/pico/pkg/shared" | |
| 24 | - | "github.com/picosh/utils" | |
| 25 | 24 | ) | |
| 26 | 25 | ||
| 27 | 26 | var ErrNoRecentArticles = errors.New("no recent articles") |
| ... | ... | @@ -564,7 +563,7 @@ func (f *Fetcher) FetchAll(logger *slog.Logger, urls []string, inlineContent boo | |
| 564 | 563 | } | |
| 565 | 564 | ||
| 566 | 565 | // cap body size to prevent abuse | |
| 567 | - | if len(html)+len(text) > 5*utils.MB { | |
| 566 | + | if len(html)+len(text) > 5*shared.MB { | |
| 568 | 567 | feeds.Options.InlineContent = false | |
| 569 | 568 | feeds.SizeWarning = true | |
| 570 | 569 | html, err = f.PrintHtml(feeds) |
+2,
-3
| ... | ... | @@ -11,7 +11,6 @@ import ( | |
| 11 | 11 | "github.com/picosh/pico/pkg/filehandlers" | |
| 12 | 12 | "github.com/picosh/pico/pkg/pssh" | |
| 13 | 13 | "github.com/picosh/pico/pkg/shared" | |
| 14 | - | "github.com/picosh/utils" | |
| 15 | 14 | ) | |
| 16 | 15 | ||
| 17 | 16 | type FeedHooks struct { |
| ... | ... | @@ -20,7 +19,7 @@ type FeedHooks struct { | |
| 20 | 19 | } | |
| 21 | 20 | ||
| 22 | 21 | func (p *FeedHooks) FileValidate(s *pssh.SSHServerConnSession, data *filehandlers.PostMetaData) (bool, error) { | |
| 23 | - | if !utils.IsTextFile(string(data.Text)) { | |
| 22 | + | if !shared.IsTextFile(string(data.Text)) { | |
| 24 | 23 | err := fmt.Errorf( | |
| 25 | 24 | "WARNING: (%s) invalid file must be plain text (utf-8), skipping", | |
| 26 | 25 | data.Filename, |
| ... | ... | @@ -28,7 +27,7 @@ func (p *FeedHooks) FileValidate(s *pssh.SSHServerConnSession, data *filehandler | |
| 28 | 27 | return false, err | |
| 29 | 28 | } | |
| 30 | 29 | ||
| 31 | - | if !utils.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) { | |
| 30 | + | if !shared.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) { | |
| 32 | 31 | extStr := strings.Join(p.Cfg.AllowedExt, ",") | |
| 33 | 32 | err := fmt.Errorf( | |
| 34 | 33 | "WARNING: (%s) invalid file, format must be (%s), skipping", |
+3,
-4
| ... | ... | @@ -16,15 +16,14 @@ import ( | |
| 16 | 16 | "github.com/picosh/pico/pkg/send/protocols/scp" | |
| 17 | 17 | "github.com/picosh/pico/pkg/send/protocols/sftp" | |
| 18 | 18 | "github.com/picosh/pico/pkg/shared" | |
| 19 | - | "github.com/picosh/utils" | |
| 20 | 19 | ) | |
| 21 | 20 | ||
| 22 | 21 | func StartSshServer() { | |
| 23 | 22 | appName := "feeds-ssh" | |
| 24 | 23 | ||
| 25 | - | host := utils.GetEnv("FEEDS_HOST", "0.0.0.0") | |
| 26 | - | port := utils.GetEnv("FEEDS_SSH_PORT", "2222") | |
| 27 | - | promPort := utils.GetEnv("FEEDS_PROM_PORT", "9222") | |
| 24 | + | host := shared.GetEnv("FEEDS_HOST", "0.0.0.0") | |
| 25 | + | port := shared.GetEnv("FEEDS_SSH_PORT", "2222") | |
| 26 | + | promPort := shared.GetEnv("FEEDS_PROM_PORT", "9222") | |
| 28 | 27 | cfg := NewConfigSite(appName) | |
| 29 | 28 | logger := cfg.Logger | |
| 30 | 29 |
+54,
-54
| ... | ... | @@ -11,7 +11,7 @@ import ( | |
| 11 | 11 | "github.com/picosh/pico/pkg/db" | |
| 12 | 12 | "github.com/picosh/pico/pkg/db/postgres" | |
| 13 | 13 | "github.com/picosh/pico/pkg/shared" | |
| 14 | - | "github.com/picosh/utils" | |
| 14 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 15 | 15 | "github.com/prometheus/client_golang/prometheus/promhttp" | |
| 16 | 16 | ) | |
| 17 | 17 |
| ... | ... | @@ -73,11 +73,11 @@ type HeaderTxt struct { | |
| 73 | 73 | } | |
| 74 | 74 | ||
| 75 | 75 | func blogHandler(w http.ResponseWriter, r *http.Request) { | |
| 76 | - | username := shared.GetUsernameFromRequest(r) | |
| 77 | - | dbpool := shared.GetDB(r) | |
| 78 | - | blogger := shared.GetLogger(r) | |
| 76 | + | username := router.GetUsernameFromRequest(r) | |
| 77 | + | dbpool := router.GetDB(r) | |
| 78 | + | blogger := router.GetLogger(r) | |
| 79 | 79 | logger := blogger.With("user", username) | |
| 80 | - | cfg := shared.GetCfg(r) | |
| 80 | + | cfg := router.GetCfg(r) | |
| 81 | 81 | ||
| 82 | 82 | user, err := dbpool.FindUserByName(username) | |
| 83 | 83 | if err != nil { |
| ... | ... | @@ -96,7 +96,7 @@ func blogHandler(w http.ResponseWriter, r *http.Request) { | |
| 96 | 96 | ||
| 97 | 97 | posts := pager.Data | |
| 98 | 98 | ||
| 99 | - | ts, err := shared.RenderTemplate(cfg, []string{ | |
| 99 | + | ts, err := router.RenderTemplate(cfg, []string{ | |
| 100 | 100 | cfg.StaticPath("html/blog.page.tmpl"), | |
| 101 | 101 | }) | |
| 102 | 102 |
| ... | ... | @@ -120,7 +120,7 @@ func blogHandler(w http.ResponseWriter, r *http.Request) { | |
| 120 | 120 | Title: post.Filename, | |
| 121 | 121 | PublishAt: post.PublishAt.Format(time.DateOnly), | |
| 122 | 122 | PublishAtISO: post.PublishAt.Format(time.RFC3339), | |
| 123 | - | UpdatedTimeAgo: utils.TimeAgo(post.UpdatedAt), | |
| 123 | + | UpdatedTimeAgo: shared.TimeAgo(post.UpdatedAt), | |
| 124 | 124 | UpdatedAtISO: post.UpdatedAt.Format(time.RFC3339), | |
| 125 | 125 | } | |
| 126 | 126 | postCollection = append(postCollection, p) |
| ... | ... | @@ -156,19 +156,19 @@ func GetBlogName(username string) string { | |
| 156 | 156 | } | |
| 157 | 157 | ||
| 158 | 158 | func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 159 | - | username := shared.GetUsernameFromRequest(r) | |
| 160 | - | subdomain := shared.GetSubdomain(r) | |
| 161 | - | cfg := shared.GetCfg(r) | |
| 159 | + | username := router.GetUsernameFromRequest(r) | |
| 160 | + | subdomain := router.GetSubdomain(r) | |
| 161 | + | cfg := router.GetCfg(r) | |
| 162 | 162 | ||
| 163 | 163 | var slug string | |
| 164 | 164 | if !cfg.IsSubdomains() || subdomain == "" { | |
| 165 | - | slug, _ = url.PathUnescape(shared.GetField(r, 1)) | |
| 165 | + | slug, _ = url.PathUnescape(router.GetField(r, 1)) | |
| 166 | 166 | } else { | |
| 167 | - | slug, _ = url.PathUnescape(shared.GetField(r, 0)) | |
| 167 | + | slug, _ = url.PathUnescape(router.GetField(r, 0)) | |
| 168 | 168 | } | |
| 169 | 169 | ||
| 170 | - | dbpool := shared.GetDB(r) | |
| 171 | - | blogger := shared.GetLogger(r) | |
| 170 | + | dbpool := router.GetDB(r) | |
| 171 | + | blogger := router.GetLogger(r) | |
| 172 | 172 | logger := blogger.With("slug", slug, "user", username) | |
| 173 | 173 | ||
| 174 | 174 | user, err := dbpool.FindUserByName(username) |
| ... | ... | @@ -190,7 +190,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 190 | 190 | unlisted := false | |
| 191 | 191 | parsedText := "" | |
| 192 | 192 | // we dont want to syntax highlight huge files | |
| 193 | - | if post.FileSize > 1*utils.MB { | |
| 193 | + | if post.FileSize > 1*shared.MB { | |
| 194 | 194 | logger.Warn("paste too large to parse and apply syntax highlighting") | |
| 195 | 195 | parsedText = post.Text | |
| 196 | 196 | } else { |
| ... | ... | @@ -242,7 +242,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 242 | 242 | } | |
| 243 | 243 | } | |
| 244 | 244 | ||
| 245 | - | ts, err := shared.RenderTemplate(cfg, []string{ | |
| 245 | + | ts, err := router.RenderTemplate(cfg, []string{ | |
| 246 | 246 | cfg.StaticPath("html/post.page.tmpl"), | |
| 247 | 247 | }) | |
| 248 | 248 |
| ... | ... | @@ -259,19 +259,19 @@ func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 259 | 259 | } | |
| 260 | 260 | ||
| 261 | 261 | func postHandlerRaw(w http.ResponseWriter, r *http.Request) { | |
| 262 | - | username := shared.GetUsernameFromRequest(r) | |
| 263 | - | subdomain := shared.GetSubdomain(r) | |
| 264 | - | cfg := shared.GetCfg(r) | |
| 262 | + | username := router.GetUsernameFromRequest(r) | |
| 263 | + | subdomain := router.GetSubdomain(r) | |
| 264 | + | cfg := router.GetCfg(r) | |
| 265 | 265 | ||
| 266 | 266 | var slug string | |
| 267 | 267 | if !cfg.IsSubdomains() || subdomain == "" { | |
| 268 | - | slug, _ = url.PathUnescape(shared.GetField(r, 1)) | |
| 268 | + | slug, _ = url.PathUnescape(router.GetField(r, 1)) | |
| 269 | 269 | } else { | |
| 270 | - | slug, _ = url.PathUnescape(shared.GetField(r, 0)) | |
| 270 | + | slug, _ = url.PathUnescape(router.GetField(r, 0)) | |
| 271 | 271 | } | |
| 272 | 272 | ||
| 273 | - | dbpool := shared.GetDB(r) | |
| 274 | - | blogger := shared.GetLogger(r) | |
| 273 | + | dbpool := router.GetDB(r) | |
| 274 | + | blogger := router.GetLogger(r) | |
| 275 | 275 | logger := blogger.With("user", username, "slug", slug) | |
| 276 | 276 | ||
| 277 | 277 | user, err := dbpool.FindUserByName(username) |
| ... | ... | @@ -300,8 +300,8 @@ func postHandlerRaw(w http.ResponseWriter, r *http.Request) { | |
| 300 | 300 | ||
| 301 | 301 | func serveFile(file string, contentType string) http.HandlerFunc { | |
| 302 | 302 | return func(w http.ResponseWriter, r *http.Request) { | |
| 303 | - | logger := shared.GetLogger(r) | |
| 304 | - | cfg := shared.GetCfg(r) | |
| 303 | + | logger := router.GetLogger(r) | |
| 304 | + | cfg := router.GetCfg(r) | |
| 305 | 305 | ||
| 306 | 306 | contents, err := os.ReadFile(cfg.StaticPath(fmt.Sprintf("public/%s", file))) | |
| 307 | 307 | if err != nil { |
| ... | ... | @@ -318,25 +318,25 @@ func serveFile(file string, contentType string) http.HandlerFunc { | |
| 318 | 318 | } | |
| 319 | 319 | } | |
| 320 | 320 | ||
| 321 | - | func createStaticRoutes() []shared.Route { | |
| 322 | - | return []shared.Route{ | |
| 323 | - | shared.NewRoute("GET", "/main.css", serveFile("main.css", "text/css")), | |
| 324 | - | shared.NewRoute("GET", "/smol.css", serveFile("smol.css", "text/css")), | |
| 325 | - | shared.NewRoute("GET", "/syntax.css", serveFile("syntax.css", "text/css")), | |
| 326 | - | shared.NewRoute("GET", "/card.png", serveFile("card.png", "image/png")), | |
| 327 | - | shared.NewRoute("GET", "/favicon-16x16.png", serveFile("favicon-16x16.png", "image/png")), | |
| 328 | - | shared.NewRoute("GET", "/favicon-32x32.png", serveFile("favicon-32x32.png", "image/png")), | |
| 329 | - | shared.NewRoute("GET", "/apple-touch-icon.png", serveFile("apple-touch-icon.png", "image/png")), | |
| 330 | - | shared.NewRoute("GET", "/favicon.ico", serveFile("favicon.ico", "image/x-icon")), | |
| 331 | - | shared.NewRoute("GET", "/robots.txt", serveFile("robots.txt", "text/plain")), | |
| 321 | + | func createStaticRoutes() []router.Route { | |
| 322 | + | return []router.Route{ | |
| 323 | + | router.NewRoute("GET", "/main.css", serveFile("main.css", "text/css")), | |
| 324 | + | router.NewRoute("GET", "/smol.css", serveFile("smol.css", "text/css")), | |
| 325 | + | router.NewRoute("GET", "/syntax.css", serveFile("syntax.css", "text/css")), | |
| 326 | + | router.NewRoute("GET", "/card.png", serveFile("card.png", "image/png")), | |
| 327 | + | router.NewRoute("GET", "/favicon-16x16.png", serveFile("favicon-16x16.png", "image/png")), | |
| 328 | + | router.NewRoute("GET", "/favicon-32x32.png", serveFile("favicon-32x32.png", "image/png")), | |
| 329 | + | router.NewRoute("GET", "/apple-touch-icon.png", serveFile("apple-touch-icon.png", "image/png")), | |
| 330 | + | router.NewRoute("GET", "/favicon.ico", serveFile("favicon.ico", "image/x-icon")), | |
| 331 | + | router.NewRoute("GET", "/robots.txt", serveFile("robots.txt", "text/plain")), | |
| 332 | 332 | } | |
| 333 | 333 | } | |
| 334 | 334 | ||
| 335 | - | func createMainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 336 | - | routes := []shared.Route{ | |
| 337 | - | shared.NewRoute("GET", "/", shared.CreatePageHandler("html/marketing.page.tmpl")), | |
| 338 | - | shared.NewRoute("GET", "/check", shared.CheckHandler), | |
| 339 | - | shared.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 335 | + | func createMainRoutes(staticRoutes []router.Route) []router.Route { | |
| 336 | + | routes := []router.Route{ | |
| 337 | + | router.NewRoute("GET", "/", router.CreatePageHandler("html/marketing.page.tmpl")), | |
| 338 | + | router.NewRoute("GET", "/check", router.CheckHandler), | |
| 339 | + | router.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 340 | 340 | } | |
| 341 | 341 | ||
| 342 | 342 | routes = append( |
| ... | ... | @@ -346,18 +346,18 @@ func createMainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 346 | 346 | ||
| 347 | 347 | routes = append( | |
| 348 | 348 | routes, | |
| 349 | - | shared.NewRoute("GET", "/([^/]+)", blogHandler), | |
| 350 | - | shared.NewRoute("GET", "/([^/]+)/([^/]+)", postHandler), | |
| 351 | - | shared.NewRoute("GET", "/([^/]+)/([^/]+)/raw", postHandlerRaw), | |
| 352 | - | shared.NewRoute("GET", "/raw/([^/]+)/([^/]+)", postHandlerRaw), | |
| 349 | + | router.NewRoute("GET", "/([^/]+)", blogHandler), | |
| 350 | + | router.NewRoute("GET", "/([^/]+)/([^/]+)", postHandler), | |
| 351 | + | router.NewRoute("GET", "/([^/]+)/([^/]+)/raw", postHandlerRaw), | |
| 352 | + | router.NewRoute("GET", "/raw/([^/]+)/([^/]+)", postHandlerRaw), | |
| 353 | 353 | ) | |
| 354 | 354 | ||
| 355 | 355 | return routes | |
| 356 | 356 | } | |
| 357 | 357 | ||
| 358 | - | func createSubdomainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 359 | - | routes := []shared.Route{ | |
| 360 | - | shared.NewRoute("GET", "/", blogHandler), | |
| 358 | + | func createSubdomainRoutes(staticRoutes []router.Route) []router.Route { | |
| 359 | + | routes := []router.Route{ | |
| 360 | + | router.NewRoute("GET", "/", blogHandler), | |
| 361 | 361 | } | |
| 362 | 362 | ||
| 363 | 363 | routes = append( |
| ... | ... | @@ -367,9 +367,9 @@ func createSubdomainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 367 | 367 | ||
| 368 | 368 | routes = append( | |
| 369 | 369 | routes, | |
| 370 | - | shared.NewRoute("GET", "/([^/]+)", postHandler), | |
| 371 | - | shared.NewRoute("GET", "/([^/]+)/raw", postHandlerRaw), | |
| 372 | - | shared.NewRoute("GET", "/raw/([^/]+)", postHandlerRaw), | |
| 370 | + | router.NewRoute("GET", "/([^/]+)", postHandler), | |
| 371 | + | router.NewRoute("GET", "/([^/]+)/raw", postHandlerRaw), | |
| 372 | + | router.NewRoute("GET", "/raw/([^/]+)", postHandlerRaw), | |
| 373 | 373 | ) | |
| 374 | 374 | ||
| 375 | 375 | return routes |
| ... | ... | @@ -388,17 +388,17 @@ func StartApiServer() { | |
| 388 | 388 | staticRoutes := createStaticRoutes() | |
| 389 | 389 | ||
| 390 | 390 | if cfg.Debug { | |
| 391 | - | staticRoutes = shared.CreatePProfRoutes(staticRoutes) | |
| 391 | + | staticRoutes = router.CreatePProfRoutes(staticRoutes) | |
| 392 | 392 | } | |
| 393 | 393 | ||
| 394 | 394 | mainRoutes := createMainRoutes(staticRoutes) | |
| 395 | 395 | subdomainRoutes := createSubdomainRoutes(staticRoutes) | |
| 396 | 396 | ||
| 397 | - | apiConfig := &shared.ApiConfig{ | |
| 397 | + | apiConfig := &router.ApiConfig{ | |
| 398 | 398 | Cfg: cfg, | |
| 399 | 399 | Dbpool: db, | |
| 400 | 400 | } | |
| 401 | - | handler := shared.CreateServe(mainRoutes, subdomainRoutes, apiConfig) | |
| 401 | + | handler := router.CreateServe(mainRoutes, subdomainRoutes, apiConfig) | |
| 402 | 402 | router := http.HandlerFunc(handler) | |
| 403 | 403 | ||
| 404 | 404 | portStr := fmt.Sprintf(":%s", cfg.Port) |
+7,
-8
| ... | ... | @@ -4,16 +4,15 @@ import ( | |
| 4 | 4 | "strings" | |
| 5 | 5 | ||
| 6 | 6 | "github.com/picosh/pico/pkg/shared" | |
| 7 | - | "github.com/picosh/utils" | |
| 8 | 7 | ) | |
| 9 | 8 | ||
| 10 | 9 | func NewConfigSite(service string) *shared.ConfigSite { | |
| 11 | - | debug := utils.GetEnv("PASTES_DEBUG", "0") | |
| 12 | - | domain := utils.GetEnv("PASTES_DOMAIN", "pastes.sh") | |
| 13 | - | port := utils.GetEnv("PASTES_WEB_PORT", "3000") | |
| 14 | - | dbURL := utils.GetEnv("DATABASE_URL", "") | |
| 15 | - | protocol := utils.GetEnv("PASTES_PROTOCOL", "https") | |
| 16 | - | withPipe := strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 10 | + | debug := shared.GetEnv("PASTES_DEBUG", "0") | |
| 11 | + | domain := shared.GetEnv("PASTES_DOMAIN", "pastes.sh") | |
| 12 | + | port := shared.GetEnv("PASTES_WEB_PORT", "3000") | |
| 13 | + | dbURL := shared.GetEnv("DATABASE_URL", "") | |
| 14 | + | protocol := shared.GetEnv("PASTES_PROTOCOL", "https") | |
| 15 | + | withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 17 | 16 | ||
| 18 | 17 | return &shared.ConfigSite{ | |
| 19 | 18 | Debug: debug == "1", |
| ... | ... | @@ -23,6 +22,6 @@ func NewConfigSite(service string) *shared.ConfigSite { | |
| 23 | 22 | DbURL: dbURL, | |
| 24 | 23 | Space: "pastes", | |
| 25 | 24 | Logger: shared.CreateLogger(service, withPipe), | |
| 26 | - | MaxAssetSize: int64(3 * utils.MB), | |
| 25 | + | MaxAssetSize: int64(3 * shared.MB), | |
| 27 | 26 | } | |
| 28 | 27 | } |
+2,
-3
| ... | ... | @@ -11,7 +11,6 @@ import ( | |
| 11 | 11 | "github.com/picosh/pico/pkg/filehandlers" | |
| 12 | 12 | "github.com/picosh/pico/pkg/pssh" | |
| 13 | 13 | "github.com/picosh/pico/pkg/shared" | |
| 14 | - | "github.com/picosh/utils" | |
| 15 | 14 | ) | |
| 16 | 15 | ||
| 17 | 16 | var DEFAULT_EXPIRES_AT = 90 |
| ... | ... | @@ -22,7 +21,7 @@ type FileHooks struct { | |
| 22 | 21 | } | |
| 23 | 22 | ||
| 24 | 23 | func (p *FileHooks) FileValidate(s *pssh.SSHServerConnSession, data *filehandlers.PostMetaData) (bool, error) { | |
| 25 | - | if !utils.IsTextFile(string(data.Text)) { | |
| 24 | + | if !shared.IsTextFile(string(data.Text)) { | |
| 26 | 25 | err := fmt.Errorf( | |
| 27 | 26 | "ERROR: (%s) invalid file must be plain text (utf-8), skipping", | |
| 28 | 27 | data.Filename, |
| ... | ... | @@ -43,7 +42,7 @@ func (p *FileHooks) FileValidate(s *pssh.SSHServerConnSession, data *filehandler | |
| 43 | 42 | } | |
| 44 | 43 | ||
| 45 | 44 | func (p *FileHooks) FileMeta(s *pssh.SSHServerConnSession, data *filehandlers.PostMetaData) error { | |
| 46 | - | data.Title = utils.ToUpper(data.Slug) | |
| 45 | + | data.Title = shared.ToUpper(data.Slug) | |
| 47 | 46 | // we want the slug to be the filename for pastes | |
| 48 | 47 | data.Slug = data.Filename | |
| 49 | 48 |
+3,
-4
| ... | ... | @@ -17,15 +17,14 @@ import ( | |
| 17 | 17 | "github.com/picosh/pico/pkg/send/protocols/scp" | |
| 18 | 18 | "github.com/picosh/pico/pkg/send/protocols/sftp" | |
| 19 | 19 | "github.com/picosh/pico/pkg/shared" | |
| 20 | - | "github.com/picosh/utils" | |
| 21 | 20 | ) | |
| 22 | 21 | ||
| 23 | 22 | func StartSshServer() { | |
| 24 | 23 | appName := "pastes-ssh" | |
| 25 | 24 | ||
| 26 | - | host := utils.GetEnv("PASTES_HOST", "0.0.0.0") | |
| 27 | - | port := utils.GetEnv("PASTES_SSH_PORT", "2222") | |
| 28 | - | promPort := utils.GetEnv("PASTES_PROM_PORT", "9222") | |
| 25 | + | host := shared.GetEnv("PASTES_HOST", "0.0.0.0") | |
| 26 | + | port := shared.GetEnv("PASTES_SSH_PORT", "2222") | |
| 27 | + | promPort := shared.GetEnv("PASTES_PROM_PORT", "9222") | |
| 29 | 28 | cfg := NewConfigSite(appName) | |
| 30 | 29 | logger := cfg.Logger | |
| 31 | 30 |
+3,
-4
| ... | ... | @@ -14,7 +14,6 @@ import ( | |
| 14 | 14 | "github.com/picosh/pico/pkg/db" | |
| 15 | 15 | sst "github.com/picosh/pico/pkg/pobj/storage" | |
| 16 | 16 | "github.com/picosh/pico/pkg/shared" | |
| 17 | - | "github.com/picosh/utils" | |
| 18 | 17 | ) | |
| 19 | 18 | ||
| 20 | 19 | func NewTabWriter(out io.Writer) *tabwriter.Writer { |
| ... | ... | @@ -46,7 +45,7 @@ func projectTable(sesh io.Writer, projects []*db.Project) { | |
| 46 | 45 | ||
| 47 | 46 | type Cmd struct { | |
| 48 | 47 | User *db.User | |
| 49 | - | Session utils.CmdSession | |
| 48 | + | Session shared.CmdSession | |
| 50 | 49 | Log *slog.Logger | |
| 51 | 50 | Store sst.ObjectStorage | |
| 52 | 51 | Dbpool pgsdb.PgsDB |
| ... | ... | @@ -222,8 +221,8 @@ func (c *Cmd) stats(cfgMaxSize uint64) error { | |
| 222 | 221 | _, _ = fmt.Fprintf( | |
| 223 | 222 | writer, | |
| 224 | 223 | "%.4f\t%.4f\t%.4f\t%d\r\n", | |
| 225 | - | utils.BytesToGB(int(totalFileSize)), | |
| 226 | - | utils.BytesToGB(int(storageMax)), | |
| 224 | + | shared.BytesToGB(int(totalFileSize)), | |
| 225 | + | shared.BytesToGB(int(storageMax)), | |
| 227 | 226 | (float32(totalFileSize)/float32(storageMax))*100, | |
| 228 | 227 | len(projects), | |
| 229 | 228 | ) |
+11,
-11
| ... | ... | @@ -7,8 +7,8 @@ import ( | |
| 7 | 7 | "time" | |
| 8 | 8 | ||
| 9 | 9 | pgsdb "github.com/picosh/pico/pkg/apps/pgs/db" | |
| 10 | + | "github.com/picosh/pico/pkg/shared" | |
| 10 | 11 | "github.com/picosh/pico/pkg/shared/storage" | |
| 11 | - | "github.com/picosh/utils" | |
| 12 | 12 | ) | |
| 13 | 13 | ||
| 14 | 14 | type PgsConfig struct { |
| ... | ... | @@ -61,26 +61,26 @@ func (c *PgsConfig) StaticPath(fname string) string { | |
| 61 | 61 | return filepath.Join("pkg", "apps", "pgs", fname) | |
| 62 | 62 | } | |
| 63 | 63 | ||
| 64 | - | var maxSize = uint64(25 * utils.MB) | |
| 65 | - | var maxAssetSize = int64(10 * utils.MB) | |
| 64 | + | var maxSize = uint64(25 * shared.MB) | |
| 65 | + | var maxAssetSize = int64(10 * shared.MB) | |
| 66 | 66 | ||
| 67 | 67 | // Needs to be small for caching files like _headers and _redirects. | |
| 68 | - | var maxSpecialFileSize = int64(5 * utils.KB) | |
| 68 | + | var maxSpecialFileSize = int64(5 * shared.KB) | |
| 69 | 69 | ||
| 70 | 70 | func NewPgsConfig(logger *slog.Logger, dbpool pgsdb.PgsDB, st storage.StorageServe, pubsub PicoPubsub) *PgsConfig { | |
| 71 | - | domain := utils.GetEnv("PGS_DOMAIN", "pgs.sh") | |
| 72 | - | port := utils.GetEnv("PGS_WEB_PORT", "3000") | |
| 73 | - | protocol := utils.GetEnv("PGS_PROTOCOL", "https") | |
| 74 | - | cacheTTL, err := time.ParseDuration(utils.GetEnv("PGS_CACHE_TTL", "")) | |
| 71 | + | domain := shared.GetEnv("PGS_DOMAIN", "pgs.sh") | |
| 72 | + | port := shared.GetEnv("PGS_WEB_PORT", "3000") | |
| 73 | + | protocol := shared.GetEnv("PGS_PROTOCOL", "https") | |
| 74 | + | cacheTTL, err := time.ParseDuration(shared.GetEnv("PGS_CACHE_TTL", "")) | |
| 75 | 75 | if err != nil { | |
| 76 | 76 | cacheTTL = 600 * time.Second | |
| 77 | 77 | } | |
| 78 | - | cacheControl := utils.GetEnv( | |
| 78 | + | cacheControl := shared.GetEnv( | |
| 79 | 79 | "PGS_CACHE_CONTROL", | |
| 80 | 80 | fmt.Sprintf("max-age=%d", int(cacheTTL.Seconds()))) | |
| 81 | 81 | ||
| 82 | - | sshHost := utils.GetEnv("PGS_SSH_HOST", "0.0.0.0") | |
| 83 | - | sshPort := utils.GetEnv("PGS_SSH_PORT", "2222") | |
| 82 | + | sshHost := shared.GetEnv("PGS_SSH_HOST", "0.0.0.0") | |
| 83 | + | sshPort := shared.GetEnv("PGS_SSH_PORT", "2222") | |
| 84 | 84 | ||
| 85 | 85 | cfg := PgsConfig{ | |
| 86 | 86 | CacheControl: cacheControl, |
+4,
-4
| ... | ... | @@ -7,7 +7,7 @@ import ( | |
| 7 | 7 | ||
| 8 | 8 | "github.com/google/uuid" | |
| 9 | 9 | "github.com/picosh/pico/pkg/db" | |
| 10 | - | "github.com/picosh/utils" | |
| 10 | + | "github.com/picosh/pico/pkg/shared" | |
| 11 | 11 | ) | |
| 12 | 12 | ||
| 13 | 13 | type MemoryDB struct { |
| ... | ... | @@ -37,9 +37,9 @@ func (me *MemoryDB) SetupTestData() { | |
| 37 | 37 | feature := db.NewFeatureFlag( | |
| 38 | 38 | user.ID, | |
| 39 | 39 | "plus", | |
| 40 | - | uint64(25*utils.MB), | |
| 41 | - | int64(10*utils.MB), | |
| 42 | - | int64(5*utils.KB), | |
| 40 | + | uint64(25*shared.MB), | |
| 41 | + | int64(10*shared.MB), | |
| 42 | + | int64(5*shared.KB), | |
| 43 | 43 | ) | |
| 44 | 44 | expiresAt := time.Now().Add(time.Hour * 24) | |
| 45 | 45 | feature.ExpiresAt = &expiresAt |
+2,
-2
| ... | ... | @@ -8,7 +8,7 @@ import ( | |
| 8 | 8 | "github.com/jmoiron/sqlx" | |
| 9 | 9 | _ "github.com/lib/pq" | |
| 10 | 10 | "github.com/picosh/pico/pkg/db" | |
| 11 | - | "github.com/picosh/utils" | |
| 11 | + | "github.com/picosh/pico/pkg/shared" | |
| 12 | 12 | ) | |
| 13 | 13 | ||
| 14 | 14 | type PgsPsqlDB struct { |
| ... | ... | @@ -107,7 +107,7 @@ func (me *PgsPsqlDB) InsertAccessLog(log *db.AccessLog) error { | |
| 107 | 107 | } | |
| 108 | 108 | ||
| 109 | 109 | func (me *PgsPsqlDB) InsertProject(userID, name, projectDir string) (string, error) { | |
| 110 | - | if !utils.IsValidSubdomain(name) { | |
| 110 | + | if !shared.IsValidSubdomain(name) { | |
| 111 | 111 | return "", fmt.Errorf("'%s' is not a valid project name, must match /^[a-z0-9-]+$/", name) | |
| 112 | 112 | } | |
| 113 | 113 |
+3,
-4
| ... | ... | @@ -15,13 +15,12 @@ import ( | |
| 15 | 15 | "github.com/picosh/pico/pkg/send/protocols/sftp" | |
| 16 | 16 | "github.com/picosh/pico/pkg/shared" | |
| 17 | 17 | "github.com/picosh/pico/pkg/tunkit" | |
| 18 | - | "github.com/picosh/utils" | |
| 19 | 18 | ) | |
| 20 | 19 | ||
| 21 | 20 | func StartSshServer(cfg *PgsConfig, killCh chan error) { | |
| 22 | - | host := utils.GetEnv("PGS_HOST", "0.0.0.0") | |
| 23 | - | port := utils.GetEnv("PGS_SSH_PORT", "2222") | |
| 24 | - | promPort := utils.GetEnv("PGS_PROM_PORT", "9222") | |
| 21 | + | host := shared.GetEnv("PGS_HOST", "0.0.0.0") | |
| 22 | + | port := shared.GetEnv("PGS_SSH_PORT", "2222") | |
| 23 | + | promPort := shared.GetEnv("PGS_PROM_PORT", "9222") | |
| 25 | 24 | logger := cfg.Logger | |
| 26 | 25 | ||
| 27 | 26 | ctx, cancel := context.WithCancel(context.Background()) |
+3,
-3
| ... | ... | @@ -16,8 +16,8 @@ import ( | |
| 16 | 16 | ||
| 17 | 17 | pgsdb "github.com/picosh/pico/pkg/apps/pgs/db" | |
| 18 | 18 | "github.com/picosh/pico/pkg/db" | |
| 19 | + | "github.com/picosh/pico/pkg/shared" | |
| 19 | 20 | "github.com/picosh/pico/pkg/shared/storage" | |
| 20 | - | "github.com/picosh/utils" | |
| 21 | 21 | "github.com/pkg/sftp" | |
| 22 | 22 | "github.com/prometheus/client_golang/prometheus" | |
| 23 | 23 | "golang.org/x/crypto/ssh" |
| ... | ... | @@ -55,7 +55,7 @@ func TestSshServerSftp(t *testing.T) { | |
| 55 | 55 | dbpool.Pubkeys = append(dbpool.Pubkeys, &db.PublicKey{ | |
| 56 | 56 | ID: "nice-pubkey", | |
| 57 | 57 | UserID: dbpool.Users[0].ID, | |
| 58 | - | Key: utils.KeyForKeyText(user.signer.PublicKey()), | |
| 58 | + | Key: shared.KeyForKeyText(user.signer.PublicKey()), | |
| 59 | 59 | }) | |
| 60 | 60 | ||
| 61 | 61 | client, err := user.NewClient() |
| ... | ... | @@ -139,7 +139,7 @@ func TestSshServerRsync(t *testing.T) { | |
| 139 | 139 | time.Sleep(time.Millisecond * 100) | |
| 140 | 140 | ||
| 141 | 141 | user := GenerateUser() | |
| 142 | - | key := utils.KeyForKeyText(user.signer.PublicKey()) | |
| 142 | + | key := shared.KeyForKeyText(user.signer.PublicKey()) | |
| 143 | 143 | // add user's pubkey to the default test account | |
| 144 | 144 | dbpool.Pubkeys = append(dbpool.Pubkeys, &db.PublicKey{ | |
| 145 | 145 | ID: "nice-pubkey", |
+10,
-10
| ... | ... | @@ -8,7 +8,7 @@ import ( | |
| 8 | 8 | ||
| 9 | 9 | "github.com/picosh/pico/pkg/db" | |
| 10 | 10 | "github.com/picosh/pico/pkg/pssh" | |
| 11 | - | "github.com/picosh/pico/pkg/shared" | |
| 11 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 12 | 12 | "golang.org/x/crypto/ssh" | |
| 13 | 13 | ) | |
| 14 | 14 |
| ... | ... | @@ -30,7 +30,7 @@ func tunnelPerm(proj *db.Project) bool { | |
| 30 | 30 | ||
| 31 | 31 | func (web *TunnelWebRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 32 | 32 | ctx := r.Context() | |
| 33 | - | ctx = context.WithValue(ctx, shared.CtxSubdomainKey{}, web.subdomain) | |
| 33 | + | ctx = context.WithValue(ctx, router.CtxSubdomainKey{}, web.subdomain) | |
| 34 | 34 | web.UserRouter.ServeHTTP(w, r.WithContext(ctx)) | |
| 35 | 35 | } | |
| 36 | 36 |
| ... | ... | @@ -57,17 +57,17 @@ func CreateHttpHandler(cfg *PgsConfig) CtxHttpBridge { | |
| 57 | 57 | pubkey := ctx.Permissions().Extensions["pubkey"] | |
| 58 | 58 | if pubkey == "" { | |
| 59 | 59 | log.Error("pubkey not found in extensions", "subdomain", subdomain) | |
| 60 | - | return http.HandlerFunc(shared.UnauthorizedHandler) | |
| 60 | + | return http.HandlerFunc(router.UnauthorizedHandler) | |
| 61 | 61 | } | |
| 62 | 62 | ||
| 63 | 63 | log = log.With( | |
| 64 | 64 | "pubkey", pubkey, | |
| 65 | 65 | ) | |
| 66 | 66 | ||
| 67 | - | props, err := shared.GetProjectFromSubdomain(subdomain) | |
| 67 | + | props, err := router.GetProjectFromSubdomain(subdomain) | |
| 68 | 68 | if err != nil { | |
| 69 | 69 | log.Error("could not get project from subdomain", "err", err.Error()) | |
| 70 | - | return http.HandlerFunc(shared.UnauthorizedHandler) | |
| 70 | + | return http.HandlerFunc(router.UnauthorizedHandler) | |
| 71 | 71 | } | |
| 72 | 72 | ||
| 73 | 73 | owner, err := cfg.DB.FindUserByName(props.Username) |
| ... | ... | @@ -77,7 +77,7 @@ func CreateHttpHandler(cfg *PgsConfig) CtxHttpBridge { | |
| 77 | 77 | "name", props.Username, | |
| 78 | 78 | "err", err.Error(), | |
| 79 | 79 | ) | |
| 80 | - | return http.HandlerFunc(shared.UnauthorizedHandler) | |
| 80 | + | return http.HandlerFunc(router.UnauthorizedHandler) | |
| 81 | 81 | } | |
| 82 | 82 | log = log.With( | |
| 83 | 83 | "owner", owner.Name, |
| ... | ... | @@ -86,7 +86,7 @@ func CreateHttpHandler(cfg *PgsConfig) CtxHttpBridge { | |
| 86 | 86 | project, err := cfg.DB.FindProjectByName(owner.ID, props.ProjectName) | |
| 87 | 87 | if err != nil { | |
| 88 | 88 | log.Error("could not get project by name", "project", props.ProjectName, "err", err.Error()) | |
| 89 | - | return http.HandlerFunc(shared.UnauthorizedHandler) | |
| 89 | + | return http.HandlerFunc(router.UnauthorizedHandler) | |
| 90 | 90 | } | |
| 91 | 91 | ||
| 92 | 92 | requester, _ := cfg.DB.FindUserByPubkey(pubkey) |
| ... | ... | @@ -108,7 +108,7 @@ func CreateHttpHandler(cfg *PgsConfig) CtxHttpBridge { | |
| 108 | 108 | ||
| 109 | 109 | if !isAdmin { | |
| 110 | 110 | log.Error("impersonation attempt failed") | |
| 111 | - | return http.HandlerFunc(shared.UnauthorizedHandler) | |
| 111 | + | return http.HandlerFunc(router.UnauthorizedHandler) | |
| 112 | 112 | } | |
| 113 | 113 | requester, _ = cfg.DB.FindUserByName(asUser) | |
| 114 | 114 | } |
| ... | ... | @@ -117,11 +117,11 @@ func CreateHttpHandler(cfg *PgsConfig) CtxHttpBridge { | |
| 117 | 117 | publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubkey)) | |
| 118 | 118 | if err != nil { | |
| 119 | 119 | log.Error("could not parse public key", "pubkey", pubkey, "err", err) | |
| 120 | - | return http.HandlerFunc(shared.UnauthorizedHandler) | |
| 120 | + | return http.HandlerFunc(router.UnauthorizedHandler) | |
| 121 | 121 | } | |
| 122 | 122 | if !HasProjectAccess(project, owner, requester, publicKey) { | |
| 123 | 123 | log.Error("no access") | |
| 124 | - | return http.HandlerFunc(shared.UnauthorizedHandler) | |
| 124 | + | return http.HandlerFunc(router.UnauthorizedHandler) | |
| 125 | 125 | } | |
| 126 | 126 | ||
| 127 | 127 | log.Info("user has access to site") |
+7,
-8
| ... | ... | @@ -22,7 +22,6 @@ import ( | |
| 22 | 22 | "github.com/picosh/pico/pkg/pssh" | |
| 23 | 23 | sendutils "github.com/picosh/pico/pkg/send/utils" | |
| 24 | 24 | "github.com/picosh/pico/pkg/shared" | |
| 25 | - | "github.com/picosh/utils" | |
| 26 | 25 | ignore "github.com/sabhiram/go-gitignore" | |
| 27 | 26 | ) | |
| 28 | 27 |
| ... | ... | @@ -405,7 +404,7 @@ func (h *UploadAssetHandler) Write(s *pssh.SSHServerConnSession, entry *sendutil | |
| 405 | 404 | ||
| 406 | 405 | fsize, err := h.writeAsset( | |
| 407 | 406 | s, | |
| 408 | - | utils.NewMaxBytesReader(data.Reader, int64(sizeRemaining)), | |
| 407 | + | shared.NewMaxBytesReader(data.Reader, int64(sizeRemaining)), | |
| 409 | 408 | data, | |
| 410 | 409 | ) | |
| 411 | 410 | if err != nil { |
| ... | ... | @@ -413,10 +412,10 @@ func (h *UploadAssetHandler) Write(s *pssh.SSHServerConnSession, entry *sendutil | |
| 413 | 412 | cerr := fmt.Errorf( | |
| 414 | 413 | "%s: storage size %.2fmb, storage max %.2fmb, file max %.2fmb, special file max %.4fmb", | |
| 415 | 414 | err, | |
| 416 | - | utils.BytesToMB(int(curStorageSize)), | |
| 417 | - | utils.BytesToMB(int(storageMax)), | |
| 418 | - | utils.BytesToMB(int(fileMax)), | |
| 419 | - | utils.BytesToMB(int(specialFileMax)), | |
| 415 | + | shared.BytesToMB(int(curStorageSize)), | |
| 416 | + | shared.BytesToMB(int(storageMax)), | |
| 417 | + | shared.BytesToMB(int(fileMax)), | |
| 418 | + | shared.BytesToMB(int(specialFileMax)), | |
| 420 | 419 | ) | |
| 421 | 420 | return "", cerr | |
| 422 | 421 | } |
| ... | ... | @@ -434,8 +433,8 @@ func (h *UploadAssetHandler) Write(s *pssh.SSHServerConnSession, entry *sendutil | |
| 434 | 433 | str := fmt.Sprintf( | |
| 435 | 434 | "%s (space: %.2f/%.2fGB, %.2f%%)", | |
| 436 | 435 | url, | |
| 437 | - | utils.BytesToGB(int(nextStorageSize)), | |
| 438 | - | utils.BytesToGB(maxSize), | |
| 436 | + | shared.BytesToGB(int(nextStorageSize)), | |
| 437 | + | shared.BytesToGB(maxSize), | |
| 439 | 438 | (float32(nextStorageSize)/float32(maxSize))*100, | |
| 440 | 439 | ) | |
| 441 | 440 |
+13,
-13
| ... | ... | @@ -22,10 +22,10 @@ import ( | |
| 22 | 22 | "github.com/darkweak/storages/core" | |
| 23 | 23 | "github.com/gorilla/feeds" | |
| 24 | 24 | "github.com/hashicorp/golang-lru/v2/expirable" | |
| 25 | - | "github.com/picosh/pico/pkg/cache" | |
| 26 | 25 | "github.com/picosh/pico/pkg/db" | |
| 27 | 26 | sst "github.com/picosh/pico/pkg/pobj/storage" | |
| 28 | 27 | "github.com/picosh/pico/pkg/shared" | |
| 28 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 29 | 29 | "github.com/picosh/pico/pkg/shared/storage" | |
| 30 | 30 | "github.com/prometheus/client_golang/prometheus/promhttp" | |
| 31 | 31 | "google.golang.org/protobuf/proto" |
| ... | ... | @@ -115,8 +115,8 @@ func NewWebRouter(cfg *PgsConfig) *WebRouter { | |
| 115 | 115 | func newWebRouter(cfg *PgsConfig) *WebRouter { | |
| 116 | 116 | router := &WebRouter{ | |
| 117 | 117 | Cfg: cfg, | |
| 118 | - | RedirectsCache: expirable.NewLRU[string, []*RedirectRule](2048, nil, cache.CacheTimeout), | |
| 119 | - | HeadersCache: expirable.NewLRU[string, []*HeaderRule](2048, nil, cache.CacheTimeout), | |
| 118 | + | RedirectsCache: expirable.NewLRU[string, []*RedirectRule](2048, nil, shared.CacheTimeout), | |
| 119 | + | HeadersCache: expirable.NewLRU[string, []*HeaderRule](2048, nil, shared.CacheTimeout), | |
| 120 | 120 | } | |
| 121 | 121 | router.initRouters() | |
| 122 | 122 | return router |
| ... | ... | @@ -242,8 +242,8 @@ func (web *WebRouter) checkHandler(w http.ResponseWriter, r *http.Request) { | |
| 242 | 242 | appDomain := strings.Split(cfg.Domain, ":")[0] | |
| 243 | 243 | ||
| 244 | 244 | if !strings.Contains(hostDomain, appDomain) { | |
| 245 | - | subdomain := shared.GetCustomDomain(hostDomain, cfg.TxtPrefix) | |
| 246 | - | props, err := shared.GetProjectFromSubdomain(subdomain) | |
| 245 | + | subdomain := router.GetCustomDomain(hostDomain, cfg.TxtPrefix) | |
| 246 | + | props, err := router.GetProjectFromSubdomain(subdomain) | |
| 247 | 247 | if err != nil { | |
| 248 | 248 | logger.Error( | |
| 249 | 249 | "could not get project from subdomain", |
| ... | ... | @@ -448,7 +448,7 @@ func (web *WebRouter) ImageRequest(perm func(proj *db.Project) bool) http.Handle | |
| 448 | 448 | } | |
| 449 | 449 | ||
| 450 | 450 | func (web *WebRouter) ServeAsset(fname string, opts *storage.ImgProcessOpts, hasPerm HasPerm, w http.ResponseWriter, r *http.Request) { | |
| 451 | - | subdomain := shared.GetSubdomain(r) | |
| 451 | + | subdomain := router.GetSubdomain(r) | |
| 452 | 452 | ||
| 453 | 453 | logger := web.Cfg.Logger.With( | |
| 454 | 454 | "subdomain", subdomain, |
| ... | ... | @@ -457,7 +457,7 @@ func (web *WebRouter) ServeAsset(fname string, opts *storage.ImgProcessOpts, has | |
| 457 | 457 | "host", r.Host, | |
| 458 | 458 | ) | |
| 459 | 459 | ||
| 460 | - | props, err := shared.GetProjectFromSubdomain(subdomain) | |
| 460 | + | props, err := router.GetProjectFromSubdomain(subdomain) | |
| 461 | 461 | if err != nil { | |
| 462 | 462 | logger.Info( | |
| 463 | 463 | "could not determine project from subdomain", |
| ... | ... | @@ -542,23 +542,23 @@ func (web *WebRouter) ServeAsset(fname string, opts *storage.ImgProcessOpts, has | |
| 542 | 542 | } | |
| 543 | 543 | ||
| 544 | 544 | func (web *WebRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 545 | - | subdomain := shared.GetSubdomainFromRequest(r, web.Cfg.Domain, web.Cfg.TxtPrefix) | |
| 545 | + | subdomain := router.GetSubdomainFromRequest(r, web.Cfg.Domain, web.Cfg.TxtPrefix) | |
| 546 | 546 | if web.RootRouter == nil || web.UserRouter == nil { | |
| 547 | 547 | web.Cfg.Logger.Error("routers not initialized") | |
| 548 | 548 | http.Error(w, "routers not initialized", http.StatusInternalServerError) | |
| 549 | 549 | return | |
| 550 | 550 | } | |
| 551 | 551 | ||
| 552 | - | var router *http.ServeMux | |
| 552 | + | var mux *http.ServeMux | |
| 553 | 553 | if subdomain == "" { | |
| 554 | - | router = web.RootRouter | |
| 554 | + | mux = web.RootRouter | |
| 555 | 555 | } else { | |
| 556 | - | router = web.UserRouter | |
| 556 | + | mux = web.UserRouter | |
| 557 | 557 | } | |
| 558 | 558 | ||
| 559 | 559 | ctx := r.Context() | |
| 560 | - | ctx = context.WithValue(ctx, shared.CtxSubdomainKey{}, subdomain) | |
| 561 | - | router.ServeHTTP(w, r.WithContext(ctx)) | |
| 560 | + | ctx = context.WithValue(ctx, router.CtxSubdomainKey{}, subdomain) | |
| 561 | + | mux.ServeHTTP(w, r.WithContext(ctx)) | |
| 562 | 562 | } | |
| 563 | 563 | ||
| 564 | 564 | type CompatLogger struct { |
+7,
-8
| ... | ... | @@ -12,7 +12,6 @@ import ( | |
| 12 | 12 | "github.com/picosh/pico/pkg/db" | |
| 13 | 13 | "github.com/picosh/pico/pkg/pssh" | |
| 14 | 14 | "github.com/picosh/pico/pkg/shared" | |
| 15 | - | "github.com/picosh/utils" | |
| 16 | 15 | ||
| 17 | 16 | pipeLogger "github.com/picosh/utils/pipe/log" | |
| 18 | 17 | ) |
| ... | ... | @@ -22,7 +21,7 @@ func getUser(s *pssh.SSHServerConnSession, dbpool db.DB) (*db.User, error) { | |
| 22 | 21 | return nil, fmt.Errorf("key not found") | |
| 23 | 22 | } | |
| 24 | 23 | ||
| 25 | - | key := utils.KeyForKeyText(s.PublicKey()) | |
| 24 | + | key := shared.KeyForKeyText(s.PublicKey()) | |
| 26 | 25 | ||
| 27 | 26 | user, err := dbpool.FindUserByKey(s.User(), key) | |
| 28 | 27 | if err != nil { |
| ... | ... | @@ -39,7 +38,7 @@ func getUser(s *pssh.SSHServerConnSession, dbpool db.DB) (*db.User, error) { | |
| 39 | 38 | type Cmd struct { | |
| 40 | 39 | User *db.User | |
| 41 | 40 | SshSession *pssh.SSHServerConnSession | |
| 42 | - | Session utils.CmdSession | |
| 41 | + | Session shared.CmdSession | |
| 43 | 42 | Log *slog.Logger | |
| 44 | 43 | Dbpool db.DB | |
| 45 | 44 | Write bool |
| ... | ... | @@ -77,9 +76,9 @@ func (c *Cmd) user() { | |
| 77 | 76 | } | |
| 78 | 77 | ||
| 79 | 78 | func (c *Cmd) notFound(host, interval string) error { | |
| 80 | - | origin := utils.StartOfYear() | |
| 79 | + | origin := shared.StartOfYear() | |
| 81 | 80 | if interval == "month" { | |
| 82 | - | origin = utils.StartOfMonth() | |
| 81 | + | origin = shared.StartOfMonth() | |
| 83 | 82 | } | |
| 84 | 83 | c.output(fmt.Sprintf("starting from: %s\n", origin.Format(time.RFC3339))) | |
| 85 | 84 | urls, err := c.Dbpool.VisitUrlNotFound(&db.SummaryOpts{ |
| ... | ... | @@ -155,10 +154,10 @@ func (c *Cmd) logs(ctx context.Context) error { | |
| 155 | 154 | continue | |
| 156 | 155 | } | |
| 157 | 156 | ||
| 158 | - | user := utils.AnyToStr(parsedData, "user") | |
| 159 | - | userId := utils.AnyToStr(parsedData, "userId") | |
| 157 | + | user := shared.AnyToStr(parsedData, "user") | |
| 158 | + | userId := shared.AnyToStr(parsedData, "userId") | |
| 160 | 159 | ||
| 161 | - | hidden := utils.AnyToBool(parsedData, "hidden") | |
| 160 | + | hidden := shared.AnyToBool(parsedData, "hidden") | |
| 162 | 161 | ||
| 163 | 162 | if !hidden && (user == c.User.Name || userId == c.User.ID) { | |
| 164 | 163 | select { |
+3,
-4
| ... | ... | @@ -4,13 +4,12 @@ import ( | |
| 4 | 4 | "strings" | |
| 5 | 5 | ||
| 6 | 6 | "github.com/picosh/pico/pkg/shared" | |
| 7 | - | "github.com/picosh/utils" | |
| 8 | 7 | ) | |
| 9 | 8 | ||
| 10 | 9 | func NewConfigSite(service string) *shared.ConfigSite { | |
| 11 | - | dbURL := utils.GetEnv("DATABASE_URL", "") | |
| 12 | - | tuns := utils.GetEnv("TUNS_CONSOLE_SECRET", "") | |
| 13 | - | withPipe := strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 10 | + | dbURL := shared.GetEnv("DATABASE_URL", "") | |
| 11 | + | tuns := shared.GetEnv("TUNS_CONSOLE_SECRET", "") | |
| 12 | + | withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 14 | 13 | ||
| 15 | 14 | return &shared.ConfigSite{ | |
| 16 | 15 | DbURL: dbURL, |
+2,
-3
| ... | ... | @@ -15,7 +15,6 @@ import ( | |
| 15 | 15 | "github.com/picosh/pico/pkg/pssh" | |
| 16 | 16 | sendutils "github.com/picosh/pico/pkg/send/utils" | |
| 17 | 17 | "github.com/picosh/pico/pkg/shared" | |
| 18 | - | "github.com/picosh/utils" | |
| 19 | 18 | "golang.org/x/crypto/ssh" | |
| 20 | 19 | ) | |
| 21 | 20 |
| ... | ... | @@ -242,7 +241,7 @@ func (h *UploadHandler) ProcessAuthorizedKeys(text []byte, logger *slog.Logger, | |
| 242 | 241 | diff := authorizedKeysDiff(s.PublicKey(), curKeys, nextKeys) | |
| 243 | 242 | ||
| 244 | 243 | for _, pk := range diff.Add { | |
| 245 | - | key := utils.KeyForKeyText(pk.Pk) | |
| 244 | + | key := shared.KeyForKeyText(pk.Pk) | |
| 246 | 245 | ||
| 247 | 246 | _, _ = fmt.Fprintf(s.Stderr(), "adding pubkey (%s)\n", key) | |
| 248 | 247 | logger.Info("adding pubkey", "pubkey", key) |
| ... | ... | @@ -255,7 +254,7 @@ func (h *UploadHandler) ProcessAuthorizedKeys(text []byte, logger *slog.Logger, | |
| 255 | 254 | } | |
| 256 | 255 | ||
| 257 | 256 | for _, pk := range diff.Update { | |
| 258 | - | key := utils.KeyForKeyText(pk.Pk) | |
| 257 | + | key := shared.KeyForKeyText(pk.Pk) | |
| 259 | 258 | ||
| 260 | 259 | _, _ = fmt.Fprintf(s.Stderr(), "updating pubkey with comment: %s (%s)\n", pk.Comment, key) | |
| 261 | 260 | logger.Info( |
+4,
-5
| ... | ... | @@ -19,7 +19,6 @@ import ( | |
| 19 | 19 | "github.com/picosh/pico/pkg/send/protocols/sftp" | |
| 20 | 20 | "github.com/picosh/pico/pkg/shared" | |
| 21 | 21 | "github.com/picosh/pico/pkg/tui" | |
| 22 | - | "github.com/picosh/utils" | |
| 23 | 22 | "golang.org/x/crypto/ssh" | |
| 24 | 23 | ) | |
| 25 | 24 |
| ... | ... | @@ -41,9 +40,9 @@ func createTui(shrd *tui.SharedModel) pssh.SSHServerMiddleware { | |
| 41 | 40 | func StartSshServer() { | |
| 42 | 41 | appName := "pico-ssh" | |
| 43 | 42 | ||
| 44 | - | host := utils.GetEnv("PICO_HOST", "0.0.0.0") | |
| 45 | - | port := utils.GetEnv("PICO_SSH_PORT", "2222") | |
| 46 | - | promPort := utils.GetEnv("PICO_PROM_PORT", "9222") | |
| 43 | + | host := shared.GetEnv("PICO_HOST", "0.0.0.0") | |
| 44 | + | port := shared.GetEnv("PICO_SSH_PORT", "2222") | |
| 45 | + | promPort := shared.GetEnv("PICO_PROM_PORT", "9222") | |
| 47 | 46 | cfg := NewConfigSite(appName) | |
| 48 | 47 | logger := cfg.Logger | |
| 49 | 48 |
| ... | ... | @@ -84,7 +83,7 @@ func StartSshServer() { | |
| 84 | 83 | if perms == nil { | |
| 85 | 84 | perms = &ssh.Permissions{ | |
| 86 | 85 | Extensions: map[string]string{ | |
| 87 | - | "pubkey": utils.KeyForKeyText(key), | |
| 86 | + | "pubkey": shared.KeyForKeyText(key), | |
| 88 | 87 | }, | |
| 89 | 88 | } | |
| 90 | 89 | } |
+37,
-36
| ... | ... | @@ -20,6 +20,7 @@ import ( | |
| 20 | 20 | "github.com/picosh/pico/pkg/db" | |
| 21 | 21 | "github.com/picosh/pico/pkg/db/postgres" | |
| 22 | 22 | "github.com/picosh/pico/pkg/shared" | |
| 23 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 23 | 24 | "github.com/picosh/utils/pipe" | |
| 24 | 25 | "github.com/prometheus/client_golang/prometheus/promhttp" | |
| 25 | 26 | ) |
| ... | ... | @@ -36,8 +37,8 @@ var ( | |
| 36 | 37 | ||
| 37 | 38 | func serveFile(file string, contentType string) http.HandlerFunc { | |
| 38 | 39 | return func(w http.ResponseWriter, r *http.Request) { | |
| 39 | - | logger := shared.GetLogger(r) | |
| 40 | - | cfg := shared.GetCfg(r) | |
| 40 | + | logger := router.GetLogger(r) | |
| 41 | + | cfg := router.GetCfg(r) | |
| 41 | 42 | ||
| 42 | 43 | contents, err := os.ReadFile(cfg.StaticPath(fmt.Sprintf("public/%s", file))) | |
| 43 | 44 | if err != nil { |
| ... | ... | @@ -54,18 +55,18 @@ func serveFile(file string, contentType string) http.HandlerFunc { | |
| 54 | 55 | } | |
| 55 | 56 | } | |
| 56 | 57 | ||
| 57 | - | func createStaticRoutes() []shared.Route { | |
| 58 | - | return []shared.Route{ | |
| 59 | - | shared.NewRoute("GET", "/main.css", serveFile("main.css", "text/css")), | |
| 60 | - | shared.NewRoute("GET", "/smol.css", serveFile("smol.css", "text/css")), | |
| 61 | - | shared.NewRoute("GET", "/syntax.css", serveFile("syntax.css", "text/css")), | |
| 62 | - | shared.NewRoute("GET", "/card.png", serveFile("card.png", "image/png")), | |
| 63 | - | shared.NewRoute("GET", "/favicon-16x16.png", serveFile("favicon-16x16.png", "image/png")), | |
| 64 | - | shared.NewRoute("GET", "/favicon-32x32.png", serveFile("favicon-32x32.png", "image/png")), | |
| 65 | - | shared.NewRoute("GET", "/apple-touch-icon.png", serveFile("apple-touch-icon.png", "image/png")), | |
| 66 | - | shared.NewRoute("GET", "/favicon.ico", serveFile("favicon.ico", "image/x-icon")), | |
| 67 | - | shared.NewRoute("GET", "/robots.txt", serveFile("robots.txt", "text/plain")), | |
| 68 | - | shared.NewRoute("GET", "/anim.js", serveFile("anim.js", "text/javascript")), | |
| 58 | + | func createStaticRoutes() []router.Route { | |
| 59 | + | return []router.Route{ | |
| 60 | + | router.NewRoute("GET", "/main.css", serveFile("main.css", "text/css")), | |
| 61 | + | router.NewRoute("GET", "/smol.css", serveFile("smol.css", "text/css")), | |
| 62 | + | router.NewRoute("GET", "/syntax.css", serveFile("syntax.css", "text/css")), | |
| 63 | + | router.NewRoute("GET", "/card.png", serveFile("card.png", "image/png")), | |
| 64 | + | router.NewRoute("GET", "/favicon-16x16.png", serveFile("favicon-16x16.png", "image/png")), | |
| 65 | + | router.NewRoute("GET", "/favicon-32x32.png", serveFile("favicon-32x32.png", "image/png")), | |
| 66 | + | router.NewRoute("GET", "/apple-touch-icon.png", serveFile("apple-touch-icon.png", "image/png")), | |
| 67 | + | router.NewRoute("GET", "/favicon.ico", serveFile("favicon.ico", "image/x-icon")), | |
| 68 | + | router.NewRoute("GET", "/robots.txt", serveFile("robots.txt", "text/plain")), | |
| 69 | + | router.NewRoute("GET", "/anim.js", serveFile("anim.js", "text/javascript")), | |
| 69 | 70 | } | |
| 70 | 71 | } | |
| 71 | 72 |
| ... | ... | @@ -86,10 +87,10 @@ var _ io.Writer = writeFlusher{} | |
| 86 | 87 | ||
| 87 | 88 | func handleSub(pubsub bool) http.HandlerFunc { | |
| 88 | 89 | return func(w http.ResponseWriter, r *http.Request) { | |
| 89 | - | logger := shared.GetLogger(r) | |
| 90 | + | logger := router.GetLogger(r) | |
| 90 | 91 | ||
| 91 | 92 | clientInfo := shared.NewPicoPipeClient() | |
| 92 | - | topic, _ := url.PathUnescape(shared.GetField(r, 0)) | |
| 93 | + | topic, _ := url.PathUnescape(router.GetField(r, 0)) | |
| 93 | 94 | ||
| 94 | 95 | topic = cleanRegex.ReplaceAllString(topic, "") | |
| 95 | 96 |
| ... | ... | @@ -139,10 +140,10 @@ func handleSub(pubsub bool) http.HandlerFunc { | |
| 139 | 140 | ||
| 140 | 141 | func handlePub(pubsub bool) http.HandlerFunc { | |
| 141 | 142 | return func(w http.ResponseWriter, r *http.Request) { | |
| 142 | - | logger := shared.GetLogger(r) | |
| 143 | + | logger := router.GetLogger(r) | |
| 143 | 144 | ||
| 144 | 145 | clientInfo := shared.NewPicoPipeClient() | |
| 145 | - | topic, _ := url.PathUnescape(shared.GetField(r, 0)) | |
| 146 | + | topic, _ := url.PathUnescape(router.GetField(r, 0)) | |
| 146 | 147 | ||
| 147 | 148 | topic = cleanRegex.ReplaceAllString(topic, "") | |
| 148 | 149 |
| ... | ... | @@ -283,7 +284,7 @@ func handlePub(pubsub bool) http.HandlerFunc { | |
| 283 | 284 | ||
| 284 | 285 | func handlePipe() http.HandlerFunc { | |
| 285 | 286 | return func(w http.ResponseWriter, r *http.Request) { | |
| 286 | - | logger := shared.GetLogger(r) | |
| 287 | + | logger := router.GetLogger(r) | |
| 287 | 288 | ||
| 288 | 289 | c, err := upgrader.Upgrade(w, r, nil) | |
| 289 | 290 | if err != nil { |
| ... | ... | @@ -296,7 +297,7 @@ func handlePipe() http.HandlerFunc { | |
| 296 | 297 | }() | |
| 297 | 298 | ||
| 298 | 299 | clientInfo := shared.NewPicoPipeClient() | |
| 299 | - | topic, _ := url.PathUnescape(shared.GetField(r, 0)) | |
| 300 | + | topic, _ := url.PathUnescape(router.GetField(r, 0)) | |
| 300 | 301 | ||
| 301 | 302 | topic = cleanRegex.ReplaceAllString(topic, "") | |
| 302 | 303 |
| ... | ... | @@ -435,7 +436,7 @@ func handlePipe() http.HandlerFunc { | |
| 435 | 436 | ||
| 436 | 437 | func rssHandler(cfg *shared.ConfigSite, dbpool db.DB) http.HandlerFunc { | |
| 437 | 438 | return func(w http.ResponseWriter, r *http.Request) { | |
| 438 | - | apiToken, _ := url.PathUnescape(shared.GetField(r, 0)) | |
| 439 | + | apiToken, _ := url.PathUnescape(router.GetField(r, 0)) | |
| 439 | 440 | user, err := dbpool.FindUserByToken(apiToken) | |
| 440 | 441 | if err != nil { | |
| 441 | 442 | cfg.Logger.Error( |
| ... | ... | @@ -470,20 +471,20 @@ func rssHandler(cfg *shared.ConfigSite, dbpool db.DB) http.HandlerFunc { | |
| 470 | 471 | } | |
| 471 | 472 | } | |
| 472 | 473 | ||
| 473 | - | func createMainRoutes(staticRoutes []shared.Route, cfg *shared.ConfigSite, dbpool db.DB) []shared.Route { | |
| 474 | - | routes := []shared.Route{ | |
| 475 | - | shared.NewRoute("GET", "/", shared.CreatePageHandler("html/marketing.page.tmpl")), | |
| 476 | - | shared.NewRoute("GET", "/check", shared.CheckHandler), | |
| 477 | - | shared.NewRoute("GET", "/rss/(.+)", rssHandler(cfg, dbpool)), | |
| 478 | - | shared.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 474 | + | func createMainRoutes(staticRoutes []router.Route, cfg *shared.ConfigSite, dbpool db.DB) []router.Route { | |
| 475 | + | routes := []router.Route{ | |
| 476 | + | router.NewRoute("GET", "/", router.CreatePageHandler("html/marketing.page.tmpl")), | |
| 477 | + | router.NewRoute("GET", "/check", router.CheckHandler), | |
| 478 | + | router.NewRoute("GET", "/rss/(.+)", rssHandler(cfg, dbpool)), | |
| 479 | + | router.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 479 | 480 | } | |
| 480 | 481 | ||
| 481 | - | pipeRoutes := []shared.Route{ | |
| 482 | - | shared.NewRoute("GET", "/topic/(.+)", handleSub(false)), | |
| 483 | - | shared.NewRoute("POST", "/topic/(.+)", handlePub(false)), | |
| 484 | - | shared.NewRoute("GET", "/pubsub/(.+)", handleSub(true)), | |
| 485 | - | shared.NewRoute("POST", "/pubsub/(.+)", handlePub(true)), | |
| 486 | - | shared.NewRoute("GET", "/pipe/(.+)", handlePipe()), | |
| 482 | + | pipeRoutes := []router.Route{ | |
| 483 | + | router.NewRoute("GET", "/topic/(.+)", handleSub(false)), | |
| 484 | + | router.NewRoute("POST", "/topic/(.+)", handlePub(false)), | |
| 485 | + | router.NewRoute("GET", "/pubsub/(.+)", handleSub(true)), | |
| 486 | + | router.NewRoute("POST", "/pubsub/(.+)", handlePub(true)), | |
| 487 | + | router.NewRoute("GET", "/pipe/(.+)", handlePipe()), | |
| 487 | 488 | } | |
| 488 | 489 | ||
| 489 | 490 | for _, route := range pipeRoutes { |
| ... | ... | @@ -510,7 +511,7 @@ func StartApiServer() { | |
| 510 | 511 | staticRoutes := createStaticRoutes() | |
| 511 | 512 | ||
| 512 | 513 | if cfg.Debug { | |
| 513 | - | staticRoutes = shared.CreatePProfRoutes(staticRoutes) | |
| 514 | + | staticRoutes = router.CreatePProfRoutes(staticRoutes) | |
| 514 | 515 | } | |
| 515 | 516 | ||
| 516 | 517 | mainRoutes := createMainRoutes(staticRoutes, cfg, db) |
| ... | ... | @@ -541,11 +542,11 @@ func StartApiServer() { | |
| 541 | 542 | } | |
| 542 | 543 | }() | |
| 543 | 544 | ||
| 544 | - | apiConfig := &shared.ApiConfig{ | |
| 545 | + | apiConfig := &router.ApiConfig{ | |
| 545 | 546 | Cfg: cfg, | |
| 546 | 547 | Dbpool: db, | |
| 547 | 548 | } | |
| 548 | - | handler := shared.CreateServe(mainRoutes, subdomainRoutes, apiConfig) | |
| 549 | + | handler := router.CreateServe(mainRoutes, subdomainRoutes, apiConfig) | |
| 549 | 550 | router := http.HandlerFunc(handler) | |
| 550 | 551 | ||
| 551 | 552 | portStr := fmt.Sprintf(":%s", cfg.Port) |
+5,
-6
| ... | ... | @@ -4,15 +4,14 @@ import ( | |
| 4 | 4 | "strings" | |
| 5 | 5 | ||
| 6 | 6 | "github.com/picosh/pico/pkg/shared" | |
| 7 | - | "github.com/picosh/utils" | |
| 8 | 7 | ) | |
| 9 | 8 | ||
| 10 | 9 | func NewConfigSite(service string) *shared.ConfigSite { | |
| 11 | - | domain := utils.GetEnv("PIPE_DOMAIN", "pipe.pico.sh") | |
| 12 | - | port := utils.GetEnv("PIPE_WEB_PORT", "3000") | |
| 13 | - | dbURL := utils.GetEnv("DATABASE_URL", "") | |
| 14 | - | protocol := utils.GetEnv("PIPE_PROTOCOL", "https") | |
| 15 | - | withPipe := strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 10 | + | domain := shared.GetEnv("PIPE_DOMAIN", "pipe.pico.sh") | |
| 11 | + | port := shared.GetEnv("PIPE_WEB_PORT", "3000") | |
| 12 | + | dbURL := shared.GetEnv("DATABASE_URL", "") | |
| 13 | + | protocol := shared.GetEnv("PIPE_PROTOCOL", "https") | |
| 14 | + | withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 16 | 15 | ||
| 17 | 16 | return &shared.ConfigSite{ | |
| 18 | 17 | Domain: domain, |
+5,
-6
| ... | ... | @@ -11,17 +11,16 @@ import ( | |
| 11 | 11 | "github.com/picosh/pico/pkg/pssh" | |
| 12 | 12 | "github.com/picosh/pico/pkg/shared" | |
| 13 | 13 | psub "github.com/picosh/pubsub" | |
| 14 | - | "github.com/picosh/utils" | |
| 15 | 14 | "golang.org/x/crypto/ssh" | |
| 16 | 15 | ) | |
| 17 | 16 | ||
| 18 | 17 | func StartSshServer() { | |
| 19 | 18 | appName := "pipe-ssh" | |
| 20 | 19 | ||
| 21 | - | host := utils.GetEnv("PIPE_HOST", "0.0.0.0") | |
| 22 | - | port := utils.GetEnv("PIPE_SSH_PORT", "2222") | |
| 23 | - | portOverride := utils.GetEnv("PIPE_SSH_PORT_OVERRIDE", port) | |
| 24 | - | promPort := utils.GetEnv("PIPE_PROM_PORT", "9222") | |
| 20 | + | host := shared.GetEnv("PIPE_HOST", "0.0.0.0") | |
| 21 | + | port := shared.GetEnv("PIPE_SSH_PORT", "2222") | |
| 22 | + | portOverride := shared.GetEnv("PIPE_SSH_PORT_OVERRIDE", port) | |
| 23 | + | promPort := shared.GetEnv("PIPE_PROM_PORT", "9222") | |
| 25 | 24 | cfg := NewConfigSite(appName) | |
| 26 | 25 | logger := cfg.Logger | |
| 27 | 26 |
| ... | ... | @@ -62,7 +61,7 @@ func StartSshServer() { | |
| 62 | 61 | if perms == nil { | |
| 63 | 62 | perms = &ssh.Permissions{ | |
| 64 | 63 | Extensions: map[string]string{ | |
| 65 | - | "pubkey": utils.KeyForKeyText(key), | |
| 64 | + | "pubkey": shared.KeyForKeyText(key), | |
| 66 | 65 | }, | |
| 67 | 66 | } | |
| 68 | 67 | } |
+2,
-3
| ... | ... | @@ -18,7 +18,6 @@ import ( | |
| 18 | 18 | "github.com/picosh/pico/pkg/pssh" | |
| 19 | 19 | "github.com/picosh/pico/pkg/shared" | |
| 20 | 20 | psub "github.com/picosh/pubsub" | |
| 21 | - | "github.com/picosh/utils" | |
| 22 | 21 | "github.com/prometheus/client_golang/prometheus" | |
| 23 | 22 | "golang.org/x/crypto/ssh" | |
| 24 | 23 | ) |
| ... | ... | @@ -225,7 +224,7 @@ func NewTestSSHServer(t *testing.T) *TestSSHServer { | |
| 225 | 224 | if perms == nil { | |
| 226 | 225 | perms = &ssh.Permissions{ | |
| 227 | 226 | Extensions: map[string]string{ | |
| 228 | - | "pubkey": utils.KeyForKeyText(key), | |
| 227 | + | "pubkey": shared.KeyForKeyText(key), | |
| 229 | 228 | }, | |
| 230 | 229 | } | |
| 231 | 230 | } |
| ... | ... | @@ -294,7 +293,7 @@ func GenerateUser(username string) UserSSH { | |
| 294 | 293 | } | |
| 295 | 294 | ||
| 296 | 295 | func (u UserSSH) PublicKey() string { | |
| 297 | - | return utils.KeyForKeyText(u.signer.PublicKey()) | |
| 296 | + | return shared.KeyForKeyText(u.signer.PublicKey()) | |
| 298 | 297 | } | |
| 299 | 298 | ||
| 300 | 299 | func (u UserSSH) NewClient() (*ssh.Client, error) { |
+97,
-97
| ... | ... | @@ -19,8 +19,8 @@ import ( | |
| 19 | 19 | "github.com/picosh/pico/pkg/db" | |
| 20 | 20 | "github.com/picosh/pico/pkg/db/postgres" | |
| 21 | 21 | "github.com/picosh/pico/pkg/shared" | |
| 22 | + | "github.com/picosh/pico/pkg/shared/router" | |
| 22 | 23 | "github.com/picosh/pico/pkg/shared/storage" | |
| 23 | - | "github.com/picosh/utils" | |
| 24 | 24 | "github.com/prometheus/client_golang/prometheus/promhttp" | |
| 25 | 25 | ) | |
| 26 | 26 |
| ... | ... | @@ -124,10 +124,10 @@ func GetBlogName(username string) string { | |
| 124 | 124 | } | |
| 125 | 125 | ||
| 126 | 126 | func blogStyleHandler(w http.ResponseWriter, r *http.Request) { | |
| 127 | - | username := shared.GetUsernameFromRequest(r) | |
| 128 | - | dbpool := shared.GetDB(r) | |
| 129 | - | logger := shared.GetLogger(r) | |
| 130 | - | cfg := shared.GetCfg(r) | |
| 127 | + | username := router.GetUsernameFromRequest(r) | |
| 128 | + | dbpool := router.GetDB(r) | |
| 129 | + | logger := router.GetLogger(r) | |
| 130 | + | cfg := router.GetCfg(r) | |
| 131 | 131 | ||
| 132 | 132 | user, err := dbpool.FindUserByName(username) | |
| 133 | 133 | if err != nil { |
| ... | ... | @@ -154,10 +154,10 @@ func blogStyleHandler(w http.ResponseWriter, r *http.Request) { | |
| 154 | 154 | } | |
| 155 | 155 | ||
| 156 | 156 | func blogHandler(w http.ResponseWriter, r *http.Request) { | |
| 157 | - | username := shared.GetUsernameFromRequest(r) | |
| 158 | - | dbpool := shared.GetDB(r) | |
| 159 | - | logger := shared.GetLogger(r) | |
| 160 | - | cfg := shared.GetCfg(r) | |
| 157 | + | username := router.GetUsernameFromRequest(r) | |
| 158 | + | dbpool := router.GetDB(r) | |
| 159 | + | logger := router.GetLogger(r) | |
| 160 | + | cfg := router.GetCfg(r) | |
| 161 | 161 | ||
| 162 | 162 | user, err := dbpool.FindUserByName(username) | |
| 163 | 163 | if err != nil { |
| ... | ... | @@ -184,7 +184,7 @@ func blogHandler(w http.ResponseWriter, r *http.Request) { | |
| 184 | 184 | return | |
| 185 | 185 | } | |
| 186 | 186 | ||
| 187 | - | ts, err := shared.RenderTemplate(cfg, []string{ | |
| 187 | + | ts, err := router.RenderTemplate(cfg, []string{ | |
| 188 | 188 | cfg.StaticPath("html/blog-default.partial.tmpl"), | |
| 189 | 189 | cfg.StaticPath("html/blog-aside.partial.tmpl"), | |
| 190 | 190 | cfg.StaticPath("html/blog.page.tmpl"), |
| ... | ... | @@ -257,10 +257,10 @@ func blogHandler(w http.ResponseWriter, r *http.Request) { | |
| 257 | 257 | p := PostItemData{ | |
| 258 | 258 | URL: template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)), | |
| 259 | 259 | BlogURL: template.URL(cfg.FullBlogURL(curl, post.Username)), | |
| 260 | - | Title: utils.FilenameToTitle(post.Filename, post.Title), | |
| 260 | + | Title: shared.FilenameToTitle(post.Filename, post.Title), | |
| 261 | 261 | PublishAt: post.PublishAt.Format(time.DateOnly), | |
| 262 | 262 | PublishAtISO: post.PublishAt.Format(time.RFC3339), | |
| 263 | - | UpdatedTimeAgo: utils.TimeAgo(post.UpdatedAt), | |
| 263 | + | UpdatedTimeAgo: shared.TimeAgo(post.UpdatedAt), | |
| 264 | 264 | UpdatedAtISO: post.UpdatedAt.Format(time.RFC3339), | |
| 265 | 265 | } | |
| 266 | 266 | postCollection = append(postCollection, p) |
| ... | ... | @@ -289,20 +289,20 @@ func blogHandler(w http.ResponseWriter, r *http.Request) { | |
| 289 | 289 | } | |
| 290 | 290 | ||
| 291 | 291 | func postRawHandler(w http.ResponseWriter, r *http.Request) { | |
| 292 | - | username := shared.GetUsernameFromRequest(r) | |
| 293 | - | subdomain := shared.GetSubdomain(r) | |
| 294 | - | cfg := shared.GetCfg(r) | |
| 292 | + | username := router.GetUsernameFromRequest(r) | |
| 293 | + | subdomain := router.GetSubdomain(r) | |
| 294 | + | cfg := router.GetCfg(r) | |
| 295 | 295 | ||
| 296 | 296 | var slug string | |
| 297 | 297 | if !cfg.IsSubdomains() || subdomain == "" { | |
| 298 | - | slug, _ = url.PathUnescape(shared.GetField(r, 1)) | |
| 298 | + | slug, _ = url.PathUnescape(router.GetField(r, 1)) | |
| 299 | 299 | } else { | |
| 300 | - | slug, _ = url.PathUnescape(shared.GetField(r, 0)) | |
| 300 | + | slug, _ = url.PathUnescape(router.GetField(r, 0)) | |
| 301 | 301 | } | |
| 302 | 302 | slug = strings.TrimSuffix(slug, "/") | |
| 303 | 303 | ||
| 304 | - | dbpool := shared.GetDB(r) | |
| 305 | - | logger := shared.GetLogger(r) | |
| 304 | + | dbpool := router.GetDB(r) | |
| 305 | + | logger := router.GetLogger(r) | |
| 306 | 306 | logger = logger.With("slug", slug) | |
| 307 | 307 | ||
| 308 | 308 | user, err := dbpool.FindUserByName(username) |
| ... | ... | @@ -330,10 +330,10 @@ func postRawHandler(w http.ResponseWriter, r *http.Request) { | |
| 330 | 330 | } | |
| 331 | 331 | ||
| 332 | 332 | func robotsHandler(w http.ResponseWriter, r *http.Request) { | |
| 333 | - | username := shared.GetUsernameFromRequest(r) | |
| 334 | - | cfg := shared.GetCfg(r) | |
| 335 | - | dbpool := shared.GetDB(r) | |
| 336 | - | logger := shared.GetLogger(r) | |
| 333 | + | username := router.GetUsernameFromRequest(r) | |
| 334 | + | cfg := router.GetCfg(r) | |
| 335 | + | dbpool := router.GetDB(r) | |
| 336 | + | logger := router.GetLogger(r) | |
| 337 | 337 | user, err := dbpool.FindUserByName(username) | |
| 338 | 338 | if err != nil { | |
| 339 | 339 | logger.Info("blog not found", "user", username) |
| ... | ... | @@ -356,20 +356,20 @@ func robotsHandler(w http.ResponseWriter, r *http.Request) { | |
| 356 | 356 | } | |
| 357 | 357 | ||
| 358 | 358 | func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 359 | - | username := shared.GetUsernameFromRequest(r) | |
| 360 | - | subdomain := shared.GetSubdomain(r) | |
| 361 | - | cfg := shared.GetCfg(r) | |
| 359 | + | username := router.GetUsernameFromRequest(r) | |
| 360 | + | subdomain := router.GetSubdomain(r) | |
| 361 | + | cfg := router.GetCfg(r) | |
| 362 | 362 | ||
| 363 | 363 | var slug string | |
| 364 | 364 | if !cfg.IsSubdomains() || subdomain == "" { | |
| 365 | - | slug, _ = url.PathUnescape(shared.GetField(r, 1)) | |
| 365 | + | slug, _ = url.PathUnescape(router.GetField(r, 1)) | |
| 366 | 366 | } else { | |
| 367 | - | slug, _ = url.PathUnescape(shared.GetField(r, 0)) | |
| 367 | + | slug, _ = url.PathUnescape(router.GetField(r, 0)) | |
| 368 | 368 | } | |
| 369 | 369 | slug = strings.TrimSuffix(slug, "/") | |
| 370 | 370 | ||
| 371 | - | dbpool := shared.GetDB(r) | |
| 372 | - | logger := shared.GetLogger(r) | |
| 371 | + | dbpool := router.GetDB(r) | |
| 372 | + | logger := router.GetLogger(r) | |
| 373 | 373 | ||
| 374 | 374 | user, err := dbpool.FindUserByName(username) | |
| 375 | 375 | if err != nil { |
| ... | ... | @@ -473,7 +473,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 473 | 473 | URL: template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)), | |
| 474 | 474 | BlogURL: template.URL(cfg.FullBlogURL(curl, username)), | |
| 475 | 475 | Description: post.Description, | |
| 476 | - | Title: utils.FilenameToTitle(post.Filename, post.Title), | |
| 476 | + | Title: shared.FilenameToTitle(post.Filename, post.Title), | |
| 477 | 477 | Slug: post.Slug, | |
| 478 | 478 | PublishAt: post.PublishAt.Format(time.DateOnly), | |
| 479 | 479 | PublishAtISO: post.PublishAt.Format(time.RFC3339), |
| ... | ... | @@ -544,7 +544,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 544 | 544 | w.WriteHeader(http.StatusNotFound) | |
| 545 | 545 | } | |
| 546 | 546 | ||
| 547 | - | ts, err := shared.RenderTemplate(cfg, []string{ | |
| 547 | + | ts, err := router.RenderTemplate(cfg, []string{ | |
| 548 | 548 | cfg.StaticPath("html/list.partial.tmpl"), | |
| 549 | 549 | cfg.StaticPath("html/post.page.tmpl"), | |
| 550 | 550 | }) |
| ... | ... | @@ -563,9 +563,9 @@ func postHandler(w http.ResponseWriter, r *http.Request) { | |
| 563 | 563 | } | |
| 564 | 564 | ||
| 565 | 565 | func readHandler(w http.ResponseWriter, r *http.Request) { | |
| 566 | - | dbpool := shared.GetDB(r) | |
| 567 | - | logger := shared.GetLogger(r) | |
| 568 | - | cfg := shared.GetCfg(r) | |
| 566 | + | dbpool := router.GetDB(r) | |
| 567 | + | logger := router.GetLogger(r) | |
| 568 | + | cfg := router.GetCfg(r) | |
| 569 | 569 | ||
| 570 | 570 | page, _ := strconv.Atoi(r.URL.Query().Get("page")) | |
| 571 | 571 | tag := r.URL.Query().Get("tag") |
| ... | ... | @@ -583,7 +583,7 @@ func readHandler(w http.ResponseWriter, r *http.Request) { | |
| 583 | 583 | return | |
| 584 | 584 | } | |
| 585 | 585 | ||
| 586 | - | ts, err := shared.RenderTemplate(cfg, []string{ | |
| 586 | + | ts, err := router.RenderTemplate(cfg, []string{ | |
| 587 | 587 | cfg.StaticPath("html/read.page.tmpl"), | |
| 588 | 588 | }) | |
| 589 | 589 |
| ... | ... | @@ -625,12 +625,12 @@ func readHandler(w http.ResponseWriter, r *http.Request) { | |
| 625 | 625 | item := PostItemData{ | |
| 626 | 626 | URL: template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)), | |
| 627 | 627 | BlogURL: template.URL(cfg.FullBlogURL(curl, post.Username)), | |
| 628 | - | Title: utils.FilenameToTitle(post.Filename, post.Title), | |
| 628 | + | Title: shared.FilenameToTitle(post.Filename, post.Title), | |
| 629 | 629 | Description: post.Description, | |
| 630 | 630 | Username: post.Username, | |
| 631 | 631 | PublishAt: post.PublishAt.Format(time.DateOnly), | |
| 632 | 632 | PublishAtISO: post.PublishAt.Format(time.RFC3339), | |
| 633 | - | UpdatedTimeAgo: utils.TimeAgo(post.UpdatedAt), | |
| 633 | + | UpdatedTimeAgo: shared.TimeAgo(post.UpdatedAt), | |
| 634 | 634 | UpdatedAtISO: post.UpdatedAt.Format(time.RFC3339), | |
| 635 | 635 | } | |
| 636 | 636 | data.Posts = append(data.Posts, item) |
| ... | ... | @@ -644,10 +644,10 @@ func readHandler(w http.ResponseWriter, r *http.Request) { | |
| 644 | 644 | } | |
| 645 | 645 | ||
| 646 | 646 | func rssBlogHandler(w http.ResponseWriter, r *http.Request) { | |
| 647 | - | username := shared.GetUsernameFromRequest(r) | |
| 648 | - | dbpool := shared.GetDB(r) | |
| 649 | - | logger := shared.GetLogger(r) | |
| 650 | - | cfg := shared.GetCfg(r) | |
| 647 | + | username := router.GetUsernameFromRequest(r) | |
| 648 | + | dbpool := router.GetDB(r) | |
| 649 | + | logger := router.GetLogger(r) | |
| 650 | + | cfg := router.GetCfg(r) | |
| 651 | 651 | ||
| 652 | 652 | user, err := dbpool.FindUserByName(username) | |
| 653 | 653 | if err != nil { |
| ... | ... | @@ -675,7 +675,7 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) { | |
| 675 | 675 | return | |
| 676 | 676 | } | |
| 677 | 677 | ||
| 678 | - | ts, err := template.New("rss.page.tmpl").Funcs(shared.FuncMap).ParseFiles( | |
| 678 | + | ts, err := template.New("rss.page.tmpl").Funcs(router.FuncMap).ParseFiles( | |
| 679 | 679 | cfg.StaticPath("html/list.partial.tmpl"), | |
| 680 | 680 | cfg.StaticPath("html/rss.page.tmpl"), | |
| 681 | 681 | ) |
| ... | ... | @@ -775,7 +775,7 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) { | |
| 775 | 775 | ||
| 776 | 776 | item := &feeds.Item{ | |
| 777 | 777 | Id: feedId, | |
| 778 | - | Title: utils.FilenameToTitle(post.Filename, post.Title), | |
| 778 | + | Title: shared.FilenameToTitle(post.Filename, post.Title), | |
| 779 | 779 | Link: &feeds.Link{Href: realUrl}, | |
| 780 | 780 | Content: content, | |
| 781 | 781 | Updated: *post.PublishAt, |
| ... | ... | @@ -805,9 +805,9 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) { | |
| 805 | 805 | } | |
| 806 | 806 | ||
| 807 | 807 | func rssHandler(w http.ResponseWriter, r *http.Request) { | |
| 808 | - | dbpool := shared.GetDB(r) | |
| 809 | - | logger := shared.GetLogger(r) | |
| 810 | - | cfg := shared.GetCfg(r) | |
| 808 | + | dbpool := router.GetDB(r) | |
| 809 | + | logger := router.GetLogger(r) | |
| 810 | + | cfg := router.GetCfg(r) | |
| 811 | 811 | ||
| 812 | 812 | pager, err := dbpool.FindPostsByFeed(&db.Pager{Num: 25, Page: 0}, cfg.Space) | |
| 813 | 813 | if err != nil { |
| ... | ... | @@ -816,7 +816,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) { | |
| 816 | 816 | return | |
| 817 | 817 | } | |
| 818 | 818 | ||
| 819 | - | ts, err := template.New("rss.page.tmpl").Funcs(shared.FuncMap).ParseFiles( | |
| 819 | + | ts, err := template.New("rss.page.tmpl").Funcs(router.FuncMap).ParseFiles( | |
| 820 | 820 | cfg.StaticPath("html/list.partial.tmpl"), | |
| 821 | 821 | cfg.StaticPath("html/rss.page.tmpl"), | |
| 822 | 822 | ) |
| ... | ... | @@ -907,8 +907,8 @@ func rssHandler(w http.ResponseWriter, r *http.Request) { | |
| 907 | 907 | ||
| 908 | 908 | func serveFile(file string, contentType string) http.HandlerFunc { | |
| 909 | 909 | return func(w http.ResponseWriter, r *http.Request) { | |
| 910 | - | logger := shared.GetLogger(r) | |
| 911 | - | cfg := shared.GetCfg(r) | |
| 910 | + | logger := router.GetLogger(r) | |
| 911 | + | cfg := router.GetCfg(r) | |
| 912 | 912 | ||
| 913 | 913 | contents, err := os.ReadFile(cfg.StaticPath(fmt.Sprintf("public/%s", file))) | |
| 914 | 914 | if err != nil { |
| ... | ... | @@ -925,29 +925,29 @@ func serveFile(file string, contentType string) http.HandlerFunc { | |
| 925 | 925 | } | |
| 926 | 926 | } | |
| 927 | 927 | ||
| 928 | - | func createStaticRoutes() []shared.Route { | |
| 929 | - | return []shared.Route{ | |
| 930 | - | shared.NewRoute("GET", "/main.css", serveFile("main.css", "text/css")), | |
| 931 | - | shared.NewRoute("GET", "/smol.css", serveFile("smol.css", "text/css")), | |
| 932 | - | shared.NewRoute("GET", "/smol-v2.css", serveFile("smol-v2.css", "text/css")), | |
| 933 | - | shared.NewRoute("GET", "/syntax.css", serveFile("syntax.css", "text/css")), | |
| 934 | - | shared.NewRoute("GET", "/card.png", serveFile("card.png", "image/png")), | |
| 935 | - | shared.NewRoute("GET", "/favicon-16x16.png", serveFile("favicon-16x16.png", "image/png")), | |
| 936 | - | shared.NewRoute("GET", "/favicon-32x32.png", serveFile("favicon-32x32.png", "image/png")), | |
| 937 | - | shared.NewRoute("GET", "/apple-touch-icon.png", serveFile("apple-touch-icon.png", "image/png")), | |
| 938 | - | shared.NewRoute("GET", "/favicon.ico", serveFile("favicon.ico", "image/x-icon")), | |
| 939 | - | shared.NewRoute("GET", "/robots.txt", serveFile("robots.txt", "text/plain")), | |
| 928 | + | func createStaticRoutes() []router.Route { | |
| 929 | + | return []router.Route{ | |
| 930 | + | router.NewRoute("GET", "/main.css", serveFile("main.css", "text/css")), | |
| 931 | + | router.NewRoute("GET", "/smol.css", serveFile("smol.css", "text/css")), | |
| 932 | + | router.NewRoute("GET", "/smol-v2.css", serveFile("smol-v2.css", "text/css")), | |
| 933 | + | router.NewRoute("GET", "/syntax.css", serveFile("syntax.css", "text/css")), | |
| 934 | + | router.NewRoute("GET", "/card.png", serveFile("card.png", "image/png")), | |
| 935 | + | router.NewRoute("GET", "/favicon-16x16.png", serveFile("favicon-16x16.png", "image/png")), | |
| 936 | + | router.NewRoute("GET", "/favicon-32x32.png", serveFile("favicon-32x32.png", "image/png")), | |
| 937 | + | router.NewRoute("GET", "/apple-touch-icon.png", serveFile("apple-touch-icon.png", "image/png")), | |
| 938 | + | router.NewRoute("GET", "/favicon.ico", serveFile("favicon.ico", "image/x-icon")), | |
| 939 | + | router.NewRoute("GET", "/robots.txt", serveFile("robots.txt", "text/plain")), | |
| 940 | 940 | } | |
| 941 | 941 | } | |
| 942 | 942 | ||
| 943 | - | func createMainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 944 | - | routes := []shared.Route{ | |
| 945 | - | shared.NewRoute("GET", "/", readHandler), | |
| 946 | - | shared.NewRoute("GET", "/read", readHandler), | |
| 947 | - | shared.NewRoute("GET", "/check", shared.CheckHandler), | |
| 948 | - | shared.NewRoute("GET", "/rss", rssHandler), | |
| 949 | - | shared.NewRoute("GET", "/rss.atom", rssHandler), | |
| 950 | - | shared.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 943 | + | func createMainRoutes(staticRoutes []router.Route) []router.Route { | |
| 944 | + | routes := []router.Route{ | |
| 945 | + | router.NewRoute("GET", "/", readHandler), | |
| 946 | + | router.NewRoute("GET", "/read", readHandler), | |
| 947 | + | router.NewRoute("GET", "/check", router.CheckHandler), | |
| 948 | + | router.NewRoute("GET", "/rss", rssHandler), | |
| 949 | + | router.NewRoute("GET", "/rss.atom", rssHandler), | |
| 950 | + | router.NewRoute("GET", "/_metrics", promhttp.Handler().ServeHTTP), | |
| 951 | 951 | } | |
| 952 | 952 | ||
| 953 | 953 | routes = append( |
| ... | ... | @@ -959,10 +959,10 @@ func createMainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 959 | 959 | } | |
| 960 | 960 | ||
| 961 | 961 | func imgRequest(w http.ResponseWriter, r *http.Request) { | |
| 962 | - | logger := shared.GetLogger(r) | |
| 963 | - | st := shared.GetStorage(r) | |
| 964 | - | dbpool := shared.GetDB(r) | |
| 965 | - | username := shared.GetUsernameFromRequest(r) | |
| 962 | + | logger := router.GetLogger(r) | |
| 963 | + | st := router.GetStorage(r) | |
| 964 | + | dbpool := router.GetDB(r) | |
| 965 | + | username := router.GetUsernameFromRequest(r) | |
| 966 | 966 | user, err := dbpool.FindUserByName(username) | |
| 967 | 967 | if err != nil { | |
| 968 | 968 | logger.Error("could not find user", "username", username) |
| ... | ... | @@ -971,8 +971,8 @@ func imgRequest(w http.ResponseWriter, r *http.Request) { | |
| 971 | 971 | } | |
| 972 | 972 | logger = shared.LoggerWithUser(logger, user) | |
| 973 | 973 | ||
| 974 | - | rawname := shared.GetField(r, 0) | |
| 975 | - | imgOpts := shared.GetField(r, 1) | |
| 974 | + | rawname := router.GetField(r, 0) | |
| 975 | + | imgOpts := router.GetField(r, 1) | |
| 976 | 976 | // we place all prose images inside a "prose" folder | |
| 977 | 977 | fname := filepath.Join("/prose", rawname) | |
| 978 | 978 |
| ... | ... | @@ -1037,17 +1037,17 @@ func imgRequest(w http.ResponseWriter, r *http.Request) { | |
| 1037 | 1037 | } | |
| 1038 | 1038 | } | |
| 1039 | 1039 | ||
| 1040 | - | func createSubdomainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 1041 | - | routes := []shared.Route{ | |
| 1042 | - | shared.NewRoute("GET", "/", blogHandler), | |
| 1043 | - | shared.NewRoute("GET", "/_styles.css", blogStyleHandler), | |
| 1044 | - | shared.NewRoute("GET", "/robots.txt", robotsHandler), | |
| 1045 | - | shared.NewRoute("GET", "/rss", rssBlogHandler), | |
| 1046 | - | shared.NewRoute("GET", "/rss.xml", rssBlogHandler), | |
| 1047 | - | shared.NewRoute("GET", "/atom.xml", rssBlogHandler), | |
| 1048 | - | shared.NewRoute("GET", "/feed.xml", rssBlogHandler), | |
| 1049 | - | shared.NewRoute("GET", "/atom", rssBlogHandler), | |
| 1050 | - | shared.NewRoute("GET", "/blog/index.xml", rssBlogHandler), | |
| 1040 | + | func createSubdomainRoutes(staticRoutes []router.Route) []router.Route { | |
| 1041 | + | routes := []router.Route{ | |
| 1042 | + | router.NewRoute("GET", "/", blogHandler), | |
| 1043 | + | router.NewRoute("GET", "/_styles.css", blogStyleHandler), | |
| 1044 | + | router.NewRoute("GET", "/robots.txt", robotsHandler), | |
| 1045 | + | router.NewRoute("GET", "/rss", rssBlogHandler), | |
| 1046 | + | router.NewRoute("GET", "/rss.xml", rssBlogHandler), | |
| 1047 | + | router.NewRoute("GET", "/atom.xml", rssBlogHandler), | |
| 1048 | + | router.NewRoute("GET", "/feed.xml", rssBlogHandler), | |
| 1049 | + | router.NewRoute("GET", "/atom", rssBlogHandler), | |
| 1050 | + | router.NewRoute("GET", "/blog/index.xml", rssBlogHandler), | |
| 1051 | 1051 | } | |
| 1052 | 1052 | ||
| 1053 | 1053 | routes = append( |
| ... | ... | @@ -1057,13 +1057,13 @@ func createSubdomainRoutes(staticRoutes []shared.Route) []shared.Route { | |
| 1057 | 1057 | ||
| 1058 | 1058 | routes = append( | |
| 1059 | 1059 | routes, | |
| 1060 | - | shared.NewRoute("GET", "/raw/(.+)", postRawHandler), | |
| 1061 | - | shared.NewRoute("GET", "/(.+).md", postRawHandler), | |
| 1062 | - | shared.NewRoute("GET", "/(.+).lxt", postRawHandler), | |
| 1063 | - | shared.NewRoute("GET", `/(.+\.(?:jpg|jpeg|png|gif|webp|svg|ico))/(.+)`, imgRequest), | |
| 1064 | - | shared.NewRoute("GET", `/(.+\.(?:jpg|jpeg|png|gif|webp|svg|ico))$`, imgRequest), | |
| 1065 | - | shared.NewRoute("GET", "/(.+).html", postHandler), | |
| 1066 | - | shared.NewRoute("GET", "/(.+)", postHandler), | |
| 1060 | + | router.NewRoute("GET", "/raw/(.+)", postRawHandler), | |
| 1061 | + | router.NewRoute("GET", "/(.+).md", postRawHandler), | |
| 1062 | + | router.NewRoute("GET", "/(.+).lxt", postRawHandler), | |
| 1063 | + | router.NewRoute("GET", `/(.+\.(?:jpg|jpeg|png|gif|webp|svg|ico))/(.+)`, imgRequest), | |
| 1064 | + | router.NewRoute("GET", `/(.+\.(?:jpg|jpeg|png|gif|webp|svg|ico))$`, imgRequest), | |
| 1065 | + | router.NewRoute("GET", "/(.+).html", postHandler), | |
| 1066 | + | router.NewRoute("GET", "/(.+)", postHandler), | |
| 1067 | 1067 | ) | |
| 1068 | 1068 | ||
| 1069 | 1069 | return routes |
| ... | ... | @@ -1087,18 +1087,18 @@ func StartApiServer() { | |
| 1087 | 1087 | staticRoutes := createStaticRoutes() | |
| 1088 | 1088 | ||
| 1089 | 1089 | if cfg.Debug { | |
| 1090 | - | staticRoutes = shared.CreatePProfRoutes(staticRoutes) | |
| 1090 | + | staticRoutes = router.CreatePProfRoutes(staticRoutes) | |
| 1091 | 1091 | } | |
| 1092 | 1092 | ||
| 1093 | 1093 | mainRoutes := createMainRoutes(staticRoutes) | |
| 1094 | 1094 | subdomainRoutes := createSubdomainRoutes(staticRoutes) | |
| 1095 | 1095 | ||
| 1096 | - | apiConfig := &shared.ApiConfig{ | |
| 1096 | + | apiConfig := &router.ApiConfig{ | |
| 1097 | 1097 | Cfg: cfg, | |
| 1098 | 1098 | Dbpool: dbpool, | |
| 1099 | 1099 | Storage: st, | |
| 1100 | 1100 | } | |
| 1101 | - | handler := shared.CreateServe(mainRoutes, subdomainRoutes, apiConfig) | |
| 1101 | + | handler := router.CreateServe(mainRoutes, subdomainRoutes, apiConfig) | |
| 1102 | 1102 | router := http.HandlerFunc(handler) | |
| 1103 | 1103 | ||
| 1104 | 1104 | portStr := fmt.Sprintf(":%s", cfg.Port) |
+9,
-10
| ... | ... | @@ -4,20 +4,19 @@ import ( | |
| 4 | 4 | "strings" | |
| 5 | 5 | ||
| 6 | 6 | "github.com/picosh/pico/pkg/shared" | |
| 7 | - | "github.com/picosh/utils" | |
| 8 | 7 | ) | |
| 9 | 8 | ||
| 10 | - | var MAX_FILE_SIZE = 3 * utils.MB | |
| 9 | + | var MAX_FILE_SIZE = 3 * shared.MB | |
| 11 | 10 | ||
| 12 | 11 | func NewConfigSite(service string) *shared.ConfigSite { | |
| 13 | - | debug := utils.GetEnv("PROSE_DEBUG", "0") | |
| 14 | - | domain := utils.GetEnv("PROSE_DOMAIN", "prose.sh") | |
| 15 | - | port := utils.GetEnv("PROSE_WEB_PORT", "3000") | |
| 16 | - | protocol := utils.GetEnv("PROSE_PROTOCOL", "https") | |
| 17 | - | dbURL := utils.GetEnv("DATABASE_URL", "") | |
| 18 | - | maxSize := uint64(25 * utils.MB) | |
| 19 | - | maxImgSize := int64(10 * utils.MB) | |
| 20 | - | withPipe := strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 12 | + | debug := shared.GetEnv("PROSE_DEBUG", "0") | |
| 13 | + | domain := shared.GetEnv("PROSE_DOMAIN", "prose.sh") | |
| 14 | + | port := shared.GetEnv("PROSE_WEB_PORT", "3000") | |
| 15 | + | protocol := shared.GetEnv("PROSE_PROTOCOL", "https") | |
| 16 | + | dbURL := shared.GetEnv("DATABASE_URL", "") | |
| 17 | + | maxSize := uint64(25 * shared.MB) | |
| 18 | + | maxImgSize := int64(10 * shared.MB) | |
| 19 | + | withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" | |
| 21 | 20 | ||
| 22 | 21 | return &shared.ConfigSite{ | |
| 23 | 22 | Debug: debug == "1", |
+4,
-5
| ... | ... | @@ -11,7 +11,6 @@ import ( | |
| 11 | 11 | "github.com/picosh/pico/pkg/filehandlers" | |
| 12 | 12 | "github.com/picosh/pico/pkg/pssh" | |
| 13 | 13 | "github.com/picosh/pico/pkg/shared" | |
| 14 | - | "github.com/picosh/utils" | |
| 15 | 14 | pipeUtil "github.com/picosh/utils/pipe" | |
| 16 | 15 | ) | |
| 17 | 16 |
| ... | ... | @@ -22,7 +21,7 @@ type MarkdownHooks struct { | |
| 22 | 21 | } | |
| 23 | 22 | ||
| 24 | 23 | func (p *MarkdownHooks) FileValidate(s *pssh.SSHServerConnSession, data *filehandlers.PostMetaData) (bool, error) { | |
| 25 | - | if !utils.IsTextFile(data.Text) { | |
| 24 | + | if !shared.IsTextFile(data.Text) { | |
| 26 | 25 | err := fmt.Errorf( | |
| 27 | 26 | "ERROR: (%s) invalid file must be plain text (utf-8), skipping", | |
| 28 | 27 | data.Filename, |
| ... | ... | @@ -42,7 +41,7 @@ func (p *MarkdownHooks) FileValidate(s *pssh.SSHServerConnSession, data *filehan | |
| 42 | 41 | return true, nil | |
| 43 | 42 | } | |
| 44 | 43 | ||
| 45 | - | if !utils.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) { | |
| 44 | + | if !shared.IsExtAllowed(data.Filename, p.Cfg.AllowedExt) { | |
| 46 | 45 | extStr := strings.Join(p.Cfg.AllowedExt, ",") | |
| 47 | 46 | err := fmt.Errorf( | |
| 48 | 47 | "ERROR: (%s) invalid file, format must be (%s), skipping", |
| ... | ... | @@ -67,7 +66,7 @@ func (p *MarkdownHooks) metaLxt(data *filehandlers.PostMetaData) error { | |
| 67 | 66 | parsedText := shared.ListParseText(data.Text) | |
| 68 | 67 | ||
| 69 | 68 | if parsedText.Title == "" { | |
| 70 | - | data.Title = utils.ToUpper(data.Slug) | |
| 69 | + | data.Title = shared.ToUpper(data.Slug) | |
| 71 | 70 | } else { | |
| 72 | 71 | data.Title = parsedText.Title | |
| 73 | 72 | } |
| ... | ... | @@ -91,7 +90,7 @@ func (p *MarkdownHooks) metaMd(data *filehandlers.PostMetaData) error { | |
| 91 | 90 | } | |
| 92 | 91 | ||
| 93 | 92 | if parsedText.Title == "" { | |
| 94 | - | data.Title = utils.ToUpper(data.Slug) | |
| 93 | + | data.Title = shared.ToUpper(data.Slug) | |
| 95 | 94 | } else { | |
| 96 | 95 | data.Title = parsedText.Title | |
| 97 | 96 | } |
+3,
-4
| ... | ... | @@ -19,15 +19,14 @@ import ( | |
| 19 | 19 | "github.com/picosh/pico/pkg/send/protocols/sftp" | |
| 20 | 20 | "github.com/picosh/pico/pkg/shared" | |
| 21 | 21 | "github.com/picosh/pico/pkg/shared/storage" | |
| 22 | - | "github.com/picosh/utils" | |
| 23 | 22 | ) | |
| 24 | 23 | ||
| 25 | 24 | func StartSshServer() { | |
| 26 | 25 | appName := "prose-ssh" | |
| 27 | 26 | ||
| 28 | - | host := utils.GetEnv("PROSE_HOST", "0.0.0.0") | |
| 29 | - | port := utils.GetEnv("PROSE_SSH_PORT", "2222") | |
| 30 | - | promPort := utils.GetEnv("PROSE_PROM_PORT", "9222") | |
| 27 | + | host := shared.GetEnv("PROSE_HOST", "0.0.0.0") | |
| 28 | + | port := shared.GetEnv("PROSE_SSH_PORT", "2222") | |
| 29 | + | promPort := shared.GetEnv("PROSE_PROM_PORT", "9222") | |
| 31 | 30 | cfg := NewConfigSite(appName) | |
| 32 | 31 | logger := cfg.Logger | |
| 33 | 32 |
+0,
-21
| ... | ... | @@ -1,21 +0,0 @@ | |
| 1 | - | package cache | |
| 2 | - | ||
| 3 | - | import ( | |
| 4 | - | "log/slog" | |
| 5 | - | "time" | |
| 6 | - | ||
| 7 | - | "github.com/picosh/utils" | |
| 8 | - | ) | |
| 9 | - | ||
| 10 | - | var CacheTimeout time.Duration | |
| 11 | - | ||
| 12 | - | func init() { | |
| 13 | - | cacheDuration := utils.GetEnv("STORAGE_MINIO_CACHE_DURATION", "1m") | |
| 14 | - | duration, err := time.ParseDuration(cacheDuration) | |
| 15 | - | if err != nil { | |
| 16 | - | slog.Error("Invalid STORAGE_MINIO_CACHE_DURATION value, using default 1m", "error", err) | |
| 17 | - | duration = 1 * time.Minute | |
| 18 | - | } | |
| 19 | - | ||
| 20 | - | CacheTimeout = duration | |
| 21 | - | } |
+2,
-2
| ... | ... | @@ -14,7 +14,7 @@ import ( | |
| 14 | 14 | "github.com/jmoiron/sqlx" | |
| 15 | 15 | _ "github.com/lib/pq" | |
| 16 | 16 | "github.com/picosh/pico/pkg/db" | |
| 17 | - | "github.com/picosh/utils" | |
| 17 | + | "github.com/picosh/pico/pkg/shared" | |
| 18 | 18 | ) | |
| 19 | 19 | ||
| 20 | 20 | var PAGER_SIZE = 15 |
| ... | ... | @@ -1232,7 +1232,7 @@ func (me *PsqlDB) FindFeedItemsByPostID(postID string) ([]*db.FeedItem, error) { | |
| 1232 | 1232 | } | |
| 1233 | 1233 | ||
| 1234 | 1234 | func (me *PsqlDB) InsertProject(userID, name, projectDir string) (string, error) { | |
| 1235 | - | if !utils.IsValidSubdomain(name) { | |
| 1235 | + | if !shared.IsValidSubdomain(name) { | |
| 1236 | 1236 | return "", fmt.Errorf("'%s' is not a valid project name, must match /^[a-z0-9-]+$/", name) | |
| 1237 | 1237 | } | |
| 1238 | 1238 |
+3,
-4
| ... | ... | @@ -19,7 +19,6 @@ import ( | |
| 19 | 19 | sendutils "github.com/picosh/pico/pkg/send/utils" | |
| 20 | 20 | "github.com/picosh/pico/pkg/shared" | |
| 21 | 21 | "github.com/picosh/pico/pkg/shared/storage" | |
| 22 | - | "github.com/picosh/utils" | |
| 23 | 22 | ) | |
| 24 | 23 | ||
| 25 | 24 | var Space = "imgs" |
| ... | ... | @@ -214,8 +213,8 @@ func (h *UploadImgHandler) Write(s *pssh.SSHServerConnSession, entry *sendutils. | |
| 214 | 213 | str := fmt.Sprintf( | |
| 215 | 214 | "%s (space: %.2f/%.2fGB, %.2f%%)", | |
| 216 | 215 | url, | |
| 217 | - | utils.BytesToGB(metadata.TotalFileSize+fileSize), | |
| 218 | - | utils.BytesToGB(maxSize), | |
| 216 | + | shared.BytesToGB(metadata.TotalFileSize+fileSize), | |
| 217 | + | shared.BytesToGB(maxSize), | |
| 219 | 218 | (float32(totalFileSize)/float32(maxSize))*100, | |
| 220 | 219 | ) | |
| 221 | 220 | return str, nil |
| ... | ... | @@ -262,7 +261,7 @@ func (h *UploadImgHandler) validateImg(data *PostMetaData) (bool, error) { | |
| 262 | 261 | return false, fmt.Errorf("ERROR: user (%s) has exceeded (%d bytes) max (%d bytes)", data.User.Name, data.TotalFileSize, storageMax) | |
| 263 | 262 | } | |
| 264 | 263 | ||
| 265 | - | if !utils.IsExtAllowed(data.Filename, h.Cfg.AllowedExt) { | |
| 264 | + | if !shared.IsExtAllowed(data.Filename, h.Cfg.AllowedExt) { | |
| 266 | 265 | extStr := strings.Join(h.Cfg.AllowedExt, ",") | |
| 267 | 266 | err := fmt.Errorf( | |
| 268 | 267 | "ERROR: (%s) invalid file, format must be (%s), skipping", |
+2,
-3
| ... | ... | @@ -14,7 +14,6 @@ import ( | |
| 14 | 14 | "github.com/picosh/pico/pkg/pssh" | |
| 15 | 15 | sendutils "github.com/picosh/pico/pkg/send/utils" | |
| 16 | 16 | "github.com/picosh/pico/pkg/shared" | |
| 17 | - | "github.com/picosh/utils" | |
| 18 | 17 | ) | |
| 19 | 18 | ||
| 20 | 19 | type PostMetaData struct { |
| ... | ... | @@ -116,9 +115,9 @@ func (h *ScpUploadHandler) Write(s *pssh.SSHServerConnSession, entry *sendutils. | |
| 116 | 115 | } | |
| 117 | 116 | ||
| 118 | 117 | now := time.Now() | |
| 119 | - | slug := utils.SanitizeFileExt(filename) | |
| 118 | + | slug := shared.SanitizeFileExt(filename) | |
| 120 | 119 | fileSize := binary.Size(origText) | |
| 121 | - | shasum := utils.Shasum(origText) | |
| 120 | + | shasum := shared.Shasum(origText) | |
| 122 | 121 | ||
| 123 | 122 | nextPost := db.Post{ | |
| 124 | 123 | Filename: filename, |
+4,
-2
| ... | ... | @@ -17,9 +17,11 @@ import ( | |
| 17 | 17 | "github.com/google/renameio/v2" | |
| 18 | 18 | "github.com/picosh/pico/pkg/send/utils" | |
| 19 | 19 | "github.com/picosh/pico/pkg/shared/mime" | |
| 20 | - | putils "github.com/picosh/utils" | |
| 21 | 20 | ) | |
| 22 | 21 | ||
| 22 | + | var KB = 1000 | |
| 23 | + | var MB = KB * 1000 | |
| 24 | + | ||
| 23 | 25 | // https://stackoverflow.com/a/32482941 | |
| 24 | 26 | func dirSize(path string) (int64, error) { | |
| 25 | 27 | var size int64 |
| ... | ... | @@ -121,7 +123,7 @@ func (s *StorageFS) GetObject(bucket Bucket, fpath string) (utils.ReadAndReaderA | |
| 121 | 123 | ||
| 122 | 124 | etag := "" | |
| 123 | 125 | // only generate etag if file is less than 10MB | |
| 124 | - | if info.Size() <= int64(10*putils.MB) { | |
| 126 | + | if info.Size() <= int64(10*MB) { | |
| 125 | 127 | // calculate etag | |
| 126 | 128 | h := md5.New() | |
| 127 | 129 | if _, err := io.Copy(h, dat); err != nil { |
| ... | ... | @@ -0,0 +1,37 @@ | |
| 1 | + | package shared | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "fmt" | |
| 5 | + | "io" | |
| 6 | + | "log/slog" | |
| 7 | + | "os" | |
| 8 | + | ) | |
| 9 | + | ||
| 10 | + | type CmdSessionLogger struct { | |
| 11 | + | Log *slog.Logger | |
| 12 | + | } | |
| 13 | + | ||
| 14 | + | func (c *CmdSessionLogger) Write(out []byte) (int, error) { | |
| 15 | + | c.Log.Info(string(out)) | |
| 16 | + | return 0, nil | |
| 17 | + | } | |
| 18 | + | ||
| 19 | + | func (c *CmdSessionLogger) Exit(code int) error { | |
| 20 | + | os.Exit(code) | |
| 21 | + | return fmt.Errorf("panic %d", code) | |
| 22 | + | } | |
| 23 | + | ||
| 24 | + | func (c *CmdSessionLogger) Close() error { | |
| 25 | + | return fmt.Errorf("closing") | |
| 26 | + | } | |
| 27 | + | ||
| 28 | + | func (c *CmdSessionLogger) Stderr() io.ReadWriter { | |
| 29 | + | return nil | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | type CmdSession interface { | |
| 33 | + | Write([]byte) (int, error) | |
| 34 | + | Exit(code int) error | |
| 35 | + | Close() error | |
| 36 | + | Stderr() io.ReadWriter | |
| 37 | + | } |
| ... | ... | @@ -1,17 +1,16 @@ | |
| 1 | 1 | package shared | |
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | - | "github.com/picosh/utils" | |
| 5 | 4 | "github.com/picosh/utils/pipe" | |
| 6 | 5 | ) | |
| 7 | 6 | ||
| 8 | 7 | func NewPicoPipeClient() *pipe.SSHClientInfo { | |
| 9 | 8 | return &pipe.SSHClientInfo{ | |
| 10 | - | RemoteHost: utils.GetEnv("PICO_PIPE_ENDPOINT", "pipe.pico.sh:22"), | |
| 11 | - | KeyLocation: utils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"), | |
| 12 | - | CertificateLocation: utils.GetEnv("PICO_PIPE_KEY_CERT", ""), | |
| 13 | - | KeyPassphrase: utils.GetEnv("PICO_PIPE_PASSPHRASE", ""), | |
| 14 | - | RemoteHostname: utils.GetEnv("PICO_PIPE_REMOTE_HOST", "pipe.pico.sh"), | |
| 15 | - | RemoteUser: utils.GetEnv("PICO_PIPE_USER", "pico"), | |
| 9 | + | RemoteHost: GetEnv("PICO_PIPE_ENDPOINT", "pipe.pico.sh:22"), | |
| 10 | + | KeyLocation: GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"), | |
| 11 | + | CertificateLocation: GetEnv("PICO_PIPE_KEY_CERT", ""), | |
| 12 | + | KeyPassphrase: GetEnv("PICO_PIPE_PASSPHRASE", ""), | |
| 13 | + | RemoteHostname: GetEnv("PICO_PIPE_REMOTE_HOST", "pipe.pico.sh"), | |
| 14 | + | RemoteUser: GetEnv("PICO_PIPE_USER", "pico"), | |
| 16 | 15 | } | |
| 17 | 16 | } |
| ... | ... | @@ -16,6 +16,7 @@ import ( | |
| 16 | 16 | "time" | |
| 17 | 17 | ||
| 18 | 18 | "github.com/picosh/pico/pkg/db" | |
| 19 | + | "github.com/picosh/pico/pkg/shared" | |
| 19 | 20 | "github.com/picosh/utils/pipe/metrics" | |
| 20 | 21 | "github.com/simplesurance/go-ip-anonymizer/ipanonymizer" | |
| 21 | 22 | "github.com/x-way/crawlerdetect" |
| ... | ... | @@ -233,7 +234,7 @@ func AnalyticsCollect(ch chan *db.AnalyticsVisits, dbpool db.DB, logger *slog.Lo | |
| 233 | 234 | drain := metrics.RegisterReconnectMetricRecorder( | |
| 234 | 235 | context.Background(), | |
| 235 | 236 | logger, | |
| 236 | - | NewPicoPipeClient(), | |
| 237 | + | shared.NewPicoPipeClient(), | |
| 237 | 238 | 100, | |
| 238 | 239 | 10*time.Millisecond, | |
| 239 | 240 | ) |
| ... | ... | @@ -9,7 +9,7 @@ import ( | |
| 9 | 9 | "strings" | |
| 10 | 10 | ||
| 11 | 11 | "github.com/picosh/pico/pkg/db" | |
| 12 | - | "github.com/picosh/utils" | |
| 12 | + | "github.com/picosh/pico/pkg/shared" | |
| 13 | 13 | "golang.org/x/crypto/ssh" | |
| 14 | 14 | ) | |
| 15 | 15 |
| ... | ... | @@ -61,7 +61,7 @@ type UserApi struct { | |
| 61 | 61 | func NewUserApi(user *db.User, pubkey ssh.PublicKey) *UserApi { | |
| 62 | 62 | return &UserApi{ | |
| 63 | 63 | User: user, | |
| 64 | - | Fingerprint: utils.KeyForSha256(pubkey), | |
| 64 | + | Fingerprint: shared.KeyForSha256(pubkey), | |
| 65 | 65 | } | |
| 66 | 66 | } | |
| 67 | 67 |
| ... | ... | @@ -136,7 +136,7 @@ var FuncMap = template.FuncMap{ | |
| 136 | 136 | "intRange": intRange, | |
| 137 | 137 | } | |
| 138 | 138 | ||
| 139 | - | func RenderTemplate(cfg *ConfigSite, templates []string) (*template.Template, error) { | |
| 139 | + | func RenderTemplate(cfg *shared.ConfigSite, templates []string) (*template.Template, error) { | |
| 140 | 140 | files := make([]string, len(templates)) | |
| 141 | 141 | copy(files, templates) | |
| 142 | 142 | files = append( |
| ... | ... | @@ -165,7 +165,7 @@ func CreatePageHandler(fname string) http.HandlerFunc { | |
| 165 | 165 | return | |
| 166 | 166 | } | |
| 167 | 167 | ||
| 168 | - | data := PageData{ | |
| 168 | + | data := shared.PageData{ | |
| 169 | 169 | Site: *cfg.GetSiteData(), | |
| 170 | 170 | } | |
| 171 | 171 | err = ts.Execute(w, data) |
| ... | ... | @@ -11,9 +11,9 @@ import ( | |
| 11 | 11 | "strings" | |
| 12 | 12 | ||
| 13 | 13 | "github.com/hashicorp/golang-lru/v2/expirable" | |
| 14 | - | "github.com/picosh/pico/pkg/cache" | |
| 15 | 14 | "github.com/picosh/pico/pkg/db" | |
| 16 | 15 | "github.com/picosh/pico/pkg/pssh" | |
| 16 | + | "github.com/picosh/pico/pkg/shared" | |
| 17 | 17 | "github.com/picosh/pico/pkg/shared/storage" | |
| 18 | 18 | ) | |
| 19 | 19 |
| ... | ... | @@ -71,7 +71,7 @@ func CreatePProfRoutesMux(mux *http.ServeMux) { | |
| 71 | 71 | } | |
| 72 | 72 | ||
| 73 | 73 | type ApiConfig struct { | |
| 74 | - | Cfg *ConfigSite | |
| 74 | + | Cfg *shared.ConfigSite | |
| 75 | 75 | Dbpool db.DB | |
| 76 | 76 | Storage storage.StorageServe | |
| 77 | 77 | } |
| ... | ... | @@ -147,7 +147,7 @@ func GetSubdomainFromRequest(r *http.Request, domain, space string) string { | |
| 147 | 147 | return "" | |
| 148 | 148 | } | |
| 149 | 149 | ||
| 150 | - | func findRouteConfig(r *http.Request, routes []Route, subdomainRoutes []Route, cfg *ConfigSite) ([]Route, string) { | |
| 150 | + | func findRouteConfig(r *http.Request, routes []Route, subdomainRoutes []Route, cfg *shared.ConfigSite) ([]Route, string) { | |
| 151 | 151 | if len(subdomainRoutes) == 0 { | |
| 152 | 152 | return routes, "" | |
| 153 | 153 | } |
| ... | ... | @@ -185,8 +185,8 @@ func GetSshCtx(r *http.Request) (*pssh.SSHServerConnSession, error) { | |
| 185 | 185 | return payload, nil | |
| 186 | 186 | } | |
| 187 | 187 | ||
| 188 | - | func GetCfg(r *http.Request) *ConfigSite { | |
| 189 | - | return r.Context().Value(ctxCfg{}).(*ConfigSite) | |
| 188 | + | func GetCfg(r *http.Request) *shared.ConfigSite { | |
| 189 | + | return r.Context().Value(ctxCfg{}).(*shared.ConfigSite) | |
| 190 | 190 | } | |
| 191 | 191 | ||
| 192 | 192 | func GetLogger(r *http.Request) *slog.Logger { |
| ... | ... | @@ -213,7 +213,7 @@ func GetSubdomain(r *http.Request) string { | |
| 213 | 213 | return r.Context().Value(CtxSubdomainKey{}).(string) | |
| 214 | 214 | } | |
| 215 | 215 | ||
| 216 | - | var txtCache = expirable.NewLRU[string, string](2048, nil, cache.CacheTimeout) | |
| 216 | + | var txtCache = expirable.NewLRU[string, string](2048, nil, shared.CacheTimeout) | |
| 217 | 217 | ||
| 218 | 218 | func GetCustomDomain(host string, space string) string { | |
| 219 | 219 | txt := fmt.Sprintf("_%s.%s", space, host) |
| ... | ... | @@ -7,7 +7,6 @@ import ( | |
| 7 | 7 | "time" | |
| 8 | 8 | ||
| 9 | 9 | "github.com/picosh/pico/pkg/db" | |
| 10 | - | "github.com/picosh/utils" | |
| 11 | 10 | "golang.org/x/crypto/ssh" | |
| 12 | 11 | ) | |
| 13 | 12 |
| ... | ... | @@ -41,7 +40,7 @@ type AuthedPubkey struct { | |
| 41 | 40 | } | |
| 42 | 41 | ||
| 43 | 42 | func PubkeyCertVerify(key ssh.PublicKey, srcPrincipal string) (*AuthedPubkey, error) { | |
| 44 | - | origPubkey := utils.KeyForKeyText(key) | |
| 43 | + | origPubkey := KeyForKeyText(key) | |
| 45 | 44 | authed := &AuthedPubkey{ | |
| 46 | 45 | OrigPubkey: origPubkey, | |
| 47 | 46 | Pubkey: origPubkey, |
| ... | ... | @@ -74,7 +73,7 @@ func PubkeyCertVerify(key ssh.PublicKey, srcPrincipal string) (*AuthedPubkey, er | |
| 74 | 73 | return nil, fmt.Errorf("ssh-cert has expired") | |
| 75 | 74 | } | |
| 76 | 75 | ||
| 77 | - | authed.Pubkey = utils.KeyForKeyText(cert.SignatureKey) | |
| 76 | + | authed.Pubkey = KeyForKeyText(cert.SignatureKey) | |
| 78 | 77 | authed.Identity = cert.KeyId | |
| 79 | 78 | return authed, nil | |
| 80 | 79 | } |
| ... | ... | @@ -4,18 +4,18 @@ import ( | |
| 4 | 4 | "fmt" | |
| 5 | 5 | "log/slog" | |
| 6 | 6 | ||
| 7 | - | "github.com/picosh/utils" | |
| 7 | + | "github.com/picosh/pico/pkg/shared" | |
| 8 | 8 | ) | |
| 9 | 9 | ||
| 10 | 10 | func GetStorageTypeFromEnv() string { | |
| 11 | - | return utils.GetEnv("STORAGE_ADAPTER", "fs") | |
| 11 | + | return shared.GetEnv("STORAGE_ADAPTER", "fs") | |
| 12 | 12 | } | |
| 13 | 13 | ||
| 14 | 14 | func NewStorage(logger *slog.Logger, adapter string) (StorageServe, error) { | |
| 15 | 15 | logger.Info("storage adapter", "adapter", adapter) | |
| 16 | 16 | switch adapter { | |
| 17 | 17 | case "fs": | |
| 18 | - | fsPath := utils.GetEnv("FS_STORAGE_DIR", "/tmp/pico_storage") | |
| 18 | + | fsPath := shared.GetEnv("FS_STORAGE_DIR", "/tmp/pico_storage") | |
| 19 | 19 | logger.Info("using filesystem storage", "path", fsPath) | |
| 20 | 20 | return NewStorageFS(logger, fsPath) | |
| 21 | 21 | case "memory": |
+4,
-4
| ... | ... | @@ -11,7 +11,7 @@ import ( | |
| 11 | 11 | "git.sr.ht/~rockorager/vaxis/vxfw/richtext" | |
| 12 | 12 | "git.sr.ht/~rockorager/vaxis/vxfw/text" | |
| 13 | 13 | "github.com/picosh/pico/pkg/db" | |
| 14 | - | "github.com/picosh/utils" | |
| 14 | + | "github.com/picosh/pico/pkg/shared" | |
| 15 | 15 | ) | |
| 16 | 16 | ||
| 17 | 17 | type SitesLoaded struct{} |
| ... | ... | @@ -355,7 +355,7 @@ func (m *AnalyticsPage) visits(ctx vxfw.DrawContext, intervals []*db.VisitInterv | |
| 355 | 355 | func (m *AnalyticsPage) fetchSites() { | |
| 356 | 356 | siteList, err := m.shared.Dbpool.FindVisitSiteList(&db.SummaryOpts{ | |
| 357 | 357 | UserID: m.shared.User.ID, | |
| 358 | - | Origin: utils.StartOfMonth(), | |
| 358 | + | Origin: shared.StartOfMonth(), | |
| 359 | 359 | }) | |
| 360 | 360 | if err != nil { | |
| 361 | 361 | m.loadingSites = false |
| ... | ... | @@ -376,9 +376,9 @@ func (m *AnalyticsPage) fetchSiteStats(site string, interval string) { | |
| 376 | 376 | } | |
| 377 | 377 | ||
| 378 | 378 | if interval == "day" { | |
| 379 | - | opts.Origin = utils.StartOfMonth() | |
| 379 | + | opts.Origin = shared.StartOfMonth() | |
| 380 | 380 | } else { | |
| 381 | - | opts.Origin = utils.StartOfYear() | |
| 381 | + | opts.Origin = shared.StartOfYear() | |
| 382 | 382 | } | |
| 383 | 383 | ||
| 384 | 384 | summary, err := m.shared.Dbpool.VisitSummary(opts) |
+10,
-11
| ... | ... | @@ -14,7 +14,6 @@ import ( | |
| 14 | 14 | "git.sr.ht/~rockorager/vaxis/vxfw/richtext" | |
| 15 | 15 | "git.sr.ht/~rockorager/vaxis/vxfw/text" | |
| 16 | 16 | "github.com/picosh/pico/pkg/shared" | |
| 17 | - | "github.com/picosh/utils" | |
| 18 | 17 | pipeLogger "github.com/picosh/utils/pipe/log" | |
| 19 | 18 | ) | |
| 20 | 19 |
| ... | ... | @@ -215,10 +214,10 @@ func (m *LogsPage) connectToLogs() error { | |
| 215 | 214 | continue | |
| 216 | 215 | } | |
| 217 | 216 | ||
| 218 | - | user := utils.AnyToStr(parsedData, "user") | |
| 219 | - | userId := utils.AnyToStr(parsedData, "userId") | |
| 217 | + | user := shared.AnyToStr(parsedData, "user") | |
| 218 | + | userId := shared.AnyToStr(parsedData, "userId") | |
| 220 | 219 | ||
| 221 | - | hidden := utils.AnyToBool(parsedData, "hidden") | |
| 220 | + | hidden := shared.AnyToBool(parsedData, "hidden") | |
| 222 | 221 | ||
| 223 | 222 | if !hidden && (user == m.shared.User.Name || userId == m.shared.User.ID) { | |
| 224 | 223 | m.shared.App.PostEvent(LogLineLoaded{parsedData}) |
| ... | ... | @@ -250,13 +249,13 @@ type LogLine struct { | |
| 250 | 249 | } | |
| 251 | 250 | ||
| 252 | 251 | func NewLogLine(data map[string]any) *LogLine { | |
| 253 | - | rawtime := utils.AnyToStr(data, "time") | |
| 254 | - | service := utils.AnyToStr(data, "service") | |
| 255 | - | level := utils.AnyToStr(data, "level") | |
| 256 | - | msg := utils.AnyToStr(data, "msg") | |
| 257 | - | errMsg := utils.AnyToStr(data, "err") | |
| 258 | - | status := utils.AnyToFloat(data, "status") | |
| 259 | - | url := utils.AnyToStr(data, "url") | |
| 252 | + | rawtime := shared.AnyToStr(data, "time") | |
| 253 | + | service := shared.AnyToStr(data, "service") | |
| 254 | + | level := shared.AnyToStr(data, "level") | |
| 255 | + | msg := shared.AnyToStr(data, "msg") | |
| 256 | + | errMsg := shared.AnyToStr(data, "err") | |
| 257 | + | status := shared.AnyToFloat(data, "status") | |
| 258 | + | url := shared.AnyToStr(data, "url") | |
| 260 | 259 | date, err := time.Parse(time.RFC3339Nano, rawtime) | |
| 261 | 260 | dateStr := rawtime | |
| 262 | 261 | if err == nil { |
+2,
-2
| ... | ... | @@ -11,7 +11,7 @@ import ( | |
| 11 | 11 | "git.sr.ht/~rockorager/vaxis/vxfw/richtext" | |
| 12 | 12 | "git.sr.ht/~rockorager/vaxis/vxfw/text" | |
| 13 | 13 | "github.com/picosh/pico/pkg/db" | |
| 14 | - | "github.com/picosh/utils" | |
| 14 | + | "github.com/picosh/pico/pkg/shared" | |
| 15 | 15 | "golang.org/x/crypto/ssh" | |
| 16 | 16 | ) | |
| 17 | 17 |
| ... | ... | @@ -253,7 +253,7 @@ func (m *AddKeyPage) addPubkey(pubkey string) error { | |
| 253 | 253 | return err | |
| 254 | 254 | } | |
| 255 | 255 | ||
| 256 | - | key := utils.KeyForKeyText(pk) | |
| 256 | + | key := shared.KeyForKeyText(pk) | |
| 257 | 257 | ||
| 258 | 258 | return m.shared.Dbpool.InsertPublicKey( | |
| 259 | 259 | m.shared.User.ID, key, comment, |
+2,
-2
| ... | ... | @@ -10,7 +10,7 @@ import ( | |
| 10 | 10 | "git.sr.ht/~rockorager/vaxis/vxfw/text" | |
| 11 | 11 | "github.com/picosh/pico/pkg/db" | |
| 12 | 12 | "github.com/picosh/pico/pkg/pssh" | |
| 13 | - | "github.com/picosh/utils" | |
| 13 | + | "github.com/picosh/pico/pkg/shared" | |
| 14 | 14 | "golang.org/x/crypto/ssh" | |
| 15 | 15 | ) | |
| 16 | 16 |
| ... | ... | @@ -44,7 +44,7 @@ func (m *SignupPage) createAccount(name string) (*db.User, error) { | |
| 44 | 44 | if name == "" { | |
| 45 | 45 | return nil, fmt.Errorf("name cannot be empty") | |
| 46 | 46 | } | |
| 47 | - | key := utils.KeyForKeyText(m.shared.Session.PublicKey()) | |
| 47 | + | key := shared.KeyForKeyText(m.shared.Session.PublicKey()) | |
| 48 | 48 | return m.shared.Dbpool.RegisterUser(name, key, "") | |
| 49 | 49 | } | |
| 50 | 50 |
+1,
-2
| ... | ... | @@ -14,7 +14,6 @@ import ( | |
| 14 | 14 | "github.com/picosh/pico/pkg/db" | |
| 15 | 15 | "github.com/picosh/pico/pkg/pssh" | |
| 16 | 16 | "github.com/picosh/pico/pkg/shared" | |
| 17 | - | "github.com/picosh/utils" | |
| 18 | 17 | ) | |
| 19 | 18 | ||
| 20 | 19 | const ( |
| ... | ... | @@ -289,7 +288,7 @@ func FindUser(shrd *SharedModel) (*db.User, error) { | |
| 289 | 288 | return nil, fmt.Errorf("unable to find public key") | |
| 290 | 289 | } | |
| 291 | 290 | ||
| 292 | - | key := utils.KeyForKeyText(shrd.Session.PublicKey()) | |
| 291 | + | key := shared.KeyForKeyText(shrd.Session.PublicKey()) | |
| 293 | 292 | ||
| 294 | 293 | user, err := shrd.Dbpool.FindUserByKey(usr, key) | |
| 295 | 294 | if err != nil { |