generated from origadmin/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
102 lines (90 loc) · 2.39 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2024 OrigAdmin. All rights reserved.
*/
// Package runtime provides functions for loading configurations and registering services.
package runtime
import (
"sync"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport"
"github.com/origadmin/runtime/application"
"github.com/origadmin/runtime/config"
"github.com/origadmin/runtime/log"
"github.com/origadmin/runtime/middleware"
"github.com/origadmin/runtime/registry"
"github.com/origadmin/runtime/service"
"github.com/origadmin/toolkits/errors"
)
const (
DefaultEnvPrefix = "ORIGADMIN_RUNTIME_SERVICE"
)
type Builder interface {
config.Builder
registry.Builder
service.Builder
MiddlewareBuilders
middlewareBuildRegistry
}
// build is a global variable that holds an instance of the builder struct.
var (
once = &sync.Once{}
build = &builder{}
)
// ErrNotFound is an error that is returned when a ConfigBuilder or RegistryBuilder is not found.
var ErrNotFound = errors.String("not found")
type Runtime struct {
once sync.Once
Debug bool
EnvPrefix string
WorkDir string
Application application.Application
Logging log.Logging
Config config.Config
Registry registry.Registry
Middleware middleware.Middleware
Service service.Service
}
func (r *Runtime) Load() error {
var rerr error
r.once.Do(func() {
//// todo: add init and check before load
//// todo: load config
//if err := r.Config.Load(); err != nil {
// rerr = errors.Wrap(err, "load config")
// return
//}
//// todo: load registry
//if err := r.Registry.Load(); err != nil {
// rerr = errors.Wrap(err, "load registry")
// return
//}
//// todo: load service
//if err := r.Service.Load(); err != nil {
// rerr = errors.Wrap(err, "load service")
// return
//}
//// todo: load middleware
//if err := r.Middleware.Load(); err != nil {
// rerr = errors.Wrap(err, "load middleware")
// return
//}
})
return rerr
}
func (r *Runtime) Build(rr registry.Registry, servers ...transport.Server) *kratos.App {
// todo: add init and check before build
return kratos.New(
kratos.ID(r.Application.ID),
kratos.Name(r.Application.Name),
kratos.Version(r.Application.Version),
kratos.Metadata(r.Application.Metadata),
kratos.Logger(r.Logging.Logger),
kratos.Server(servers...),
kratos.Registrar(rr),
)
}
func New() Runtime {
return Runtime{
EnvPrefix: DefaultEnvPrefix,
}
}