diff --git a/config.json b/config.json new file mode 100644 index 0000000..8f0726a --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "htpasswdPath": "C:\\nginx\\.htpasswd", + "port": 8000 +} diff --git a/deployment.md b/deployment.md index 67af368..e723534 100644 --- a/deployment.md +++ b/deployment.md @@ -12,21 +12,24 @@ go build -o vaultkeeper -ldflags "-s -w" main.go 빌드가 완료되면 현재 디렉토리에 `vaultkeeper` 실행 파일이 생성됩니다. -## 2. `.htpasswd` 파일 경로 설정 +## 2. VaultKeeper 설정 파일 (`config.json`) 생성 및 설정 -VaultKeeper는 `.htpasswd` 파일의 경로를 환경 변수 `HTPASSWD_PATH`를 통해 설정합니다. -애플리케이션을 실행하기 전에 이 환경 변수를 설정해야 합니다. +VaultKeeper는 `config.json` 파일을 통해 `.htpasswd` 파일 경로와 서버 포트를 설정합니다. +애플리케이션을 실행하기 전에 이 파일을 생성하고 내용을 채워야 합니다. -예시: -```bash -export HTPASSWD_PATH="/etc/nginx/.htpasswd" -``` -또는 -```bash -export HTPASSWD_PATH="/usr/local/etc/htpasswd" +프로젝트 루트 디렉토리에 `config.json` 파일을 다음 내용으로 생성합니다. + +```json +{ + "htpasswdPath": "/etc/nginx/.htpasswd", + "port": 8000 +} ``` -**주의:** 지정된 경로에 Nginx가 실제로 사용하는 `.htpasswd` 파일이 있어야 합니다. 쓰기 권한도 필요합니다. +* `"htpasswdPath"`: Nginx가 사용하는 실제 `.htpasswd` 파일의 절대 경로를 지정합니다. VaultKeeper가 이 파일에 대한 읽기/쓰기 권한을 가지고 있어야 합니다. +* `"port"`: VaultKeeper 서버가 수신 대기할 포트 번호를 지정합니다. (기본값: 8000) + +**주의:** Windows 환경의 경우, `htpasswdPath`는 `"C:\\nginx\\.htpasswd"`와 같이 백슬래시를 두 번 사용하여 이스케이프해야 합니다. ## 3. 최초 관리자 계정 생성 (터미널) @@ -48,13 +51,13 @@ htpasswd /etc/nginx/.htpasswd anotheruser ## 4. VaultKeeper 실행 -환경 변수 설정 및 `.htpasswd` 파일 준비가 완료되면, 빌드된 VaultKeeper 바이너리를 실행합니다. +`config.json` 파일 생성 및 `.htpasswd` 파일 준비가 완료되면, 빌드된 VaultKeeper 바이너리를 실행합니다. ```bash ./vaultkeeper ``` -기본적으로 VaultKeeper는 8000번 포트에서 수신 대기합니다. +VaultKeeper는 `config.json`에 설정된 포트에서 수신 대기합니다 (기본값: 8000번 포트). ## 5. Nginx 설정 예시 diff --git a/main.go b/main.go index 808aca9..098568e 100644 --- a/main.go +++ b/main.go @@ -24,19 +24,42 @@ type AddUserRequest struct { Password string `json:"password"` } +// Config는 애플리케이션 설정을 나타냅니다. +type Config struct { + HtpasswdPath string `json:"htpasswdPath"` + Port int `json:"port"` +} + var ( - htpasswdPath string + htpasswdPath string + appPort int // 애플리케이션 포트 htpasswdMutex sync.Mutex // htpasswd 명령 실행 시 동시성 문제를 방지하기 위한 뮤텍스 usernameRegex *regexp.Regexp // 사용자 이름 유효성 검사를 위한 정규식 ) func init() { - // HTPASSWD_PATH 환경 변수 설정 - htpasswdPath = os.Getenv("HTPASSWD_PATH") - if htpasswdPath == "" { - log.Fatal("환경 변수 HTPASSWD_PATH가 설정되지 않았습니다.") + // config.json 파일에서 설정 로드 + configData, err := os.ReadFile("config.json") + if err != nil { + log.Fatalf("config.json 파일을 읽을 수 없습니다: %v", err) } + var cfg Config + if err := json.Unmarshal(configData, &cfg); err != nil { + log.Fatalf("config.json 파싱 오류: %v", err) + } + + htpasswdPath = cfg.HtpasswdPath + appPort = cfg.Port + + if htpasswdPath == "" { + log.Fatal("config.json에 'htpasswdPath'가 설정되지 않았습니다.") + } + if appPort == 0 { + appPort = 8000 // 기본 포트 + log.Printf("config.json에 'port'가 설정되지 않아 기본값 %d번 포트를 사용합니다.\n", appPort) + } + // 사용자 이름 유효성 검사 정규식 초기화: 영문, 숫자, 하이픈, 언더스코어만 허용 usernameRegex = regexp.MustCompile("^[a-zA-Z0-9_-]+$") } @@ -51,9 +74,9 @@ func main() { mux.HandleFunc("/api/users", handleUsers) mux.HandleFunc("/api/users/", handleDeleteUser) // DELETE 요청 처리 - log.Println("VaultKeeper 서버가 8000 포트에서 시작됩니다.") + log.Printf("VaultKeeper 서버가 %d 포트에서 시작됩니다.\n", appPort) log.Printf("HTPASSWD_PATH: %s\n", htpasswdPath) - log.Fatal(http.ListenAndServe(":8000", mux)) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appPort), mux)) } // serveFrontend는 index.html 파일을 제공합니다.