Skip to content

Commit

Permalink
golang lint fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
MothScientist committed Jan 5, 2025
1 parent 5718b57 commit d62d1e0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 79 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/go_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ jobs:
with:
go-version: '1.22.5'

- name: Change directory to reCaptchaAPI
run: cd reCaptchaAPI # Change the directory for working with the module

- name: Initialize Go module
run: go mod init budget_graph_recaptcha_api

- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
uses: golangci/golangci-lint-action@v2

- name: Lint with golangci-lint
working-directory: ./recaptcha-microservice
run: |
golangci-lint run reCaptchaAPI/... --exclude-use-default # Run the linter for all files in the reCaptchaAPI dir
golangci-lint run ./... --exclude-use-default
37 changes: 0 additions & 37 deletions reCaptchaAPI/go.mod

This file was deleted.

File renamed without changes.
40 changes: 40 additions & 0 deletions recaptcha-microservice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"log"
"net/http"
"os"
"path/filepath"

"github.com/joho/godotenv"
"path/to/your/project/reCaptchaAPI" // import reCaptchaAPI
)

func main() {
logDir := filepath.Join("logs", "go") // setting the path for logs

// Create directory if it does not exist
if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
log.Fatalf("[ERROR] creating log directory: %v", err)
}

logFile, err := os.OpenFile(filepath.Join(logDir, "recaptcha.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("[ERROR] open log directory: %v", err)
}
defer logFile.Close()

log.SetOutput(logFile) // redirect logs to a file

if err := godotenv.Load(); err != nil {
log.Fatalf("[ERROR] No .env file found")
} else {
log.Println("[SUCCESS] .env file found")
}

http.HandleFunc("/validate-recaptcha", reCaptchaAPI.RecaptchaHandler) // use the handler from the package
log.Println("[SUCCESS] Starting server on :8080...")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("[ERROR] Error starting server: %v", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,17 @@ import (
"log"
"net/http"
"os"
"path/filepath"

"github.com/joho/godotenv"
recaptcha "cloud.google.com/go/recaptchaenterprise/v2/apiv1"
recaptchapb "cloud.google.com/go/recaptchaenterprise/v2/apiv1/recaptchaenterprisepb"
)

func main() {
logDir := filepath.Join("logs", "go") // setting the path for logs

// Create directory if it does not exist
if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
log.Fatalf("[ERROR] creating log directory: %v", err)
}

logFile, err := os.OpenFile(filepath.Join(logDir, "recaptcha.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("[ERROR] open log directory: %v", err)
}
defer logFile.Close()

log.SetOutput(logFile) // redirect logs to a file

if err := godotenv.Load(); err != nil {
log.Fatalf("[ERROR] No .env file found")
} else {
log.Println("[SUCCESS] .env file found")
}

http.HandleFunc("/validate-recaptcha", recaptchaHandler)
log.Println("[SUCCESS] Starting server on :8080...")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("[ERROR] Error starting server: %v", err)
}
}

// recaptchaHandler processes reCAPTCHA validation request
func recaptchaHandler(w http.ResponseWriter, r *http.Request) {
// RecaptchaHandler processes reCAPTCHA validation request
func RecaptchaHandler(w http.ResponseWriter, r *http.Request) {
log.Println("[INFO] recaptchaHandler init")
if r.Method != http.MethodPost {
log.Printf("[ERROR] Unsupported request method: %v", http.StatusMethodNotAllowed)
log.Printf("[ERROR] Unsupported request method: %v", http.StatusMethodNotAllowed)
http.Error(w, "Unsupported request method", http.StatusMethodNotAllowed)
return
}
Expand All @@ -57,7 +27,7 @@ func recaptchaHandler(w http.ResponseWriter, r *http.Request) {
}

if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
log.Printf("[ERROR] open log directory: %v", err)
log.Printf("[ERROR] Invalid request payload: %v", err)
http.Error(w, "Invalid request payload", http.StatusBadRequest)
return
}
Expand Down

0 comments on commit d62d1e0

Please sign in to comment.