https://github.com/behnambm/fmthandler
https://github.com/behnambm/fmthandler
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/behnambm/fmthandler
- Owner: behnambm
- License: mit
- Created: 2024-04-23T15:47:52.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-23T16:36:04.000Z (about 1 year ago)
- Last Synced: 2025-01-11T23:47:03.113Z (5 months ago)
- Language: Go
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fmthandler
fmthandler is a tool that enhances Go HTTP server code by automatically adding logging to HTTP HandlerFunc handlers.
It parses the Abstract Syntax Tree (AST) of your code to achieve this.### Features
- Automatically adds logging statements to HTTP HandlerFunc handlers.
- Works seamlessly with both single files and entire directories of Go code.## Installation
```shell
go install github.com/behnambm/fmthandler@latest
```### Code Before
```go
package mainimport (
"log"
"net/http"
)func main() {
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("pong"))
})err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatalln(err)
}}
```
### Running the fmthanlder
```shell
fmthandler --file main.go
```### Code After
```go
package mainimport (
"fmt"
"log"
"net/http"
)func main() {
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Invoking HandlerFunc: '/ping'")
w.WriteHeader(200)
w.Write([]byte("pong"))
})err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatalln(err)
}}
```