Configuration
The development environment is built using Docker, which offers a portable and consistent way to manage dependencies and run the application.
To modify the configuration, make changes to the following files:
Dockerfile
- This file defines the Docker image for the application. It manages dependencies, builds the application, and exposes the necessary port.go.mod
- This file specifies the Golang dependencies used in the project.app.go
- This file contains the main application logic, including the HTTP server configuration.
Dockerfile
The Dockerfile
defines the steps for building a Docker image.
Configuration Options:
FROM golang:1.21-alpine AS builder
- This line specifies the base Docker image used for building the application. You can modify this line to change the Golang version or the base image.EXPOSE 8080
- This line exposes port 8080 on the Docker container, allowing external connections to the application.CMD ["./myapp"]
- This line sets the command to run when the Docker container starts. It runs the compiledmyapp
executable.
Example:
# First stage: build the application
FROM golang:1.21-alpine AS builder
# Set the working directory
WORKDIR /app
# Copy go.mod
COPY go.mod go.sum ./
# Download dependencies and generate go.sum
RUN go mod download && go mod tidy
# Copy the rest of the application code
COPY . .
# Build the application
RUN go build -o myapp
# Second stage: create the runtime image
FROM alpine:latest
# Set the working directory
WORKDIR /root/
# Copy the built application from the builder stage
COPY --from=builder /app/myapp .
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the executable
CMD ["./myapp"]
go.mod
The go.mod
file manages the Golang dependencies for the project.
Configuration Options:
module myapp
- This line specifies the Golang module name for the project, which impacts the output ofgo build
.go 1.18
- This line specifies the required Go version for the project.require github.com/gorilla/mux v1.8.0
- This line defines the dependencies used by the project, including the version.
Example:
module myapp
go 1.18
require github.com/gorilla/mux v1.8.0
app.go
The app.go
file contains the application logic, including the HTTP server configuration.
Configuration Options:
Addr: ":8080"
- This line sets the address and port for the HTTP server to listen on.ReadTimeout: 15 * time.Second
- This line sets the read timeout for the HTTP server.WriteTimeout: 15 * time.Second
- This line sets the write timeout for the HTTP server.IdleTimeout: 60 * time.Second
- This line sets the idle timeout for the HTTP server.
Example:
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HelloWorld).Methods("GET")
server := &http.Server{
Addr: ":8080",
Handler: r,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
go func() {
fmt.Println("Server started at :8080")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on :8080: %v\n", err)
}
}()
// Wait for an interrupt signal to gracefully shutdown the server
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
server.Shutdown(ctx)
log.Println("shutting down")
os.Exit(0)
}