feat: scaffold codespace backend

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
tao.chen
2026-07-02 15:07:54 +08:00
co-authored by Claude Fable 5
commit 411ed1f8ba
43 changed files with 4428 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
package api
import (
"net/http"
"codespace/internal/util"
"codespace/pkg/response"
"github.com/gin-gonic/gin"
)
// writeError maps a service-layer error to an HTTP JSON error response.
func writeError(c *gin.Context, err error) {
code := util.CodeOf(err)
var status int
switch code {
case util.CodeBadRequest:
status = http.StatusBadRequest
case util.CodeNotFound:
status = http.StatusNotFound
case util.CodeConflict:
status = http.StatusConflict
default:
status = http.StatusInternalServerError
}
msg := http.StatusText(status)
if err != nil {
msg = err.Error()
}
c.JSON(status, response.ErrorBody{Error: response.ErrorDetail{
Code: string(code),
Message: msg,
}})
}
// writeBadRequest is a shorthand for 400 responses without a service error.
func writeBadRequest(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, response.ErrorBody{Error: response.ErrorDetail{
Code: string(util.CodeBadRequest),
Message: message,
}})
}