Files
GoSally/cmd/node/node.go
alex 96fb13e3c7 Refactor and restructure core components
- Removed obsolete files: `hanle_multi.go`, `logger.go`, `handle_com.go`, `handle_list.go`, `server.go`, and `utils.go` from the `general_server` and `sv1` packages.
- Introduced new implementations for the `GeneralServer` and `HandlerV1` in the `core` package, enhancing the overall architecture.
- Updated the `go.mod` and `go.sum` files to include new dependencies.
- Added a new configuration structure in `config.yaml` and `config.go` for better management of application settings.
- Implemented a Lua command handling mechanism in `handle_com.go` and `handle_list.go` for improved command execution.
- Enhanced logging capabilities with a new logger setup in `logger.go`.
- Added a new Lua script `http.lua` for handling HTTP requests.
2025-06-26 16:50:35 +03:00

61 lines
1.6 KiB
Go

package main
import (
"log/slog"
"net/http"
"regexp"
"github.com/akyaiy/GoSally-mvp/core/config"
gs "github.com/akyaiy/GoSally-mvp/core/general_server"
"github.com/akyaiy/GoSally-mvp/core/logs"
"github.com/akyaiy/GoSally-mvp/core/sv1"
"github.com/go-chi/chi/v5"
)
var log *slog.Logger
var cfg *config.ConfigConf
func init() {
cfg = config.MustLoadConfig()
log = logs.SetupLogger(cfg.Mode)
log = log.With("mode", cfg.Mode)
log.Info("Initializing server", slog.String("address", cfg.HTTPServer.Address))
log.Debug("Server running in debug mode")
}
func main() {
serverv1 := sv1.InitV1Server(&sv1.HandlerV1InitStruct{
Log: *logs.SetupLogger(cfg.Mode),
Config: cfg,
AllowedCmd: regexp.MustCompile(`^[a-zA-Z0-9]+$`),
ListAllowedCmd: regexp.MustCompile(`^[a-zA-Z0-9_-]+$`),
Ver: "v1",
})
s := gs.InitGeneral(&gs.GeneralServerInit{
Log: *logs.SetupLogger(cfg.Mode),
Config: cfg,
}, serverv1)
r := chi.NewRouter()
r.Route("/api/{ver}/com", func(r chi.Router) {
r.Get("/", s.HandleList)
r.Get("/{cmd}", s.Handle)
})
r.NotFound(serverv1.ErrNotFound)
if cfg.TlsEnabled == "true" {
log.Info("Server started with TLS", slog.String("address", cfg.Address))
err := http.ListenAndServeTLS(cfg.Address, cfg.CertFile, cfg.KeyFile, r)
if err != nil {
log.Error("Failed to start HTTPS server", slog.String("error", err.Error()))
}
}
log.Info("Server started", slog.String("address", cfg.Address))
err := http.ListenAndServe(cfg.Address, r)
if err != nil {
log.Error("Failed to start HTTP server", slog.String("error", err.Error()))
}
}