some changes
This commit is contained in:
58
cmd/serve.go
58
cmd/serve.go
@@ -4,11 +4,10 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.oblat.lv/alex/triggerssmith/api"
|
||||
application "git.oblat.lv/alex/triggerssmith/internal/app"
|
||||
@@ -21,30 +20,36 @@ import (
|
||||
var optsServeCmd = struct {
|
||||
ConfigPath *string
|
||||
Debug *bool
|
||||
HideGreetings *bool
|
||||
}{}
|
||||
|
||||
// simple middleware for request logging
|
||||
func loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
// // simple middleware for request logging
|
||||
// func loggingMiddleware(next http.Handler) http.Handler {
|
||||
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// start := time.Now()
|
||||
|
||||
slog.Info("HTTP request",
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.URL.Path),
|
||||
slog.String("remote", r.RemoteAddr),
|
||||
)
|
||||
// slog.Info("HTTP request",
|
||||
// slog.String("method", r.Method),
|
||||
// slog.String("path", r.URL.Path),
|
||||
// slog.String("remote", r.RemoteAddr),
|
||||
// )
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
// next.ServeHTTP(w, r)
|
||||
|
||||
slog.Debug("HTTP request finished",
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.URL.Path),
|
||||
slog.Duration("latency", time.Since(start)),
|
||||
)
|
||||
})
|
||||
}
|
||||
// slog.Debug("HTTP request finished",
|
||||
// slog.String("method", r.Method),
|
||||
// slog.String("path", r.URL.Path),
|
||||
// slog.Duration("latency", time.Since(start)),
|
||||
// )
|
||||
// })
|
||||
// }
|
||||
|
||||
func writePID(path string) error {
|
||||
dir := filepath.Dir(path)
|
||||
err := os.MkdirAll(dir, 0644)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
pid := os.Getpid()
|
||||
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
@@ -61,12 +66,26 @@ var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "Start the server",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
text := fmt.Sprintf(`
|
||||
_______ _____
|
||||
|__ __/ ____|
|
||||
| | | (___
|
||||
| | \___ \
|
||||
| | ____) |
|
||||
|_| |_____/
|
||||
|
||||
TriggerSmith - v%s
|
||||
`, vars.Version)
|
||||
if !*optsServeCmd.HideGreetings {
|
||||
fmt.Println(text)
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
slog.Error("Application panicked", slog.Any("error", r))
|
||||
os.Exit(-1)
|
||||
}
|
||||
}()
|
||||
|
||||
// configure logger
|
||||
if *optsServeCmd.Debug {
|
||||
slog.SetDefault(slog.New(slog.NewTextHandler(cmd.OutOrStdout(), &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: true})))
|
||||
@@ -170,5 +189,6 @@ var serveCmd = &cobra.Command{
|
||||
func init() {
|
||||
optsServeCmd.Debug = serveCmd.Flags().BoolP("debug", "d", false, "Enable debug logs")
|
||||
optsServeCmd.ConfigPath = serveCmd.Flags().StringP("config", "c", "config.yaml", "Path to configuration file")
|
||||
optsServeCmd.HideGreetings = serveCmd.Flags().BoolP("hide-greetings", "g", false, "Hide the welcome message and version when starting the server")
|
||||
rootCmd.AddCommand(serveCmd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user