Skip to content

Commit 95e5431

Browse files
committed
small enhancements
1 parent 16099eb commit 95e5431

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

acme.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
package httpserver
2+
3+
//go:generate echo hello acme
4+
func T() {}

example/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"log/slog"
66
"net/http"
7-
"os"
87
"strconv"
98
"time"
109

@@ -18,7 +17,7 @@ const (
1817
var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1918

2019
select {
21-
case <-time.Tick(3 * time.Second):
20+
case <-time.Tick(1 * time.Second):
2221
w.Write([]byte("Hello, World"))
2322
case <-r.Context().Done():
2423
w.Write([]byte("server stopped"))
@@ -31,7 +30,7 @@ func nextRequestID() string {
3130
}
3231

3332
func main() {
34-
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
33+
slog.SetDefault(httpserver.DefaultLoggerDevelopment)
3534

3635
mw := logging(slog.Default())(handler)
3736
mw = tracing(nextRequestID)(mw)

httpserver.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"net/http"
1111
"os"
12+
"os/exec"
1213
"os/signal"
1314
"path"
1415
"path/filepath"
@@ -155,7 +156,26 @@ func (s *Server) startGracefulShutdown() error {
155156
}
156157

157158
func (s *Server) handleFileChange(event notify.EventInfo) {
158-
s.log.Info("file changed", "event", event.Event(), "path", event.Path())
159+
isGoFile := strings.HasSuffix(event.Path(), ".go")
160+
if !isGoFile {
161+
return
162+
}
163+
164+
moduleRoot := modulePath()
165+
pathToGenerate := strings.Replace(path.Dir(event.Path()), moduleRoot, ".", 1)
166+
s.log.Info("file changed", "event", event.Event(), "path", pathToGenerate)
167+
genCmd := exec.Command("go", "generate", pathToGenerate)
168+
genCmd.Dir = moduleRoot
169+
genCmd.Stdout = os.Stdout
170+
genCmd.Stderr = os.Stderr
171+
172+
err := genCmd.Run()
173+
if err != nil {
174+
s.log.Error("go generate failed", "error", err)
175+
return
176+
}
177+
178+
os.Getwd()
159179
}
160180

161181
func watchForFileChanges(logger *slog.Logger) (c chan notify.EventInfo) {

options.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import (
1010

1111
const defaultShutdownTimeout = 10 * time.Second
1212

13+
var (
14+
DefaultLoggerProduction = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
15+
Level: slog.LevelInfo,
16+
}))
17+
DefaultLoggerDevelopment = slog.New(tint.NewHandler(os.Stdout, &tint.Options{
18+
Level: slog.LevelDebug,
19+
TimeFormat: time.TimeOnly,
20+
}))
21+
)
22+
1323
type Option func(*Server)
1424

1525
func setDefaultLogger(server *Server) {
@@ -18,15 +28,9 @@ func setDefaultLogger(server *Server) {
1828
switch server.mode {
1929

2030
case ModeProduction:
21-
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
22-
Level: slog.LevelInfo,
23-
}))
24-
31+
logger = DefaultLoggerProduction
2532
default:
26-
logger = slog.New(tint.NewHandler(os.Stdout, &tint.Options{
27-
Level: slog.LevelDebug,
28-
TimeFormat: time.TimeOnly,
29-
}))
33+
logger = DefaultLoggerDevelopment
3034
}
3135

3236
WithLogger(logger.WithGroup("httpserver"))(server)

0 commit comments

Comments
 (0)