Add Go rewrite with Postgres 17 and Dokploy Docker Compose
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
// Command cleanup runs one pass of expired-share-link cleanup and exits; meant
|
||||
// to be invoked periodically (cron, systemd timer, or the docker-compose
|
||||
// "cleanup" service loop).
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"amnezia-share/internal/config"
|
||||
"amnezia-share/internal/db"
|
||||
"amnezia-share/internal/panel"
|
||||
"amnezia-share/internal/settings"
|
||||
"amnezia-share/internal/share"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_ = godotenv.Load()
|
||||
cfg := config.Load()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
pool, err := db.Connect(ctx, cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
log.Fatalf("подключение к БД: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
if err := db.Migrate(ctx, pool, cfg.MigrationsDir); err != nil {
|
||||
log.Fatalf("миграции: %v", err)
|
||||
}
|
||||
|
||||
st := &settings.Store{
|
||||
Pool: pool,
|
||||
EnvPanelURL: cfg.PanelURL,
|
||||
EnvToken: cfg.PanelToken,
|
||||
EnvLabelsJSON: cfg.ServerLabelsJSON,
|
||||
}
|
||||
pc := panel.New(cfg.HTTPBudget())
|
||||
repo := share.New(pool)
|
||||
|
||||
runCtx, runCancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer runCancel()
|
||||
|
||||
res, err := repo.CleanupExpired(runCtx, pc, st)
|
||||
if err != nil {
|
||||
log.Fatalf("очистка не выполнена: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("очистка завершена: всего=%d (без конфигов=%d, снято с панели=%d), ошибок=%d",
|
||||
res.Count, res.DBCount, res.PanelCount, len(res.Failures))
|
||||
for _, f := range res.Failures {
|
||||
log.Printf(" - %s", f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Command server is the Amnezia VPN Share Panel HTTP application entrypoint.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"amnezia-share/internal/config"
|
||||
"amnezia-share/internal/web"
|
||||
_ "amnezia-share/internal/web/handlers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_ = godotenv.Load()
|
||||
|
||||
cfg := config.Load()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
app, err := web.New(ctx, cfg)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Fatalf("не удалось запустить приложение: %v", err)
|
||||
}
|
||||
defer app.Close()
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + cfg.Port,
|
||||
Handler: app.Handler(),
|
||||
ReadHeaderTimeout: 15 * time.Second,
|
||||
ReadTimeout: 2 * time.Minute,
|
||||
WriteTimeout: 3 * time.Minute,
|
||||
IdleTimeout: 90 * time.Second,
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Printf("amnezia-share слушает на порту %s (base url: %q)", cfg.Port, cfg.BaseURL)
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("сервер остановлен с ошибкой: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-stop
|
||||
|
||||
log.Println("получен сигнал остановки, завершаем работу...")
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer shutdownCancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Printf("ошибка при остановке сервера: %v", err)
|
||||
}
|
||||
log.Println("сервер остановлен.")
|
||||
}
|
||||
Reference in New Issue
Block a user