찾다
백엔드 개발GolangGo를 사용하여 OTP 기반 인증 서버 구축: 부품 파일 업로드 및 정상 종료

Build an OTP-Based Authentication Server with Go: Part  File Uploads and Graceful Shutdown

이번 기사에서는 파일 업로드 기능을 추가하고, Makefile을 사용하여 개발을 간소화하고, 서버 종료를 정상적으로 구현하여 Go 인증 서버를 개선합니다. 이렇게 하면 갑작스러운 종료를 방지하고 서버가 중지되기 전에 진행 중인 모든 작업이 완료되도록 할 수 있습니다.

정상적인 종료 구현

새로운 cmd/api/server.go 파일이 서버 관리를 중앙 집중화합니다. 고루틴은 종료 신호(SIGINT, SIGTERM)를 모니터링합니다. 신호를 받으면 서버를 정상적으로 종료합니다. 코멘트는 각 단계를 명확하게 해줍니다.

package main

import (
    "context"
    "errors"
    "fmt"
    "net/http"
    "os"
    "os/signal"
    "syscall"
    "time"
)

// ... (Existing application code) ...

func (app *application) serve(router http.Handler) error {
    // Server initialization with timeouts for idle, read, and write operations.
    srv := &http.Server{
        Addr:         fmt.Sprintf(":%d", app.config.port),
        Handler:      app.recoverPanic(app.authenticate(router)),
        IdleTimeout:  time.Minute,
        ReadTimeout:  10 * time.Second,
        WriteTimeout: 30 * time.Second,
    }

    // Channel to manage errors during shutdown.
    shutdownError := make(chan error)

    // Goroutine to listen for termination signals and initiate graceful shutdown.
    go func() {
        quit := make(chan os.Signal, 1)
        signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
        s := <-quit // Wait for signal
        fmt.Println("Shutting down server...")

        // Context with timeout for graceful shutdown.
        ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
        defer cancel()

        // Attempt graceful shutdown.
        shutdownError <- srv.Shutdown(ctx)
    }()

    // Start the server and report any errors.
    fmt.Printf("Starting server on port %d\n", app.config.port)
    err := srv.ListenAndServe()
    if !errors.Is(err, http.ErrServerClosed) {
        return fmt.Errorf("server error: %w", err)
    }
    return <-shutdownError
}

// ... (Rest of application code) ...

main.go에서 srv.ListenAndServe() 블록을 err = app.serve(router); if err != nil { logger.Fatal(err, nil) }으로 바꿉니다. 이를 테스트하려면 재시도 메커니즘(연속 재시도를 위해 자격 증명 제거)을 사용하고 Ctrl C를 사용하여 서버를 중단하는 것이 포함됩니다. 종료하기 전에 재시도가 완료될 때까지 기다려야 합니다.

Makefile로 작업 자동화

AMakefile는 반복적인 명령을 자동화합니다. 프로젝트의 Makefile:

에 다음을 추가하세요.
# Include variables from the .envrc file
include .envrc

## help: print this help message
.PHONY: help
help:
    @echo 'Usage:'
    @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'

.PHONY: confirm
confirm:
    @echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]

## run/api: run the cmd/api application
.PHONY: run/api
run/api:
    go run ./cmd/api

## db/psql: connect to the database using psql
.PHONY: db/psql
db/psql:
    psql ${GREENLIGHT_DB_DSN}

## db/migrations/new name=: create a new database migration
.PHONY: db/migrations/new
db/migrations/new:
    @echo "Migrating ${name}"
    migrate create -seq -ext=.sql -dir=./migrations ${name}

## db/migrations/up: apply all up database migrations
.PHONY: db/migrations/up
db/migrations/up: confirm
    @echo "Running migrations"
    migrate -path=./migrations -database=${GREENLIGHT_DB_DSN} up

## audit: tidy and vendor dependencies and format, vet and test all code
.PHONY: audit
audit: vendor
    @echo 'Formatting code...'
    go fmt ./...
    @echo 'Vetting code...'
    go vet ./...
    staticcheck ./...
    @echo 'Running tests...'
    go test -race -vet=off ./...

## vendor: tidy and vendor dependencies
.PHONY: vendor
vendor:
    @echo 'Tidying and verifying module dependencies...'
    go mod tidy
    go mod verify
    @echo 'Vendoring dependencies...'
    go mod vendor

## build/api: build the cmd/api application
.PHONY: build/api
build/api:
    @echo 'Building cmd/api...'
    go build -o=./bin/api ./cmd/api
    GOOS=linux GOARCH=amd64 go build -o=./bin/linux_amd64/api ./cmd/api

make help, make run/api, make db/migrations/new name=my_migration, make db/migrations/up, make audit 또는 make build/api과 같은 명령을 실행합니다. 이 구조에 따라 추가 명령을 추가할 수 있습니다.

파일 업로드 및 추적

새 데이터베이스 테이블은 업로드된 파일을 추적합니다.

마이그레이션 생성 make db/migrations/new name=create-creatives.

000003_create-creatives.up.sql:

CREATE TABLE IF NOT EXISTS creatives (
    id bigserial PRIMARY KEY,
    user_id bigint NOT NULL REFERENCES users ON DELETE CASCADE,
    creative_url text NOT NULL,
    scheduled_at DATE NOT NULL,
    created_at timestamp(0) with time zone NOT NULL DEFAULT NOW()
);

000003_create-creatives.down.sql:

DROP TABLE IF EXISTS creatives;

internal/data/creatives.go:

package data

import (
    "context"
    "database/sql"
    "time"

    "github.com/lib/pq"
)

// ... (Creative struct and CreativeModel struct) ...

func (c *CreativeModel) Insert(creative *Creative) error {
    query := `INSERT INTO creatives (user_id, creative_url, scheduled_at)
            VALUES (, , )
            RETURNING id, created_at`

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()

    args := []interface{}{creative.UserID, creative.CreativeURL, creative.ScheduledAt}
    return c.DB.QueryRowContext(ctx, query, args...).Scan(&creative.ID, &creative.CreatedAt)
}

func (c *CreativeModel) GetScheduledCreatives() (map[string][]Creative, error) {
    query := `
        SELECT id, user_id, creative_url, scheduled_at, created_at 
        FROM creatives 
        WHERE scheduled_at = ANY()
    `

    dates := []time.Time{
        time.Now().Truncate(24 * time.Hour),
        time.Now().AddDate(0, 0, 1).Truncate(24 * time.Hour),
    }

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()

    rows, err := c.DB.QueryContext(ctx, query, pq.Array(dates))
    if err != nil {
        return nil, err
    }
    defer rows.Close()

    creatives := map[string][]Creative{
        "today":    {},
        "tomorrow": {},
    }

    for rows.Next() {
        var creative Creative
        err := rows.Scan(&creative.ID, &creative.UserID, &creative.CreativeURL, &creative.ScheduledAt, &creative.CreatedAt)
        if err != nil {
            return nil, err
        }
        if creative.ScheduledAt.Equal(dates[0]) {
            creatives["today"] = append(creatives["today"], creative)
        } else if creative.ScheduledAt.Equal(dates[1]) {
            creatives["tomorrow"] = append(creatives["tomorrow"], creative)
        }
    }

    return creatives, rows.Err()
}

cmd/api/creatives.go: (파일 업로드 및 예약된 광고 검색 처리)

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
    "path"
    "strings"
    "time"

    "github.com/google/uuid"
    "github.com/vishaaxl/cheershare/internal/data"
)

const MaxFileSize = 10 << 20 // 10MB

// ... (Existing code) ...

func (app *application) uploadCreativeHandler(w http.ResponseWriter, r *http.Request) {
    // ... (File upload handling logic) ...
}

func (app *application) getScheduledCreativesHandler(w http.ResponseWriter, r *http.Request) {
    // ... (Retrieve scheduled creatives logic) ...
}

업로드 디렉토리가 없는 경우 main.go 또는 서버 초기화에 다음을 추가하여 업로드 디렉토리를 생성하세요.

uploadDir := "./uploads"
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
    err := os.MkdirAll(uploadDir, os.ModePerm)
    if err != nil {
        fmt.Println("Unable to create uploads directory:", err)
    }
}

router.HandlerFunc(http.MethodPost, "/upload-creative", app.requireAuthenticatedUser(app.uploadCreativeHandler))
router.HandlerFunc(http.MethodGet, "/scheduled", app.requireAuthenticatedUser(app.getScheduledCreativesHandler))

핸들러 내의 파일 처리 및 오류 관리에 대한 자리 표시자 주석을 실제 구현 세부 정보로 바꾸는 것을 잊지 마세요. 이로써 결제 처리(향후 업데이트에서 다룰 예정)를 제외한 핵심 기능이 완성되었습니다. 3부 제공 링크는 그대로 유지됩니다.

위 내용은 Go를 사용하여 OTP 기반 인증 서버 구축: 부품 파일 업로드 및 정상 종료의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
GO 애플리케이션에서 로깅 오류가 효과적입니다GO 애플리케이션에서 로깅 오류가 효과적입니다Apr 30, 2025 am 12:23 AM

효과적인 GO 애플리케이션 오류 로깅에는 밸런싱 세부 사항 및 성능이 필요합니다. 1) 표준 로그 패키지 사용은 간단하지만 컨텍스트가 부족합니다. 2) Logrus는 구조화 된 로그 및 사용자 정의 필드를 제공합니다. 3) ZAP는 성능과 구조화 된 로그를 결합하지만 더 많은 설정이 필요합니다. 완전한 오류 로깅 시스템에는 오류 강화, 로그 레벨, 중앙 집중식 로깅, 성능 고려 사항 및 오류 처리 모드가 포함되어야합니다.

Go의 빈 인터페이스 (인터페이스 {}) : 사용 사례 및 고려 사항Go의 빈 인터페이스 (인터페이스 {}) : 사용 사례 및 고려 사항Apr 30, 2025 am 12:23 AM

NOMPLINGOREAREINTERFACES의 NOMETHODS를 사용하고, value를 대표하며, handlingunknowndatatypes를 대적 할 때 houldliedlling니다.

동시성 모델 비교 : GO 대 기타 언어동시성 모델 비교 : GO 대 기타 언어Apr 30, 2025 am 12:20 AM

Go'sconcurrencymodelisuniqueduetoitsuseofgoroutinesandchannels, onuverylight wecondeficeedtotheredtotheredtotheinlanguages ​​likejava, python, andrust.1) go'sgoroutinesArimageTime, gountchernaged-thengernageTime, gendownStoruncUrentlyWithminiments

Go의 동시성 모델 : Goroutines 및 채널이 설명되었습니다Go의 동시성 모델 : Goroutines 및 채널이 설명되었습니다Apr 30, 2025 am 12:04 AM

go'sconcurrencymodelusesgoroutines 및 channelSmanageConcurrentProgrammingEfficially.1) GoroutinesArelightwheightShreadsthathalloweAparAllelizationOftasks, 향상된 성능

GO의 인터페이스 및 다형성 : 코드 재사용 성 달성GO의 인터페이스 및 다형성 : 코드 재사용 성 달성Apr 29, 2025 am 12:31 AM

InterfacesandPolymorphismingoEnhancecodereusabilitableandabledaysainability.

GO에서 'Init'기능의 역할은 무엇입니까?GO에서 'Init'기능의 역할은 무엇입니까?Apr 29, 2025 am 12:28 AM

theinitfunctionorunsautomically weconitializepackages 및 seteptheenvironment.ituplopgortingupglobalvariables, andperformingone-timesetupstasksacrossanypackage

GO의 인터페이스 구성 : 복잡한 추상화 구축GO의 인터페이스 구성 : 복잡한 추상화 구축Apr 29, 2025 am 12:24 AM

인터페이스 조합은 기능을 작고 집중된 인터페이스로 분류하여 GO 프로그래밍에서 복잡한 추상화를 구축합니다. 1) 독자, 작가 및 더 가까운 인터페이스를 정의하십시오. 2) 이러한 인터페이스를 결합하여 파일 및 네트워크 스트림과 같은 복잡한 유형을 만듭니다. 3) ProcessData 함수를 사용하여 이러한 결합 된 인터페이스를 처리하는 방법을 보여줍니다. 이 접근법은 코드 유연성, 테스트 가능성 및 재사용 성을 향상 시키지만 과도한 조각화 및 조합 복잡성을 피하기 위해주의를 기울여야합니다.

GO에서 시작 함수를 사용할 때 잠재적 인 함정 및 고려 사항GO에서 시작 함수를 사용할 때 잠재적 인 함정 및 고려 사항Apr 29, 2025 am 12:02 AM

inittectionsingoareautomaticallyCalledBeforeMainForeChalledBectOnforTeForTupButcomewithChalleds

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음