Skip to content

Commit d81cdbd

Browse files
committed
Update getting started and index documentation
1 parent eb15e94 commit d81cdbd

File tree

2 files changed

+74
-64
lines changed

2 files changed

+74
-64
lines changed

src/guide/getting-started.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
# Getting Started
22

33
## Install
4-
`go get github.com/go-spring-projects/go-spring@latest`
4+
`go get go-spring.dev/spring@latest`
55

66
## Hello world
77

88
```go
99
package main
1010

1111
import (
12+
"context"
1213
"log/slog"
1314

14-
"github.com/go-spring-projects/go-spring/gs"
15+
"go-spring.dev/spring/gs"
1516
)
1617

1718
type MyApp struct {
1819
Logger *slog.Logger `logger:""`
1920
}
2021

21-
func (m *MyApp) OnInit(ctx gs.Context) error {
22-
m.Logger.Info("HELLO WORLD")
22+
func (m *MyApp) OnInit(ctx context.Context) error {
23+
m.Logger.Info("Hello world")
2324
return nil
2425
}
2526

2627
func main() {
27-
// register object bean `MyApp`
28+
// register object bean
2829
gs.Object(new(MyApp))
2930

30-
// start go-spring boot app.
31+
// run go-spring boot app
3132
gs.Run()
3233
}
3334

src/guide/index.md

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Go-Spring
22

3-
<img align="right" width="159px" src="/logo.svg"/>
4-
53
`Go-Spring` vision is to empower Go programmers with a powerful programming framework similar to Java `Spring`. It is dedicated to providing users with a simple, secure, and reliable programming experience.
64

7-
This project initial code based from [go-spring/go-spring](https://github.com/go-spring/go-spring) created by [lvan100](https://github.com/lvan100)
8-
95
### Install
10-
`go get github.com/go-spring-projects/go-spring@latest`
6+
`go get go-spring.dev/spring@latest`
7+
8+
### Features
9+
* **IoC Container**: Implements an inversion of control (IoC) container based on reflection, supporting the injection of structs, functions, and constants. This means you can use the `autowired` tag to automatically inject dependencies without having to manage them manually.
10+
* **Flexible Configuration Management**: Taking inspiration from Spring's @Value annotation, Go-Spring allows you to fetch configuration items from multiple sources (such as environment variables, files, command-line arguments, etc.). This brings unprecedented flexibility in configuration management.
11+
* **Validator Extension for Configuration**: Extends its robust configuration management capabilities with support for custom validator extensions. This enables you to perform validity checks on properties, ensuring only valid configurations are applied to your application.
12+
* **Logger Based on Standard slog**: Provides built-in logger support using the standard library slog for effective and streamlined logging. This enhancement offers clear, concise, and well-structured logging information that aids in system debugging and performance monitoring.
13+
* **Dynamic Property Refreshing**: Provides dynamic property refreshing which lets you update the application properties on-the-fly without needing to reboot your application. It caters to the needs of applications that require high availability and real-time responsiveness.
14+
* **Dependency Ordered Application Events**: Ensures the correct notification of initialization and destruction events according to the lifecycle of objects, following the order of bean dependencies. This enhances the robustness and reliability of the system during its lifecycle operations.
1115

1216
### IoC container
1317

@@ -43,40 +47,43 @@ In addition to implementing a powerful IoC container similar to Java Spring, Go-
4347
package main
4448

4549
import (
50+
"context"
4651
"log/slog"
4752

48-
"github.com/go-spring-projects/go-spring/gs"
53+
"go-spring.dev/spring/gs"
4954
)
5055

5156
type MyApp struct {
52-
Logger *slog.Logger `logger:""`
57+
Logger *slog.Logger `logger:""`
5358
}
5459

55-
func (m *MyApp) OnInit(ctx gs.Context) error {
56-
m.Logger.Info("Hello world")
57-
return nil
60+
func (m *MyApp) OnInit(ctx context.Context) error {
61+
m.Logger.Info("Hello world")
62+
return nil
5863
}
5964

6065
func main() {
61-
// register object bean
62-
gs.Object(new(MyApp))
63-
64-
// run go-spring boot app
65-
gs.Run()
66+
// register object bean
67+
gs.Object(new(MyApp))
68+
69+
// run go-spring boot app
70+
gs.Run()
6671
}
6772

6873
// Output:
69-
// time=2023-09-25T14:50:32.927+08:00 level=INFO source=main.go:14 msg="Hello world" logger=go-spring type=main.MyApp
74+
// time=2023-09-25T14:50:32.927+08:00 level=INFO source=main.go:14 msg="Hello world" logger=go-spring
7075
```
7176

7277
#### Bean register
7378

7479
```go
7580
package mypkg
7681

82+
import "go-spring.dev/spring/gs"
83+
7784
type MyApp struct {}
7885

79-
type NewApp() *MyApp {
86+
func NewApp() *MyApp {
8087
return &MyApp{}
8188
}
8289

@@ -99,13 +106,13 @@ Property binding and bean injection annotations are marked using struct field ta
99106

100107
Bind properties to a value, the bind value can be primitive type, map, slice, struct. When binding to struct, the tag 'value' indicates which properties should be bind. The 'value' tags are defined by value:"${a:=b}", 'a' is the property name, 'b' is the default value.
101108

102-
![binding](/image/binding.svg)
109+
![binding](binding.svg)
103110

104111
##### Dependency Injection
105112

106113
Dependency Injection is a design pattern used to implement decoupling between classes and the management of dependencies. It transfers the responsibility of creating and maintaining dependencies to an external container, so that the class does not need to instantiate dependent objects itself. Instead, the external container dynamically injects the dependencies.
107114

108-
![autowire](/image/autowire.svg)
115+
![autowire](autowire.svg)
109116

110117
### Conditional registering
111118

@@ -179,11 +186,12 @@ In this example, we will use [go-validator/validator](https://github.com/go-vali
179186
package main
180187
181188
import (
189+
"context"
182190
"fmt"
183191
"log/slog"
184192
185-
"github.com/go-spring-projects/go-spring/conf"
186-
"github.com/go-spring-projects/go-spring/gs"
193+
"go-spring.dev/spring/conf"
194+
"go-spring.dev/spring/gs"
187195
"gopkg.in/validator.v2"
188196
)
189197
@@ -219,7 +227,7 @@ type MysqlDatabase struct {
219227
Options DBOptions `value:"${db}"`
220228
}
221229

222-
func (md *MysqlDatabase) OnInit(ctx gs.Context) error {
230+
func (md *MysqlDatabase) OnInit(ctx context.Context) error {
223231
md.Logger.Info("mysql connection summary",
224232
"url", fmt.Sprintf("mysql://%s:%s@%s:%d/%s", md.Options.UserName, md.Options.Password, md.Options.IP, md.Options.Port, md.Options.DB))
225233
return nil
@@ -255,19 +263,20 @@ Allows dynamically refresh properties during runtime, not only supporting basic
255263
package main
256264

257265
import (
266+
"context"
258267
"fmt"
259268
"log/slog"
260269
"net/http"
261270

262-
"github.com/go-spring-projects/go-spring/dync"
263-
"github.com/go-spring-projects/go-spring/gs"
271+
"go-spring.dev/spring/dync"
272+
"go-spring.dev/spring/gs"
264273
)
265274

266275
type Handler struct {
267276
Open dync.Bool `value:"${server.open:=true}"`
268277
}
269278

270-
func (h *Handler) OnInit(ctx gs.Context) error {
279+
func (h *Handler) OnInit(ctx context.Context) error {
271280

272281
http.HandleFunc("/server/status", func(writer http.ResponseWriter, request *http.Request) {
273282
if !h.Open.Value() {
@@ -284,9 +293,9 @@ type Server struct {
284293
Logger *slog.Logger `logger:""`
285294
}
286295

287-
func (s *Server) OnInit(ctx gs.Context) error {
296+
func (s *Server) OnInit(ctx context.Context) error {
288297

289-
props := ctx.(gs.Container).Properties()
298+
props := gs.FromContext(ctx).(gs.Container).Properties()
290299

291300
http.HandleFunc("/server/status/open", func(writer http.ResponseWriter, request *http.Request) {
292301
props.Set("server.open", "true")
@@ -315,7 +324,6 @@ func main() {
315324
}
316325
}
317326

318-
319327
// Output:
320328
//
321329
// $ curl http://127.0.0.1:7878/server/status
@@ -340,12 +348,13 @@ Automatically injects named logger, the logger library powered by the std [slog]
340348
package main
341349

342350
import (
351+
"context"
343352
"io"
344353
"log/slog"
345354
"os"
346355
"strings"
347356

348-
"github.com/go-spring-projects/go-spring/gs"
357+
"go-spring.dev/spring/gs"
349358
)
350359

351360
func init() {
@@ -356,27 +365,27 @@ func init() {
356365
Primary bool `value:"${primary:=false}"`
357366
}
358367

359-
/*
360-
logger:
361-
# application logger.
362-
app:
363-
level: debug
364-
file: /your/path/app.log
365-
console: false
366-
primary: true
367-
368-
# system logger.
369-
sys:
370-
level: info
371-
file: /your/path/sys.log
372-
console: true
373-
374-
# trace logger.
375-
trace:
376-
level: info
377-
file: /your/path/trace.log
378-
console: false
379-
*/
368+
/*
369+
logger:
370+
# application logger.
371+
app:
372+
level: debug
373+
file: /your/path/app.log
374+
console: false
375+
primary: true
376+
377+
# system logger.
378+
sys:
379+
level: info
380+
file: /your/path/sys.log
381+
console: true
382+
383+
# trace logger.
384+
trace:
385+
level: info
386+
file: /your/path/trace.log
387+
console: false
388+
*/
380389

381390
gs.OnProperty("logger", func(loggers map[string]Logger) {
382391
for name, logger := range loggers {
@@ -425,15 +434,15 @@ type App struct {
425434
TraceLogger *slog.Logger `logger:"${app.trace.logger:=trace}"`
426435
}
427436

428-
func (app *App) OnInit(ctx gs.Context) error {
437+
func (app *App) OnInit(ctx context.Context) error {
429438
app.Logger.Info("hello primary logger")
430439
app.SysLogger.Info("hello system logger")
431440
app.TraceLogger.Info("hello trace logger")
432441
return nil
433442
}
434443

435444
func main() {
436-
445+
437446
gs.Property("logger.app.level", "debug")
438447
gs.Property("logger.app.file", "./app.log")
439448
gs.Property("logger.app.console", "true")
@@ -455,16 +464,16 @@ func main() {
455464
}
456465

457466
// Output:
458-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello primary logger","logger":"app","type":"main.App"}
459-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello system logger","logger":"sys","type":"main.App"}
460-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello trace logger","logger":"trace","type":"main.App"}
467+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello primary logger","logger":"app"}
468+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello system logger","logger":"sys"}
469+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello trace logger","logger":"trace"}
461470
```
462471

463472
### Dependent order event
464473

465474
Initialization and deinitialization based on dependency order, everything will be executed as expected.
466475

467-
![order](/image/event.svg)
476+
![order](event.svg)
468477

469478
### Project layout
470479

@@ -503,8 +512,8 @@ import (
503512
"os"
504513
"strings"
505514

506-
"github.com/go-spring-projects/go-spring/gs"
507515
"github.com/urfave/cli/v2"
516+
"go-spring.dev/spring/gs"
508517
)
509518

510519
//import _ "testapp/pkg/infra"
@@ -554,4 +563,4 @@ func main() {
554563

555564
### License
556565

557-
The `Go-Spring` is released under version 2.0 of the Apache License.
566+
The `Go-Spring` is released under version 2.0 of the Apache License.

0 commit comments

Comments
 (0)