muyu-apiserver/gateway/gateway.go
Chever John 4144628cb6 build(deploy): replace pre-compiled arm64 binaries with multi-stage Go compilation for amd64
WHY: existing Dockerfiles copied pre-compiled arm64 binaries which cannot
run on the amd64 K3s cluster (exec format error). Also fixed port conflicts
when running muyu alongside iloom in docker-compose.

HOW:
- rewrite 3 Dockerfiles (system/inventory/gateway) to multi-stage builds
  using golang:1.24-alpine with CGO_ENABLED=0 GOARCH=amd64
- parameterize host ports in docker-compose to avoid conflicts (3306->13306,
  6379->16379, 8080->18080)
- fix inv_product unique key from product_name-only to composite
  (product_name, spec, color) to allow same-name products with different specs
- add X-Token to Authorization header middleware in gateway for
  iloom frontend compatibility

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-14 12:38:46 +08:00

66 lines
1.5 KiB
Go

// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package main
import (
"context"
"flag"
"fmt"
"net/http"
"muyu-apiserver/gateway/internal/config"
"muyu-apiserver/gateway/internal/handler"
"muyu-apiserver/gateway/internal/svc"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
"google.golang.org/grpc/status"
)
var configFile = flag.String("f", "etc/gateway-api.yaml", "the config file")
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
}
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf, rest.WithCors())
defer server.Stop()
server.Use(func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if token := r.Header.Get("X-Token"); token != "" {
r.Header.Set("Authorization", "Bearer "+token)
}
next(w, r)
}
})
httpx.SetErrorHandlerCtx(func(ctx context.Context, err error) (int, interface{}) {
s, ok := status.FromError(err)
if ok {
return http.StatusOK, &Response{Code: int(s.Code()), Msg: s.Message()}
}
return http.StatusOK, &Response{Code: 1001, Msg: err.Error()}
})
httpx.SetOkHandler(func(ctx context.Context, a interface{}) interface{} {
return &Response{Code: 0, Msg: "success", Data: a}
})
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}