Skip to content

Commit 2b2c14d

Browse files
authored
Merge pull request #16 from lvan100/main
Add MiniAPI Example Project
2 parents c0bb561 + f78d948 commit 2b2c14d

File tree

14 files changed

+511
-385
lines changed

14 files changed

+511
-385
lines changed

gs/examples/bookman/.cover/cover.html

Lines changed: 258 additions & 302 deletions
Large diffs are not rendered by default.

gs/examples/bookman/init.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
"runtime"
24+
25+
"github.com/go-spring/spring-core/gs"
26+
)
27+
28+
const banner = `
29+
____ _ __ __
30+
| __ ) ___ ___ | | __| \/ | __ _ _ __
31+
| _ \ / _ \ / _ \ | |/ /| |\/| | / _' || '_ \
32+
| |_) || (_) || (_) || < | | | || (_| || | | |
33+
|____/ \___/ \___/ |_|\_\|_| |_| \__,_||_| |_|
34+
`
35+
36+
func init() {
37+
gs.Banner(banner)
38+
}
39+
40+
func init() {
41+
var execDir string
42+
_, filename, _, ok := runtime.Caller(0)
43+
if ok {
44+
execDir = filepath.Dir(filename)
45+
}
46+
err := os.Chdir(execDir)
47+
if err != nil {
48+
panic(err)
49+
}
50+
workDir, err := os.Getwd()
51+
if err != nil {
52+
panic(err)
53+
}
54+
fmt.Println(workDir)
55+
}

gs/examples/bookman/main.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,23 @@ import (
2121
"fmt"
2222
"io"
2323
"net/http"
24-
"os"
25-
"path/filepath"
26-
"runtime"
2724
"time"
2825

2926
"github.com/go-spring/spring-core/gs"
30-
"github.com/go-spring/spring-core/util/syslog"
3127
"github.com/lvan100/go-loop"
3228

3329
_ "github.com/go-spring/spring-core/gs/examples/bookman/src/app"
3430
_ "github.com/go-spring/spring-core/gs/examples/bookman/src/biz"
3531
)
3632

37-
const banner = `
38-
____ _ __ __
39-
| __ ) ___ ___ | | __| \/ | __ _ _ __
40-
| _ \ / _ \ / _ \ | |/ /| |\/| | / _' || '_ \
41-
| |_) || (_) || (_) || < | | | || (_| || | | |
42-
|____/ \___/ \___/ |_|\_\|_| |_| \__,_||_| |_|
43-
`
44-
4533
func init() {
46-
gs.Banner(banner)
4734
gs.SetActiveProfiles("online")
4835
gs.EnableSimplePProfServer(true)
49-
}
50-
51-
func init() {
5236
gs.FuncJob(runTest).Name("#job")
5337
}
5438

55-
func init() {
56-
var execDir string
57-
_, filename, _, ok := runtime.Caller(0)
58-
if ok {
59-
execDir = filepath.Dir(filename)
60-
}
61-
err := os.Chdir(execDir)
62-
if err != nil {
63-
panic(err)
64-
}
65-
workDir, err := os.Getwd()
66-
if err != nil {
67-
panic(err)
68-
}
69-
fmt.Println(workDir)
70-
}
71-
7239
func main() {
73-
// Start the application and log errors if startup fails
74-
if err := gs.Run(); err != nil {
75-
syslog.Errorf("app run failed: %s", err.Error())
76-
}
40+
gs.Run()
7741
}
7842

7943
// runTest performs a simple test.

gs/examples/bookman/public/.keep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
It Works!

gs/examples/bookman/src/app/bootstrap/bootstrap.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ import (
2626
)
2727

2828
func init() {
29+
// Register a function runner to initialize the remote configuration setup.
2930
gs.B.FuncRunner(initRemoteConfig).OnProfiles("online")
3031
}
3132

32-
// initRemoteConfig initializes the remote configuration setup
33+
// initRemoteConfig initializes the remote configuration setup.
34+
// It first attempts to retrieve remote config, then starts a background job
35+
// to periodically refresh the configuration.
3336
func initRemoteConfig() error {
3437
if err := getRemoteConfig(); err != nil {
3538
return err
@@ -38,7 +41,36 @@ func initRemoteConfig() error {
3841
return nil
3942
}
4043

41-
// refreshRemoteConfig periodically refreshes the remote configuration
44+
// getRemoteConfig fetches and writes the remote configuration to a local file.
45+
// It creates necessary directories and generates a properties file containing.
46+
func getRemoteConfig() error {
47+
err := os.MkdirAll("./conf/remote", os.ModePerm)
48+
if err != nil {
49+
return err
50+
}
51+
52+
const data = `
53+
server.addr=0.0.0.0:9090
54+
55+
log.access.name=access.log
56+
log.access.dir=./log
57+
58+
log.biz.name=biz.log
59+
log.biz.dir=./log
60+
61+
log.dao.name=dao.log
62+
log.dao.dir=./log
63+
64+
refresh_time=%v
65+
`
66+
67+
const file = "conf/remote/app-online.properties"
68+
str := fmt.Sprintf(data, time.Now().UnixMilli())
69+
return os.WriteFile(file, []byte(str), os.ModePerm)
70+
}
71+
72+
// refreshRemoteConfig runs a continuous loop to periodically update configuration.
73+
// It refreshes every 500ms until context cancellation.
4274
func refreshRemoteConfig(ctx context.Context) error {
4375
for {
4476
select {
@@ -58,29 +90,3 @@ func refreshRemoteConfig(ctx context.Context) error {
5890
}
5991
}
6092
}
61-
62-
// getRemoteConfig fetches and writes the remote configuration to a local file
63-
func getRemoteConfig() error {
64-
err := os.MkdirAll("./conf/remote", os.ModePerm)
65-
if err != nil {
66-
return err
67-
}
68-
69-
const data = `
70-
server.addr=0.0.0.0:9090
71-
72-
log.access.name=access.log
73-
log.access.dir=./log
74-
75-
log.biz.name=biz.log
76-
log.biz.dir=./log
77-
78-
log.dao.name=dao.log
79-
log.dao.dir=./log
80-
81-
refresh_time=%v`
82-
83-
const file = "conf/remote/app-online.properties"
84-
str := fmt.Sprintf(data, time.Now().UnixMilli())
85-
return os.WriteFile(file, []byte(str), os.ModePerm)
86-
}

gs/examples/bookman/src/app/common/httpsvr/httpsvr.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ import (
2727
)
2828

2929
func init() {
30-
gs.Provide(NewServeMux, gs.IndexArg(1, gs.TagArg("access")))
30+
// Registers a custom ServeMux to replace the default implementation.
31+
gs.Provide(
32+
NewServeMux,
33+
gs.IndexArg(1, gs.TagArg("access")),
34+
)
3135
}
3236

3337
// NewServeMux Creates a new HTTP request multiplexer and registers
3438
// routes with access logging middleware.
3539
func NewServeMux(c *controller.Controller, logger *slog.Logger) *http.ServeMux {
3640
mux := http.NewServeMux()
3741
proto.RegisterRouter(mux, c, Access(logger))
42+
mux.Handle("GET /", http.FileServer(http.Dir("./public")))
3843
return mux
3944
}
4045

gs/examples/miniapi/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"net/http"
21+
22+
"github.com/go-spring/spring-core/gs"
23+
)
24+
25+
func main() {
26+
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
27+
_, _ = w.Write([]byte("hello world!"))
28+
})
29+
gs.Run()
30+
}
31+
32+
//~ curl http://127.0.0.1:9090/echo
33+
//hello world!

gs/examples/noweb/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"github.com/go-spring/spring-core/gs"
21+
)
22+
23+
func main() {
24+
gs.Web(false).Run()
25+
}
26+
27+
// ~ telnet 127.0.0.1 9090
28+
// Trying 127.0.0.1...
29+
// telnet: connect to address 127.0.0.1: Connection refused
30+
// telnet: Unable to connect to remote host

gs/examples/startup/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/go-spring/spring-core/gs"
2525
"github.com/go-spring/spring-core/util/sysconf"
26-
"github.com/go-spring/spring-core/util/syslog"
2726
)
2827

2928
func init() {
@@ -36,6 +35,9 @@ func init() {
3635
http.HandleFunc("/refresh", s.Refresh)
3736
return http.DefaultServeMux
3837
})
38+
39+
sysconf.Set("start-time", time.Now().Format(timeLayout))
40+
sysconf.Set("refresh-time", time.Now().Format(timeLayout))
3941
}
4042

4143
const timeLayout = "2006-01-02 15:04:05.999 -0700 MST"
@@ -59,13 +61,7 @@ func (s *Service) Refresh(w http.ResponseWriter, r *http.Request) {
5961
}
6062

6163
func main() {
62-
sysconf.Set("start-time", time.Now().Format(timeLayout))
63-
sysconf.Set("refresh-time", time.Now().Format(timeLayout))
64-
65-
// Start the Go-Spring application. If it fails, log the error.
66-
if err := gs.Run(); err != nil {
67-
syslog.Errorf("app run failed: %s", err.Error())
68-
}
64+
gs.Run()
6965
}
7066

7167
// ➜ curl http://127.0.0.1:9090/echo

0 commit comments

Comments
 (0)