Skip to content

add log module - 2 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ jobs:
run: go mod download

- name: Run tests
run: go test -count=1 -coverprofile=coverage.txt ./... &&
grep -v "^github.com/go-spring/spring-core/log" coverage.txt > coverage.txt.tmp && mv coverage.txt.tmp coverage.txt
run: go test -count=1 -coverprofile=coverage.txt ./...

- name: Upload results to Codecov
uses: codecov/codecov-action@v5
Expand Down
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ go.work.sum

/conf/remote/

doc/examples/bookman/conf/
doc/examples/bookman/log/*.log
docs/**/bookman/conf/
docs/**/bookman/log/*.log

coverage.txt
coverage.txt

log/access.log
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
<img src="https://img.shields.io/github/license/go-spring/spring-core" alt="license"/>
<img src="https://img.shields.io/github/go-mod/go-version/go-spring/spring-core" alt="go-version"/>
<img src="https://img.shields.io/github/v/release/go-spring/spring-core?include_prereleases" alt="release"/>
<img src="https://codecov.io/gh/go-spring/spring-core/branch/main/graph/badge.svg" alt="test-coverage"/>
<a href="https://codecov.io/gh/go-spring/spring-core" >
<img src="https://codecov.io/gh/go-spring/spring-core/graph/badge.svg?token=SX7CV1T0O8" alt="test-coverage"/>
</a>
<a href="https://deepwiki.com/go-spring/spring-core"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a>
</div>

[中文](README_CN.md)
[English](README.md) | [中文](README_CN.md)

**Go-Spring is a high-performance framework for modern Go application development, inspired by the Spring / Spring Boot
ecosystem in the Java community.**
Expand Down
7 changes: 5 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
<img src="https://img.shields.io/github/license/go-spring/spring-core" alt="license"/>
<img src="https://img.shields.io/github/go-mod/go-version/go-spring/spring-core" alt="go-version"/>
<img src="https://img.shields.io/github/v/release/go-spring/spring-core?include_prereleases" alt="release"/>
<img src="https://codecov.io/gh/go-spring/spring-core/branch/main/graph/badge.svg" alt="test-coverage"/>
<a href="https://codecov.io/gh/go-spring/spring-core" >
<img src="https://codecov.io/gh/go-spring/spring-core/graph/badge.svg?token=SX7CV1T0O8" alt="test-coverage"/>
</a>
<a href="https://deepwiki.com/go-spring/spring-core"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a>
</div>

[English](README.md)
[English](README.md) | [中文](README_CN.md)

**Go-Spring 是一个面向现代 Go 应用开发的高性能框架,灵感源自 Java 社区的 Spring / Spring Boot。**
它的设计理念深度融合 Go 语言的特性,既保留了 Spring 世界中成熟的开发范式,如依赖注入(DI)、自动配置和生命周期管理,
Expand Down
2 changes: 1 addition & 1 deletion conf/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestExpr(t *testing.T) {
var v struct {
A int `value:"${a}" expr:"checkInt(2$)"`
}
p := conf.Map(map[string]interface{}{
p := conf.Map(map[string]any{
"a": 4,
})
err := p.Bind(&v)
Expand Down
Empty file removed doc/examples/servers/gin/.keep
Empty file.
1 change: 1 addition & 0 deletions docs/0. overview/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo 项目概览、目标、特性,解决了什么问题
1 change: 1 addition & 0 deletions docs/1. getting-started/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo 快速开始、安装、HelloWorld、项目结构引导
1 change: 1 addition & 0 deletions docs/2. concepts/concepts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo 核心架构理念、DI、配置系统、生命周期管理等
1 change: 1 addition & 0 deletions docs/3. guides/guides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo 常见操作,如创建服务、注入 Bean、读取配置、测试
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
181 changes: 181 additions & 0 deletions docs/4. examples/chatAI/chatAI.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8"/>
<title>Chat AI</title>
<style>
body {
font-family: "Segoe UI", sans-serif;
background-color: #f3f4f6;
padding: 2em;
display: flex;
justify-content: center;
}

.container {
background: #fff;
padding: 2em;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 1200px;
}

h1 {
margin-bottom: 1em;
font-size: 1.5em;
color: #333;
}

.input-row {
display: flex;
gap: 1em;
}

input[type="text"] {
flex: 1;
padding: 0.75em;
border: 1px solid #ccc;
border-radius: 8px;
font-size: 1em;
outline: none;
transition: border 0.2s;
}

input[type="text"]:focus {
border-color: #4f46e5;
}

button {
padding: 0.75em 1.5em;
background-color: #4f46e5;
color: white;
border: none;
border-radius: 8px;
font-size: 1em;
cursor: pointer;
transition: background-color 0.2s;
}

button:hover {
background-color: #4338ca;
}

button:disabled {
background-color: #a5b4fc;
cursor: not-allowed;
}

#response {
margin-top: 1.5em;
padding: 1em;
background-color: #f9fafb;
border: 1px solid #ddd;
border-radius: 8px;
min-height: 240px;
white-space: pre-wrap;
font-family: monospace;
line-height: 1.4;
position: relative;
}

.loading-dots::after {
content: '';
display: inline-block;
width: 1em;
text-align: left;
animation: dots 1s steps(3, end) infinite;
}

@keyframes dots {
0% {
content: '';
}
33% {
content: '.';
}
66% {
content: '..';
}
100% {
content: '...';
}
}
</style>
</head>
<body>
<div class="container">
<h1>🧠 Chat AI</h1>
<div class="input-row">
<input type="text" id="userInput" placeholder="请输入你的问题..."/>
<button id="sendBtn">Send</button>
</div>
<div id="response"></div>
</div>

<script>
const sendBtn = document.getElementById("sendBtn");
const userInput = document.getElementById("userInput");
const responseDiv = document.getElementById("response");

let controller = null;

sendBtn.addEventListener("click", async () => {
if (sendBtn.textContent === "Stop") {
if (controller) controller.abort();
sendBtn.textContent = "Send";
return;
}

const input = userInput.value.trim();
if (!input) return;

responseDiv.textContent = "";
responseDiv.classList.add("loading-dots");
sendBtn.textContent = "Stop";
controller = new AbortController();

try {
const res = await fetch("/chat/sse", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({message: input}),
signal: controller.signal
});

if (!res.ok) throw new Error(`HTTP error: ${res.status}`);

const reader = res.body.getReader();
const decoder = new TextDecoder("utf-8");

while (true) {
const {done, value} = await reader.read();
if (done) break;
const chunk = decoder.decode(value, {stream: true});

// Remove the two line breaks specified in the SSE protocol
// and add a line break at the end
const processedChunk = chunk.endsWith('\n\n')
? chunk.slice(0, -1)
: chunk;

responseDiv.textContent += processedChunk;
}

} catch (err) {
if (err.name === 'AbortError') {
responseDiv.textContent += "\n⛔️ The user has terminated the connection.";
} else {
responseDiv.textContent = `❌ Error: ${err.message}`;
}
} finally {
sendBtn.textContent = "Send";
responseDiv.classList.remove("loading-dots");
controller = null;
}
});
</script>
</body>
</html>
15 changes: 15 additions & 0 deletions docs/4. examples/chatAI/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module chatai

go 1.24

require github.com/go-spring/spring-core v0.0.0

require (
github.com/expr-lang/expr v1.17.2 // indirect
github.com/magiconair/properties v1.8.10 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/spf13/cast v1.7.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/go-spring/spring-core => ../../../
File renamed without changes.
73 changes: 73 additions & 0 deletions docs/4. examples/chatAI/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2025 The Go-Spring Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"embed"
"fmt"
"net/http"
"time"

"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/util/sysconf"
)

//go:embed chatAI.html
var files embed.FS

func main() {
// Disable the write timeout for the HTTP server
sysconf.Set("http.server.writeTimeout", "0")

// Serve static files from the embedded file system under the "/public/" path
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.FS(files))))

// Handle the Server-Sent Events (SSE) endpoint
http.HandleFunc("/chat/sse", func(w http.ResponseWriter, r *http.Request) {

// Set the necessary HTTP headers for SSE
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")

flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}

// Send an SSE message every second for 10 seconds
for i := 0; i < 10; i++ {
select {
case <-r.Context().Done():
// Exit the loop if the client disconnects
return
default:
// Each SSE message must end with two newlines to be recognized correctly by the client
// See more about SSE protocol: https://www.ruanyifeng.com/blog/2017/05/server-sent_events.html
fmt.Fprintf(w, "data: Message %d at %s\n\n", i, time.Now().Format("15:04:05"))
flusher.Flush()
time.Sleep(1 * time.Second)
}
}
})

gs.Run()
}

// open http://127.0.0.1:9090/public/chatAI.html in the browser
1 change: 1 addition & 0 deletions docs/4. examples/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo 完整的 demo 项目或代码片段
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package main

import (
"context"
"net/http"

"github.com/go-spring/spring-core/gs"
"github.com/go-spring/spring-core/util/syslog"
)

func main() {
Expand All @@ -34,7 +36,10 @@ func main() {
// - Property Binding: Binds external configs (YAML, ENV) into structs.
// - Dependency Injection: Wires beans automatically.
// - Dynamic Refresh: Updates configs at runtime without restart.
gs.Run()
gs.RunWith(func(ctx context.Context) error {
syslog.Infof("app started")
return nil
})
}

//~ curl http://127.0.0.1:9090/echo
Expand Down
File renamed without changes.
File renamed without changes.
Loading