From c405ca27dd04de10d169a0c03e612624ec065dbc Mon Sep 17 00:00:00 2001 From: "othman.suseno" Date: Mon, 15 Dec 2025 01:42:38 +0700 Subject: [PATCH] fix installer script --- install.sh | 201 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 187 insertions(+), 14 deletions(-) diff --git a/install.sh b/install.sh index acbaa96..d10aedf 100755 --- a/install.sh +++ b/install.sh @@ -216,33 +216,206 @@ build_binaries() { echo -e "${GREEN}Building binaries...${NC}" # Check if we're in the right directory - # Use REPO_DIR if specified, otherwise try current directory, then script directory - if [[ -n "$REPO_DIR" ]] && [[ -d "$REPO_DIR/cmd" ]] && [[ -f "$REPO_DIR/cmd/atlas-api/main.go" ]]; then + # Look for go.mod and internal/ directory (indicates we're in the repo root) + BUILD_DIR="" + + # Function to check if directory is valid repo root + is_repo_root() { + local dir="$1" + [[ -f "$dir/go.mod" ]] && [[ -d "$dir/internal" ]] + } + + # Try REPO_DIR first + if [[ -n "$REPO_DIR" ]] && is_repo_root "$REPO_DIR"; then BUILD_DIR="$(cd "$REPO_DIR" && pwd)" - elif [[ -d "./cmd" ]] && [[ -f "./cmd/atlas-api/main.go" ]]; then + # Try current directory + elif is_repo_root "."; then BUILD_DIR="$(pwd)" - elif [[ -d "$SCRIPT_DIR/cmd" ]] && [[ -f "$SCRIPT_DIR/cmd/atlas-api/main.go" ]]; then + # Try script directory + elif is_repo_root "$SCRIPT_DIR"; then BUILD_DIR="$SCRIPT_DIR" - elif [[ -d "$SCRIPT_DIR/../cmd" ]] && [[ -f "$SCRIPT_DIR/../cmd/atlas-api/main.go" ]]; then + # Try parent of script directory + elif is_repo_root "$SCRIPT_DIR/.."; then BUILD_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" - else - echo -e "${RED}Error: Cannot find source code directory${NC}" + fi + + # If still not found, show error + if [[ -z "$BUILD_DIR" ]]; then + echo -e "${RED}Error: Cannot find atlas repository root${NC}" echo " Current directory: $(pwd)" echo " Script directory: $SCRIPT_DIR" - echo " Looking for: cmd/atlas-api/main.go" echo "" - echo " Please ensure you're running the installer from the atlas repository root" - echo " Or specify the repository path with: --repo-dir /path/to/atlas" + echo " Looking for directory with:" + echo " - go.mod file" + echo " - internal/ directory" + echo "" + echo " Checking current directory:" + [[ -f "./go.mod" ]] && echo " ✓ Found go.mod" || echo " ✗ No go.mod" + [[ -d "./internal" ]] && echo " ✓ Found internal/" || echo " ✗ No internal/" + echo "" + echo " Please run installer from the atlas repository root" + echo " Or specify path with: --repo-dir /path/to/atlas" exit 1 fi cd "$BUILD_DIR" echo "Building from: $(pwd)" - # Verify source files exist - if [[ ! -f "$BUILD_DIR/cmd/atlas-api/main.go" ]]; then - echo -e "${RED}Error: Cannot find cmd/atlas-api/main.go${NC}" - echo " Expected at: $BUILD_DIR/cmd/atlas-api/main.go" + # Create cmd directory structure if it doesn't exist + if [[ ! -f "./cmd/atlas-api/main.go" ]]; then + echo -e "${YELLOW}cmd/ directory not found, creating...${NC}" + mkdir -p ./cmd/atlas-api + mkdir -p ./cmd/atlas-tui + + # Create atlas-api/main.go + cat > ./cmd/atlas-api/main.go <<'MAINGO' +package main + +import ( + "context" + "crypto/tls" + "log" + "net/http" + "os" + "os/signal" + "time" + + "gitea.avt.data-center.id/othman.suseno/atlas/internal/httpapp" + tlscfg "gitea.avt.data-center.id/othman.suseno/atlas/internal/tls" +) + +func main() { + addr := env("ATLAS_HTTP_ADDR", ":8080") + dbPath := env("ATLAS_DB_PATH", "data/atlas.db") + + app, err := httpapp.New(httpapp.Config{ + Addr: addr, + TemplatesDir: "web/templates", + StaticDir: "web/static", + DatabasePath: dbPath, + }) + if err != nil { + log.Fatalf("init app: %v", err) + } + + tlsConfig := tlscfg.LoadConfig() + if err := tlsConfig.Validate(); err != nil { + log.Fatalf("TLS configuration error: %v", err) + } + + var tlsServerConfig interface{} + if tlsConfig.Enabled { + var err error + tlsServerConfig, err = tlsConfig.BuildTLSConfig() + if err != nil { + log.Fatalf("TLS configuration error: %v", err) + } + log.Printf("[atlas-api] TLS enabled (cert: %s, key: %s)", tlsConfig.CertFile, tlsConfig.KeyFile) + } + + srv := &http.Server{ + Addr: addr, + Handler: app.Router(), + ReadHeaderTimeout: 5 * time.Second, + WriteTimeout: 30 * time.Second, + IdleTimeout: 120 * time.Second, + } + + if tlsServerConfig != nil { + if cfg, ok := tlsServerConfig.(*tls.Config); ok { + srv.TLSConfig = cfg + } + } + + go func() { + log.Printf("[atlas-api] listening on %s", addr) + var err error + if tlsConfig.Enabled { + err = srv.ListenAndServeTLS("", "") + } else { + err = srv.ListenAndServe() + } + if err != nil && err != http.ErrServerClosed { + log.Fatalf("listen: %v", err) + } + }() + + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt, os.Kill) + <-stop + log.Printf("[atlas-api] shutdown requested") + + app.StopScheduler() + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + if err := srv.Shutdown(ctx); err != nil { + log.Printf("[atlas-api] shutdown error: %v", err) + } else { + log.Printf("[atlas-api] shutdown complete") + } +} + +func env(key, def string) string { + if v := os.Getenv(key); v != "" { + return v + } + return def +} +MAINGO + + # Create atlas-tui/main.go + cat > ./cmd/atlas-tui/main.go <<'MAINGO' +package main + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + "gitea.avt.data-center.id/othman.suseno/atlas/internal/tui" +) + +const ( + defaultAPIURL = "http://localhost:8080" +) + +func main() { + apiURL := os.Getenv("ATLAS_API_URL") + if apiURL == "" { + apiURL = defaultAPIURL + } + + client := tui.NewAPIClient(apiURL) + app := tui.NewApp(client) + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) + go func() { + <-sigChan + app.Cleanup() + os.Exit(0) + }() + + if err := app.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } +} +MAINGO + + echo -e "${GREEN}Created cmd/ directory structure${NC}" + fi + + # Verify cmd files exist now + if [[ ! -f "./cmd/atlas-api/main.go" ]]; then + echo -e "${RED}Error: Failed to create cmd/atlas-api/main.go${NC}" + exit 1 + fi + if [[ ! -f "./cmd/atlas-tui/main.go" ]]; then + echo -e "${RED}Error: Failed to create cmd/atlas-tui/main.go${NC}" exit 1 fi