Align origin/main with upstream/main (GitHub). The two branches diverged due to pre-rebase vs post-rebase merge commits for the k8s-amd64-dockerfiles feature. Includes: multi-stage Go compilation Dockerfiles, pan-bolt inventory features, CRM relations, Excel import tooling, proto/gRPC updates, migration scripts, and tools/ directory. Excludes deploy/bin/ pre-compiled binaries (arm64, not needed for amd64 K3s cluster; builds use multi-stage Dockerfiles now).
66 lines
1.6 KiB
Go
66 lines
1.6 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.StatusBadRequest, &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()
|
|
}
|