Api.go

package core

import (
	"encoding/json"
	"net/http"
	"os"
)

// Result νƒ€μž… μ •μ˜
type Result struct {
	Code    int                    `json:"code"`          // HTTP μƒνƒœ μ½”λ“œ
	Data    interface{}            `json:"data,omitempty"` // 선택적 데이터
	Attr    map[string]interface{} `json:"attr"`          // 기타 속성듀
}

// HTTP μƒνƒœ μ½”λ“œλ“€
const (
	OK                   = 200   // 성곡
	FAIL                 = 999   // μ‹€νŒ¨
	INTERNAL_SERVER_ERROR = 500   // λ‚΄λΆ€ μ„œλ²„ 였λ₯˜
)

// Api ꡬ쑰체 μ •μ˜
type Api struct{}

// Main λ©”μ„œλ“œλŠ” κΈ°λ³Έ λ™μž‘μ„ μˆ˜ν–‰ν•˜κ³  nil을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
func (api *Api) Main() interface{} {
	return nil
}

// Fail λ©”μ„œλ“œλŠ” HTTP 응닡에 μ‹€νŒ¨ μƒνƒœλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
// 기본적으둜 μ½”λ“œ 999(FAIL)λ₯Ό λ°˜ν™˜ν•˜λ©°, λ©”μ‹œμ§€κ°€ μ—†λŠ” 경우 "fail"을 κΈ°λ³Έ λ©”μ‹œμ§€λ‘œ μ‚¬μš©ν•©λ‹ˆλ‹€.
func (api *Api) Fail(w http.ResponseWriter, code int, msg string) {
	if code == 0 {
		code = FAIL
	}
	if msg == "" {
		msg = "fail"
	}

	result := Result{
		Code: code,
		Attr: map[string]interface{}{
			"msg": msg,
		},
	}
	api.View(w, result)
}

// View λ©”μ„œλ“œλŠ” JSON ν˜•μ‹μœΌλ‘œ HTTP 응닡을 μ²˜λ¦¬ν•©λ‹ˆλ‹€.
func (api *Api) View(w http.ResponseWriter, result Result) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(result.Code)

	jsonResponse, err := json.Marshal(result)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write(jsonResponse)
	os.Exit(0) // 일반적으둜 HTTP μ„œλ²„μ—μ„œλŠ” os.Exitλ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 이 μ˜ˆμ œμ—μ„œλŠ” κ°„λ‹¨νžˆ μ’…λ£Œλ₯Ό 보여주기 μœ„ν•΄ μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
}

ꡬ쑰체 μ •μ˜

  1. Result ꡬ쑰체

    • Code: 응닡 μƒνƒœ μ½”λ“œ(예: 200, 500).

    • Data: 응닡 데이터. λΉ„μ–΄μžˆμ„ 수 있음(omitempty).

    • Attr: μΆ”κ°€ 속성듀을 λ‹΄λŠ” λ§΅. μ—¬κΈ°μ„œλŠ” 주둜 λ©”μ‹œμ§€λ₯Ό μ €μž₯함.

  2. Api ꡬ쑰체

    • λ©”μ„œλ“œλ₯Ό 포함할 빈 ꡬ쑰체둜, API κ΄€λ ¨ λ©”μ„œλ“œλ₯Ό μ •μ˜ν•˜λŠ” 데 μ‚¬μš©λ¨.

μƒμˆ˜ μ •μ˜

  • OK, FAIL, INTERNAL_SERVER_ERROR: μƒνƒœ μ½”λ“œλ₯Ό μ˜λ―Έν•˜λŠ” μƒμˆ˜.

λ©”μ„œλ“œ μ„€λͺ…

  1. Main λ©”μ„œλ“œ

    • API의 메인 μ—”νŠΈλ¦¬ 포인트. ν˜„μž¬λŠ” nil을 λ°˜ν™˜ν•˜λ„λ‘ λ˜μ–΄ 있음.

  2. Fail λ©”μ„œλ“œ

    • μ‹€νŒ¨ν•œ μš”μ²­μ— λŒ€ν•œ 응닡을 생성.

    • μƒνƒœ μ½”λ“œ(code)와 λ©”μ‹œμ§€(msg)λ₯Ό λ°›μ•„μ„œ Result ꡬ쑰체λ₯Ό 생성.

    • View λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ 응닡을 전솑.

  3. View λ©”μ„œλ“œ

    • 응닡을 JSON ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•˜κ³  ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ 전솑.

    • μ‘λ‹΅μ˜ 헀더λ₯Ό application/json; charset=utf-8둜 μ„€μ •.

    • 응닡 μ½”λ“œ μ„€μ •(WriteHeader).

    • Result ꡬ쑰체λ₯Ό JSON으둜 λ³€ν™˜(json.Marshal).

    • λ³€ν™˜λœ JSON을 μ‘λ‹΅μœΌλ‘œ μž‘μ„±(Write).

    • ν”„λ‘œμ„ΈμŠ€λ₯Ό μ’…λ£Œ(os.Exit(0)). 이 μ½”λ“œλŠ” 주둜 μ›Ή μ„œλ²„μ—μ„œ μ‹€νŒ¨ν•œ μš”μ²­μ— λŒ€ν•œ 응닡을 μ²˜λ¦¬ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ  수 있으며, ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ JSON ν˜•μ‹μ˜ 응닡을 μ „μ†‘ν•©λ‹ˆλ‹€. os.Exit(0)둜 인해 λ©”μ„œλ“œ 호좜 ν›„ ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλœλ‹€λŠ” 점을 μœ μ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. λ”°λΌμ„œ 이 μ½”λ“œλŠ” 일반적인 μ›Ή μ„œλ²„μ—μ„œλŠ” λ°”λ‘œ μ‚¬μš©ν•˜κΈ°μ—λŠ” μ ν•©ν•˜μ§€ μ•Šμ„ 수 있으며, μˆ˜μ •μ΄ ν•„μš”ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Last updated