setup the project structure
This commit is contained in:
parent
279efc8817
commit
ed3854f542
10 changed files with 257 additions and 80 deletions
0
.env.example
Normal file
0
.env.example
Normal file
0
Makefile
Normal file
0
Makefile
Normal file
51
cmd/server/main.go
Normal file
51
cmd/server/main.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.eveutil.org/fabric/everate/internal/logger"
|
||||
"git.eveutil.org/fabric/everate/internal/middleware"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1. Load config
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("Warning: .env file not found")
|
||||
}
|
||||
|
||||
// 2. Create the logger instance
|
||||
isDev := os.Getenv("DEV") == "true"
|
||||
appLogger, err := logger.New(isDev)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create logger: %v", err)
|
||||
}
|
||||
defer appLogger.Sync() // This is still important!
|
||||
|
||||
// 3. Create Echo instance
|
||||
e := echo.New()
|
||||
|
||||
// 4. Register your custom Zap logger middleware
|
||||
// This replaces `e.Use(echomiddleware.Logger())`
|
||||
e.Use(middleware.ZapRequestLogger(appLogger))
|
||||
|
||||
// 5. Register other middleware
|
||||
sessionSecret := os.Getenv("SESSION_SECRET")
|
||||
e.Use(session.Middleware(sessions.NewCookieStore([]byte(sessionSecret))))
|
||||
|
||||
// 6. Routes
|
||||
|
||||
|
||||
// Start the server
|
||||
appLogger.Info("Starting the server...", zap.String("address", ":1323"))
|
||||
if err := e.Start(":1323"); err != nil {
|
||||
appLogger.Fatal("Server failed to start", zap.Error(err))
|
||||
|
||||
}
|
||||
}
|
76
doc/project_structure.md
Normal file
76
doc/project_structure.md
Normal file
|
@ -0,0 +1,76 @@
|
|||
everate/
|
||||
├── cmd/
|
||||
│ ├── server/
|
||||
│ │ └── main.go # Entry point for the web server
|
||||
│ └── wallet-worker/
|
||||
│ └── main.go # Entry point for the background ISK wallet worker
|
||||
│
|
||||
├── internal/
|
||||
│ ├── auth/ # EVE SSO logic, session management
|
||||
│ │ └── sso.go
|
||||
│ │
|
||||
│ ├── config/ # Configuration loading from environment variables
|
||||
│ │ └── config.go
|
||||
│ │
|
||||
│ ├── datastore/ # Database interaction layer (interfaces + implementation)
|
||||
│ │ ├── datastore.go # Defines interfaces (e.g., UserStore, RatingStore)
|
||||
│ │ └── mongodb/ # MongoDB implementation of the datastore interfaces
|
||||
│ │ ├── user.go
|
||||
│ │ ├── rating.go
|
||||
│ │ ├── report.go
|
||||
│ │ └── db.go # Connection logic
|
||||
│ │
|
||||
│ ├── handler/ # HTTP handlers (the "Controllers" in MVC)
|
||||
│ │ ├── handler.go # Base handler struct with dependencies (logger, services)
|
||||
│ │ ├── auth.go # SSO login/callback handlers
|
||||
│ │ ├── entity.go # Character/Corp/Alliance page handlers
|
||||
│ │ ├── rating.go # Comment posting, voting, reporting handlers
|
||||
│ │ ├── draft.go # Draft saving/loading handlers
|
||||
│ │ ├── search.go # Autocomplete search handler
|
||||
│ │ └── admin/ # Sub-package for admin panel handlers
|
||||
│ │ └── admin_handler.go
|
||||
│ │
|
||||
│ ├── logger/ # Zap logger initialization
|
||||
│ │ └── logger.go # Defines New() constructor for zap.Logger
|
||||
│ │
|
||||
│ ├── middleware/ # Custom Echo middleware
|
||||
│ │ ├── auth.go # Check for login, admin roles, ban status
|
||||
│ │ ├── request_logger.go # Custom Zap request logger
|
||||
│ │ └── ratelimit.go # Rate limiting middleware
|
||||
│ │
|
||||
│ ├── model/ # Core application data structures (User, Rating, etc.)
|
||||
│ │ ├── user.go
|
||||
│ │ ├── entity.go
|
||||
│ │ └── ...
|
||||
│ │
|
||||
│ ├── server/ # Echo server setup and routing
|
||||
│ │ └── server.go # NewServer(), setupRoutes(), Start()
|
||||
│ │
|
||||
│ ├── service/ # Core business logic (decoupled from web layer)
|
||||
│ │ ├── rating_service.go # Logic for calculating costs, updating scores
|
||||
│ │ └── wallet_service.go # Logic for processing ESI journal transactions
|
||||
│ │
|
||||
│ └── view/ # HTML template management
|
||||
│ ├── view.go # Template parsing and rendering engine
|
||||
│ ├── layout/ # Base layouts (*.html)
|
||||
│ ├── page/ # Full page templates (*.html)
|
||||
│ └── component/ # Reusable htmx partials (*.html)
|
||||
│
|
||||
├── pkg/
|
||||
│ └── esi/ # Reusable EVE Swagger Interface client wrapper
|
||||
│ ├── client.go # The ESI client with caching
|
||||
│ └── models.go # Structs for ESI API responses
|
||||
│
|
||||
├── web/
|
||||
│ └── static/ # Static assets (CSS, JS, images)
|
||||
│ ├── css/
|
||||
│ │ └── style.css
|
||||
│ └── js/
|
||||
│ ├── main.js
|
||||
│ └── editor.js
|
||||
│
|
||||
├── .env.example # Example environment variables for local development
|
||||
├── .gitignore
|
||||
├── go.mod
|
||||
├── go.sum
|
||||
└── Makefile # For common dev tasks (build, run, test)
|
22
go.mod
22
go.mod
|
@ -3,25 +3,25 @@ module git.eveutil.org/fabric/everate
|
|||
go 1.24.4
|
||||
|
||||
require (
|
||||
github.com/golang/snappy v1.0.0 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/klauspost/compress v1.16.7 // indirect
|
||||
github.com/labstack/echo/v4 v4.13.4 // indirect
|
||||
github.com/gorilla/sessions v1.4.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/labstack/echo-contrib v0.17.4
|
||||
github.com/labstack/echo/v4 v4.13.4
|
||||
go.uber.org/zap v1.27.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gorilla/context v1.1.2 // indirect
|
||||
github.com/gorilla/securecookie v1.1.2 // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.2.2 // indirect
|
||||
go.uber.org/multierr v1.10.0 // indirect
|
||||
go.uber.org/zap v1.27.0 // indirect
|
||||
golang.org/x/crypto v0.38.0 // indirect
|
||||
golang.org/x/net v0.40.0 // indirect
|
||||
golang.org/x/sync v0.14.0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
golang.org/x/text v0.25.0 // indirect
|
||||
golang.org/x/time v0.11.0 // indirect
|
||||
)
|
||||
|
|
62
go.sum
62
go.sum
|
@ -1,9 +1,17 @@
|
|||
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
|
||||
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o=
|
||||
github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM=
|
||||
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
|
||||
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
|
||||
github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ=
|
||||
github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
|
||||
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/labstack/echo-contrib v0.17.4 h1:g5mfsrJfJTKv+F5uNKCyrjLK7js+ZW6HTjg4FnDxxgk=
|
||||
github.com/labstack/echo-contrib v0.17.4/go.mod h1:9O7ZPAHUeMGTOAfg80YqQduHzt0CzLak36PZRldYrZ0=
|
||||
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
|
||||
github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ=
|
||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||
|
@ -12,56 +20,30 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
|
|||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.mongodb.org/mongo-driver/v2 v2.2.2 h1:9cYuS3fl1Xhqwpfazso10V7BHQD58kCgtzhfAmJYz9c=
|
||||
go.mongodb.org/mongo-driver/v2 v2.2.2/go.mod h1:qQkDMhCGWl3FN509DfdPd4GRBLU/41zqF/k8eTRceps=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
||||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
|
||||
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
|
||||
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
|
||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
|
||||
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
|
||||
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
46
internal/logger/logger.go
Normal file
46
internal/logger/logger.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Package logger provides a configurable constructor for the application's zap logger.
|
||||
// It supports distinct configurations for development and production environments.
|
||||
package logger
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// New creates a new zap.Logger tailored for either a development or production environment.
|
||||
//
|
||||
// In development (isDev = true), it returns a human-readable, colored, debug-level logger.
|
||||
//
|
||||
// In production (isDev = false), it returns a structured, JSON-formatted, info-level logger
|
||||
// that is optimized for performance and machine parsing.
|
||||
func New(isDev bool) (*zap.Logger, error) {
|
||||
var config zap.Config
|
||||
var err error
|
||||
|
||||
if isDev {
|
||||
// Development configuration:
|
||||
// - Human-readable, console-friendly output
|
||||
// - Logs at Debug level and above
|
||||
// - Includes caller's file and line number
|
||||
// - Adds color to log levels for easy scanning
|
||||
config = zap.NewDevelopmentConfig()
|
||||
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
} else {
|
||||
// Production configuration:
|
||||
// - Structured JSON output for machine parsing
|
||||
// - Logs at Info level and above
|
||||
// - Highly performant
|
||||
// - Uses ISO8601 timestamp format for consistency across services
|
||||
config = zap.NewProductionConfig()
|
||||
config.EncoderConfig.TimeKey = "timestamp"
|
||||
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
}
|
||||
|
||||
// Build the logger from the selected configuration.
|
||||
logger, err := config.Build()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return logger, nil
|
||||
}
|
51
internal/middleware/request_logger.go
Normal file
51
internal/middleware/request_logger.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ZapRequestLogger is a middleware that logs incoming HTTP requests using a zap.Logger.
|
||||
// It logs the method, URI, status, latency, remote IP, and response size.
|
||||
func ZapRequestLogger(log *zap.Logger) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
start := time.Now()
|
||||
|
||||
// Run the next handler in the chain
|
||||
err := next(c)
|
||||
if err != nil {
|
||||
// Let Echo's central error handler deal with it
|
||||
c.Error(err)
|
||||
}
|
||||
|
||||
// After the handler has run, we can log the details
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
stop := time.Now()
|
||||
|
||||
// These are our structured logging fields
|
||||
fields := []zap.Field{
|
||||
zap.String("method", req.Method),
|
||||
zap.String("uri", req.RequestURI),
|
||||
zap.Int("status", res.Status),
|
||||
zap.String("remote_ip", c.RealIP()),
|
||||
zap.Duration("latency", stop.Sub(start)),
|
||||
zap.Int64("size", res.Size),
|
||||
}
|
||||
|
||||
// Differentiate between server errors and client errors
|
||||
if res.Status >= 500 {
|
||||
log.Error("Server Error", fields...)
|
||||
} else if res.Status >= 400 {
|
||||
log.Warn("Client Error", fields...)
|
||||
} else {
|
||||
log.Info("Request Handled", fields...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Sugar is a global instance of the zap sugared logger.
|
||||
var Sugar *zap.SugaredLogger
|
||||
|
||||
// Init initializes the global logger and returns a cleanup function.
|
||||
// The cleanup function should be deferred in the main function.
|
||||
func Init() func() {
|
||||
// NewProduction builds a sensible production logger that writes InfoLevel and
|
||||
// above logs to standard error.
|
||||
coreLogger, err := zap.NewDevelopment()
|
||||
if err != nil {
|
||||
log.Fatalf("can't initialize zap logger: %v", err)
|
||||
}
|
||||
|
||||
// Assign the sugared logger to our global variable.
|
||||
Sugar = coreLogger.Sugar()
|
||||
|
||||
// Return the Sync function to be deferred by the caller.
|
||||
return func() {
|
||||
_ = coreLogger.Sync()
|
||||
}
|
||||
}
|
0
web/static/css/style.css
Normal file
0
web/static/css/style.css
Normal file
Loading…
Add table
Add a link
Reference in a new issue