diff --git a/cmd/client/main.go b/cmd/client/main.go index 137d87c4..50139ed5 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -178,7 +178,7 @@ func AddCommands(rootCmd *cobra.Command) { Run: func(cmd *cobra.Command, args []string) { resp, err := client.Blog.Ping(client.Context(), &proto.PingRequest{}) if err != nil { - panic(err) + log.Fatalln(err) } fmt.Println(resp.Pong) }, diff --git a/gateway/main.go b/gateway/main.go index 73f52f88..f690e995 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -70,7 +70,7 @@ func (g *Gateway) register(ctx context.Context, mux *http.ServeMux) error { // 可跨进程使用。 { // 扩展功能动态生成的样式、脚本、文件。 - mc.Handle(`GET /v3/dynamic/`, http.StripPrefix(`/v3/dynamic`, &dynamic.Handler{})) + mc.Handle(`GET /v3/dynamic/`, http.StripPrefix(`/v3/dynamic`, dynamic.New())) // 博客功能集 mc.Handle(`GET /v3/features/{theme}`, features.New()) diff --git a/modules/utils/dir/dir.go b/modules/utils/dir/dir.go index ee4ed852..86f326ae 100644 --- a/modules/utils/dir/dir.go +++ b/modules/utils/dir/dir.go @@ -1,7 +1,6 @@ package dir import ( - "log" "os" "path/filepath" "runtime" @@ -17,7 +16,7 @@ func init() { panic(err) } Root = rootDir - log.Println(`WorkingDir:`, rootDir) + // log.Println(`WorkingDir:`, rootDir) } type Dir string @@ -27,7 +26,7 @@ func (d Dir) Join(components ...string) string { } func SourceRelativeDir() Dir { - s := strings.TrimPrefix(string(SourceAbsoluteDir()), Root) + s := strings.TrimPrefix(abs(), Root) s = strings.TrimPrefix(s, "/") if s == "" { s = "." @@ -36,12 +35,16 @@ func SourceRelativeDir() Dir { } func SourceAbsoluteDir() Dir { - _, file, _, ok := runtime.Caller(1) + return Dir(abs()) +} + +func abs() string { + _, file, _, ok := runtime.Caller(2) if !ok { panic(`无法获取路径。`) } if Root == "" { panic(`没有设置根目录。`) } - return Dir(filepath.Dir(file)) + return filepath.Dir(file) } diff --git a/protocols/clients/client.go b/protocols/clients/client.go index 45937ba9..6732de34 100644 --- a/protocols/clients/client.go +++ b/protocols/clients/client.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "log" "net" "net/url" "time" @@ -83,7 +82,7 @@ func _NewConn(home, orGrpcAddress string) *grpc.ClientConn { grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) { conn, err := http2tcp.Dial(u.JoinPath(`/v3/grpc`).String(), grpc_proxy.PreSharedKey, ``) if err != nil { - log.Println(home, err) + // log.Println(home, err) return nil, err } return &_NetConn{conn}, nil diff --git a/service/comment.go b/service/comment.go index 506362a9..3f6030f3 100644 --- a/service/comment.go +++ b/service/comment.go @@ -529,7 +529,7 @@ func (s *Service) SetCommentPostID(ctx context.Context, in *proto.SetCommentPost panic(`不能转移子评论`) } // 只是为了判断存在性。 - _, err := s.GetPost(ctx, &proto.GetPostRequest{Id: int32(in.PostId)}) + _, err := txs.GetPost(ctx, &proto.GetPostRequest{Id: int32(in.PostId)}) if err != nil { return err } diff --git a/service/modules/renderers/_dynamic/dynamic.go b/service/modules/renderers/_dynamic/dynamic.go index 0b3fd70b..6e0d00a7 100644 --- a/service/modules/renderers/_dynamic/dynamic.go +++ b/service/modules/renderers/_dynamic/dynamic.go @@ -24,10 +24,15 @@ var style string var script string var files []fs.FS var mod = time.Now() +var once2 sync.Once -type Handler struct { +func New() http.Handler { + once2.Do(callInits) + return &Handler{} } +type Handler struct{} + func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { once.Do(func() { for name, d := range Dynamic { @@ -74,3 +79,17 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) } + +var ( + inits []func() +) + +func RegisterInit(init func()) { + inits = append(inits, init) +} + +func callInits() { + for _, init := range inits { + init() + } +} diff --git a/service/modules/renderers/friends/friends.go b/service/modules/renderers/friends/friends.go index bacc3174..d2e2011e 100644 --- a/service/modules/renderers/friends/friends.go +++ b/service/modules/renderers/friends/friends.go @@ -25,12 +25,14 @@ import ( var _root embed.FS func init() { - dynamic.Dynamic[`friends`] = dynamic.Content{ - Styles: []string{ - string(utils.Must1(_root.ReadFile(`style.css`))), - }, - } - sass.WatchDefaultAsync(string(dir.SourceAbsoluteDir())) + dynamic.RegisterInit(func() { + dynamic.Dynamic[`friends`] = dynamic.Content{ + Styles: []string{ + string(utils.Must1(_root.ReadFile(`style.css`))), + }, + } + sass.WatchDefaultAsync(string(dir.SourceAbsoluteDir())) + }) } type Friends struct {