jobs-monorepo/services/api/cmd/server/main.go
Elshimy Ziad Magdy Taha 2ab810be72
All checks were successful
Deploy scraper-google / build (push) Successful in 21m58s
Deploy scraper-google / deploy (push) Has been skipped
Move shared Go packages from libs/ to internal/
internal/ and libs/ were both shared-code roots with no rule for which
one a package belonged in, so the split carried no information. Adopt a
rule the language answers by itself:

  shared Go          -> internal/
  shared TypeScript  -> libs/   (npm workspace packages)

libs/{ports,repo,server} move to internal/, leaving libs/ holding only
the two TypeScript packages (@jobs-scraper/rabbitmq-ts and
@jobs-scraper/browser-automation), which matches npm workspace
convention. internal/ is also Go's marker for code not importable from
outside the repo, which is accurate here since none of it is published.

Import paths are rewritten mechanically (jobs-scraper/libs/ ->
jobs-scraper/internal/) across 15 lines in 10 files. The 6 moved files
are pure renames with no content change. Doing this after the module
collapse in the previous commit meant no go.mod or replace-directive
edits were needed.

gofmt is applied to services/api/pkg/http/job.go, whose import group the
rewrite left out of order. Three files were already unformatted before
this refactor (internal/openai/interface.go, internal/ports/job-queries.go,
internal/repo/job-analysis-result.go) and are deliberately left alone to
keep this diff limited to the move.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-04 17:30:04 +05:00

105 lines
2.5 KiB
Go

package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/jobs-scraper/internal/infrastructure"
"github.com/jobs-scraper/internal/infrastructure/rabbitmq"
"github.com/jobs-scraper/internal/repo"
"github.com/jobs-scraper/services/api/internal/app"
httpHandler "github.com/jobs-scraper/services/api/pkg/http"
_ "github.com/jobs-scraper/services/api/pkg/swagger"
"github.com/joho/godotenv"
httpSwagger "github.com/swaggo/http-swagger"
)
// @title Jobs Scraper API
// @version 1.0
// @description API for managing job scraping operations
// @BasePath /
func main() {
// Try to load .local.env first, then fallback to .env
if err := godotenv.Load("../../.local.env"); err != nil {
log.Println("No .local.env file found, trying .env")
if err := godotenv.Load("../../.env"); err != nil {
log.Println("No .env file found, using system environment variables")
}
}
dbConfig := infrastructure.LoadConfigFromEnv()
db, err := infrastructure.NewConnection(dbConfig)
if err != nil {
log.Fatal("Error connecting to db")
}
err = db.Ping()
if err != nil {
log.Fatal("Error pinging db")
}
log.Println("Successfully connected to db")
rmq, err := rabbitmq.NewRabbitMQClient()
if err != nil {
log.Fatal("Failed to create RabbitMQ client")
}
defer rmq.Close()
// Run database migrations
if err := infrastructure.RunMigrations(db); err != nil {
log.Fatalf("Failed to run migrations: %v", err)
}
log.Println("Successfully ran migrations")
router := mux.NewRouter()
router.Use(httpHandler.CORSMiddleware)
router.Use(httpHandler.LogsMiddleware)
// Swagger endpoint
router.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)
app := app.NewApplication(router, db, rmq)
// Create job analysis result repository
jobAnalysisResultRepo := repo.NewJobAnalysisResultRepository(db)
jobHandler := httpHandler.NewJobHandler(app.JobCommands, jobAnalysisResultRepo, app.JobQueries)
jobHandler.RegisterRoutes(router)
go func() {
log.Printf("Server running on http %s\n", "8080")
if err := app.Server.Start(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on http %s: %v\n", "8080", err)
}
}()
stopChan := make(chan os.Signal, 1)
signal.Notify(
stopChan,
os.Interrupt,
syscall.SIGTERM,
)
<-stopChan
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
app.Server.Stop(ctx)
log.Println("Server shutting down")
os.Exit(0)
}