상세 컨텐츠

본문 제목

Golang - Echo BoilerPlate (해외 github repository 참고)

Web/Echo

by Yongari 2023. 4. 12. 17:22

본문

 

Overview

Echo 프레임워크에 기반한 API의 뼈대를 만든 프로젝트입니다. API 작업 시 자주 접할 수 있는 기본 기능에 대한 개발 시간을 단축하는 것이 목표입니다. 아래에 설명된 유용한 도구 세트가 있습니다. 자유롭게 기여해 주세요!

git 주소

https://github.com/nixsolutions/golang-echo-boilerplate.git

What's inside: 내부구조

  • Registration - 등록 
  • Authentication with JWT - JWT로 인증 
  • CRUD API for posts -  post용 CRUD 
  • Migrations - 마이그레이션 
  • Request validation - 요청에 대한 유효성 검사 
  • Swagger docs - Swagger 문서 
  • Environment configuration - 환경 구성  
  • Docker development environment - Docker 개발 환경

 

Usage - 사용법

1. .env.dist를 .env에 복사하고 환경 변수를 설정합니다. 린터에서 사용하는 COMPOSE_USER_ID, COMPOSE_GROUP_ID를 제외한 모든 환경 변수에 대한 예제가 있습니다. 현재 사용자 ID를 가져오려면 터미널에서 실행합니다:

echo $UID

.env 파일에서 이러한 변수를 설정합니다:

COMPOSE_USER_ID="username in current system" - your username in system

COMPOSE_GROUP_ID="user uid" - the user ID which you got in the terminal

2. 터미널에서 명령어를 사용해서 앱을 실행합니다.

docker-compose up

3. 호스트}:{포트}/swagger/index.html로 이동합니다. Swagger 2.0 API 문서가 표시됩니다.

4. API 설명서를 사용하여 사용자 등록(필요한 경우) 및 로그인 요청을 합니다.
5. 로그인에 성공하면 응답에서 토큰을 복사한 다음 "인증"을 클릭하고 팝업이 열리면 양식에 "apiKey"의 값을 입력합니다: "무기명 {토큰}" 형식으로 입력합니다. 예를 들어 다음과 같습니다. 

Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODk0NDA5NjYsIm9yaWdfaWF0IjoxNTg5NDM5OTY2LCJ1c2VyX2lkIjo1fQ.f8dSG3NxFLHwyA5-XIYALT5GtXm4eiH-motqtqAUBOI

그런 다음 '인증'을 클릭하고 팝업을 닫습니다. 이제 인증이 필요한 요청을 할 수 있습니다. 

 

Directories

 

  1. /cmd entry points. - 진입점 
  2. /config has structures which contains service config. - 서비스 구조에 대한 내용이 있습니다. 
  3. /db has seeders and method for connecting to the database. - DB에 연결하기위한 메소드와 seeder가 있습니다. 
  4. /deploy contains the container (Docker) package configuration and template(docker-compose) for project deployment. - 프로젝트 배포를 위한 컨테이너(Docker) 패키지 구성과 템플릿(docker-compose)이 포함되어 있습니다. 
  5. /development includes Docker and docker-compose files for setup linter. - 린터용 Docker 및 docker-compose 파일이 포함되어 있습니다. 
  6. /migrations has files for run migrations. - 마이그레이션 실행을 위한 파일들이 있습니다. 
  7. /models includes structures describing data models. - 데이터 모델을 설명하는 구조가 포함됩니다.
  8. /repositories contains methods for selecting entities from the database.  - 데이터베이스에서 엔티티를 선택하는 메서드가 포함되어 있습니다.
  9. /requests has structures describing the parameters of incoming requests, and validator.  - 들어오는 요청의 매개변수와 유효성 검사기를 설명하는 구조가 있습니다.
  10. /responses includes structures describing the parameters of outgoing response. -  발신 응답의 매개변수를 설명하는 구조가 포함되어 있습니다.
  11. /server is the main project folder. This folder contains the executable server.go. - 메인 프로젝트 폴더로 이동합니다. 이 폴더에는 실행 파일 server.go가 들어 있습니다. 
  12. /server/builders contains builders for initializing entities. - 엔티티 초기화를 위한 빌더가 포함되어 있습니다.
  13. /server/handlers contains request handlers. - 요청 핸들러를 포함합니다.
  14. /server/routes has a file for configuring routes. -  경로를 구성하기 위한 파일이 있습니다.
  15. /services contains methods for creating entities. -  엔티티를 생성하는 메서드가 포함되어 있습니다.
  16. /tests includes tests and test data. - 테스트 및 테스트 데이터가 포함됩니다.

 

 

Code quality

코드 품질을 제어하기 위해 golangci-lint를 사용하고 있습니다. 골랑치-린트는 린터 애그리게이터입니다.

왜 린터를 사용하나요? 린터가 도움이 됩니다:

 

린터를 사용하는 이유는 무엇인가요? 린터는 우리를 도와줍니다:

1. 중요한 버그 찾기
2. 출시 전에 버그 찾기
3. 성능 오류 찾기
4. 검토자가 구문 오류를 검색하고 일반적으로 허용되는 코드 스타일 위반을 검색하는 데 시간을 소비하지 않으므로 코드 검토 속도를 높일 수 있습니다.
5. 코드의 품질이 상당히 높은 수준으로 보장됩니다.

 

How to use

린터 도구는 도커 컴포넌트에 래핑되어 있으며, 우선 린터로 컨테이너를 빌드해야 합니다.

- make lint-build



다음으로 린터를 실행하여 버그 앤트 오류를 확인해야 합니다.

- make lint-check - 린터가 발견 한 버그와 오류를 콘솔에 기록합니다.



마지막으로, 모든 문제를 수동으로 또는 자동 수정 기능을 사용하여 수정해야 합니다(린터에서 지원하는 경우).

- make lint-fix

 

 

 

Libraries

Migrations - https://github.com/ShkrutDenis/go-migrations

Jwt - https://github.com/dgrijalva/jwt-go

Swagger - https://github.com/swaggo/echo-swagger

Mocking db - https://github.com/selvatico/go-mocket

Orm - https://github.com/jinzhu/gorm

License

The project is developed by NIX Solutions Go team and distributed under MIT LICENSE

 

 

출처 : https://github.com/nixsolutions/golang-echo-boilerplate

 

GitHub - nixsolutions/golang-echo-boilerplate: Golang API Starter Kit based by Echo

Golang API Starter Kit based by Echo. Contribute to nixsolutions/golang-echo-boilerplate development by creating an account on GitHub.

github.com

 

'Web > Echo' 카테고리의 다른 글

Golang - echo Binding Request Data  (0) 2023.04.11

관련글 더보기