Switch backend to wait for SQL Server and use direct INSERTs

Replace stored procedure calls with inline INSERT statements and
SCOPE_IDENTITY()
Add netcat for readiness check in new entrypoint.sh
Introduce dedicated SQL Server and init-db services in docker-compose
This commit is contained in:
2026-02-22 09:36:38 -05:00
parent c780dff015
commit 2d1f55eea1
6 changed files with 120 additions and 21 deletions

View File

@@ -20,15 +20,19 @@ FROM alpine:3.19
# Install ca-certificates (if needed)
RUN apk add --no-cache ca-certificates
RUN apk add --no-cache netcat-openbsd
# Set workdir
WORKDIR /app
# Removed hosts entry addition (handled via docker-compose extra_hosts)
# Copy the binary from the builder stage
COPY --from=builder /app/main .
# Expose the application port (default for the Go server)
COPY entrypoint.sh .
RUN chmod +x entrypoint.sh
EXPOSE 8080
# Command to run the binary
CMD ["./main"]
CMD ["./entrypoint.sh"]

24
backend/entrypoint.sh Normal file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env sh
# entrypoint.sh wait for the SQL Server container to become ready
# before starting the Go backend binary.
set -e
# Default environment variables (can be overridden in docker-compose)
: "${DB_SERVER:=sql}"
: "${DB_USER:=sa}"
: "${DB_PASSWORD:=Ou812@12!@}"
# Wait until netcat can successfully connect to the SQL Server port
while ! nc -z "$DB_SERVER" 1433; do
echo "⏳ Waiting for SQL Server at $DB_SERVER:1433..."
sleep 1
done
# Add a short pause after the port is open to let SQL Server finish startup
sleep 5
# Add a short pause after the port is open to let SQL Server finish startup
sleep 2
echo "✅ SQL Server is ready. Starting backend."
exec ./main

View File

@@ -41,17 +41,9 @@ func main() {
// -----------------------------------------------------------------
var newCustID int
insertCustSQL := `
DECLARE @NewID int;
EXEC dbo.sp_InsertCustomer
@CustomerName = @name,
@AddressLine1 = @addr1,
@AddressLine2 = @addr2,
@City = @city,
@State = @state,
@ZipCode = @zip,
@Country = @country,
@NewCustomerID = @NewID OUTPUT;
SELECT @NewID;
INSERT INTO dbo.Customers (CustomerName, AddressLine1, AddressLine2, City, State, ZipCode, Country)
VALUES (@name, @addr1, @addr2, @city, @state, @zip, @country);
SELECT SCOPE_IDENTITY() AS NewID;
`
row := db.QueryRow(insertCustSQL,
sql.Named("name", "Acme Corp"),
@@ -72,14 +64,9 @@ SELECT @NewID;
// -----------------------------------------------------------------
var newContactID int
insertContactSQL := `
DECLARE @NewContactID int;
EXEC dbo.sp_InsertContact
@CustomerID = @custID,
@ContactTypeID = @typeID,
@ContactValue = @value,
@IsPrimary = @primary,
@NewContactID = @NewContactID OUTPUT;
SELECT @NewContactID;
INSERT INTO dbo.Contacts (CustomerID, ContactTypeID, ContactValue, IsPrimary)
VALUES (@custID, @typeID, @value, @primary);
SELECT SCOPE_IDENTITY() AS NewContactID;
`
// Assume ContactTypeID 1 = Phone (created by init.sql)
row = db.QueryRow(insertContactSQL,