Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer.json → .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Go with MySQL",
// Specify the Docker Compose file instead of a single image
"dockerComposeFile": "docker-compose.yml",
"dockerComposeFile": "../docker-compose.yml",
// Define the service name of your application container defined in the Docker Compose file
"service": "app",
"workspaceFolder": "/workspace",
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ go.work.sum
# env file
.env

# IDE
.vscode/

# Output of vscode debugger (files tsarting with __debug_)
**/__debug_*
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Server",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/movie-api/main.go"
}
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"go.buildFlags": [
"-tags=unit,integration"
],
"go.testFlags": [
"-tags=unit,integration"
],
"cSpell.words": [
"dgrijalva",
"godotenv"
],
}
10 changes: 6 additions & 4 deletions ci.docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ services:
timeout: 5s
retries: 5
depends_on:
mysql:
mariadb:
condition: service_healthy

mysql:
image: mysql:lts
container_name: mysql
mariadb:
image: mariadb:10.6
container_name: mariadb
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: moviesdb
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "3306:3306"
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
Expand Down
37 changes: 35 additions & 2 deletions cmd/movie-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@ import (
_ "go-demo-api/docs"
"go-demo-api/internal/api"
"go-demo-api/internal/db"
user "go-demo-api/internal/db/user"
verification "go-demo-api/internal/db/verification"
"log"
"net/http"

"github.com/gorilla/mux"
httpSwagger "github.com/swaggo/http-swagger"
)

// corsMiddleware sets up the CORS headers
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set headers
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow any origin
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") // Allowed methods
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") // Allowed headers

// Check if the request is for the OPTIONS method (pre-flight request)
if r.Method == "OPTIONS" {
// Respond with OK status for pre-flight requests
w.WriteHeader(http.StatusOK)
return
}

// Pass down the request to the next middleware (or final handler)
next.ServeHTTP(w, r)
})
}

func main() {
// Initialize the database connection
database, err := db.NewDB()
Expand All @@ -19,18 +41,29 @@ func main() {
}

// Instantiate the repository
repo := &db.MovieRepository{DB: database}
movieRepo := &db.MovieRepository{DB: database}
verifyRepo := &verification.EmailVerificationRepository{VerificationRepository: &verification.VerificationRepository{DB: database}}
userRepo := &user.UserRepository{DB: database, VerificationRepository: verifyRepo}

// Instantiate the handler struct with the repository
movieHandler := &api.MovieHandler{Repo: repo}
movieHandler := &api.MovieHandler{Repo: movieRepo, IsTest: false}
userHandler := &api.UserHandler{Repo: userRepo}
verifyHandler := &api.VerificationHandler{VerificationRepository: verifyRepo}

r := mux.NewRouter()

r.Use(corsMiddleware) // Use the CORS middleware

// Use the methods of movieHandler as HTTP handlers
r.HandleFunc("/movies", movieHandler.GetMovies).Methods("GET")
r.HandleFunc("/movies/{id}", movieHandler.GetMovie).Methods("GET")
r.HandleFunc("/health", movieHandler.HealthCheckHandler).Methods("GET")

// Use the methods of userHandler as HTTP handlers
r.HandleFunc("/users/register", userHandler.RegisterUser).Methods("POST")
r.HandleFunc("/verify", verifyHandler.VerifyUser).Methods("GET")
r.HandleFunc("/login", userHandler.LoginUser).Methods("POST")

// Serve Swagger UI
r.PathPrefix("/swagger-ui/").Handler(httpSwagger.WrapHandler)

Expand Down
14 changes: 8 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@ services:
command: /bin/sh -c "while sleep 1000; do :; done"
ports:
- 9000:9000

mysql:
image: mysql:lts
container_name: mysql
mariadb:
image: mariadb:10.6
container_name: mariadb
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: moviesdb
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "3306:3306"

phpmyadmin:
image: phpmyadmin:5.2.1
container_name: phpmyadmin
environment:
PMA_HOST: mysql
PMA_HOST: mariadb
PMA_USER: user
PMA_PASSWORD: password
ports:
- "9001:80"
depends_on:
- mysql
- mariadb

volumes:
mysql-data:
Loading