(FIX): Needed some work on the logging and handlers.
This commit is contained in:
parent
a78a79bfab
commit
396ca08381
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
/flake.lock
|
/flake.lock
|
||||||
/.env
|
/.env
|
||||||
/*.dump
|
/*.dump
|
||||||
|
/*.log
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/haydenhargreaves/Potion/internal/app/server"
|
import (
|
||||||
|
"github.com/haydenhargreaves/Potion/internal/app/server"
|
||||||
|
)
|
||||||
|
|
||||||
const PORT = 3000
|
const PORT = 3000
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := server.Init(PORT).Setup()
|
s := server.Init(PORT).Setup()
|
||||||
defer s.DB.Close()
|
|
||||||
|
|
||||||
s.Start()
|
s.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,10 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/a-h/templ/examples/integration-gin/gintemplrenderer"
|
"github.com/a-h/templ/examples/integration-gin/gintemplrenderer"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
@ -26,6 +29,7 @@ type Server struct {
|
|||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
deps domain.InjectedDependencies
|
deps domain.InjectedDependencies
|
||||||
logs []logging.Logger
|
logs []logging.Logger
|
||||||
|
cleanupFuncs []func() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init initializes the server with the provided port. CORS settings are defined here.
|
// Init initializes the server with the provided port. CORS settings are defined here.
|
||||||
@ -36,6 +40,7 @@ func Init(port int) *Server {
|
|||||||
port: port,
|
port: port,
|
||||||
config: cors.DefaultConfig(),
|
config: cors.DefaultConfig(),
|
||||||
logs: []logging.Logger{},
|
logs: []logging.Logger{},
|
||||||
|
cleanupFuncs: []func() error{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default logger which logs everything
|
// Default logger which logs everything
|
||||||
@ -66,7 +71,33 @@ func Init(port int) *Server {
|
|||||||
// Start starts the server on the port provided when the server was initialized
|
// Start starts the server on the port provided when the server was initialized
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
logging.LogAll(s.logs, logging.LogLevelDebug, "Server started on :%d\n", s.port)
|
logging.LogAll(s.logs, logging.LogLevelDebug, "Server started on :%d\n", s.port)
|
||||||
s.Router.Run(fmt.Sprintf(":%d", s.port))
|
|
||||||
|
// Create channel that only listens for SIGINT and SIGTERM
|
||||||
|
quit := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := s.Router.Run(fmt.Sprintf(":%d", s.port)); err != nil {
|
||||||
|
logging.LogAll(s.logs, logging.LogLevelFatal, "Server failed: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Block until we get a message on the quit channel
|
||||||
|
<-quit
|
||||||
|
logging.LogAll(s.logs, logging.LogLevelInfo, "Shutting down server...\n")
|
||||||
|
|
||||||
|
s.cleanup()
|
||||||
|
logging.LogAll(s.logs, logging.LogLevelInfo, "Server exited\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) cleanup() {
|
||||||
|
for _, cleanup := range s.cleanupFuncs {
|
||||||
|
// NOTE: Ignoring error
|
||||||
|
cleanup()
|
||||||
|
}
|
||||||
|
if s.DB != nil {
|
||||||
|
s.DB.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: (9/4/2025) Abstract these functions and cleanup. This is fucking messy...
|
// TODO: (9/4/2025) Abstract these functions and cleanup. This is fucking messy...
|
||||||
@ -121,7 +152,7 @@ func (s *Server) Setup() *Server {
|
|||||||
} else {
|
} else {
|
||||||
logging.LogAll(s.logs, logging.LogLevelDebug, "Initialized file logger on file '%s'\n", cfg.LogFilePath)
|
logging.LogAll(s.logs, logging.LogLevelDebug, "Initialized file logger on file '%s'\n", cfg.LogFilePath)
|
||||||
s.logs = append(s.logs, fileLogger)
|
s.logs = append(s.logs, fileLogger)
|
||||||
defer cleanup()
|
s.cleanupFuncs = append(s.cleanupFuncs, cleanup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
type FileLogger struct {
|
type FileLogger struct {
|
||||||
writer io.Writer
|
writer io.Writer
|
||||||
|
file *os.File
|
||||||
filter logging.LogLevel
|
filter logging.LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ func NewFileLogger(filepath string, filter logging.LogLevel) (logging.Logger, fu
|
|||||||
|
|
||||||
logger := &FileLogger{
|
logger := &FileLogger{
|
||||||
writer: f,
|
writer: f,
|
||||||
|
file: f,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,4 +57,5 @@ func (l *FileLogger) Log(level logging.LogLevel, format string, v ...any) {
|
|||||||
fullFormat := fmt.Sprintf("%-13s %s | %s\n", "["+level+"]", timestamp, format)
|
fullFormat := fmt.Sprintf("%-13s %s | %s\n", "["+level+"]", timestamp, format)
|
||||||
bytes := fmt.Appendf(nil, fullFormat, v...)
|
bytes := fmt.Appendf(nil, fullFormat, v...)
|
||||||
l.writer.Write(bytes)
|
l.writer.Write(bytes)
|
||||||
|
l.file.Sync()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user