mirror of
https://github.com/taigrr/gico.git
synced 2026-04-16 02:00:59 -07:00
refactor(server): migrate from gorilla/mux to stdlib net/http
Replace gorilla/mux and gorilla/handlers with Go 1.22+ stdlib ServeMux method patterns. Fix panic-on-error to return proper HTTP error responses. Fix Content-Type from text/html to image/svg+xml for SVG endpoints. Remove gorilla dependencies from go.mod.
This commit is contained in:
@@ -2,71 +2,72 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/taigrr/gico/commits"
|
||||
"github.com/taigrr/gico/graph/svg"
|
||||
)
|
||||
|
||||
type DayCount [366]int
|
||||
|
||||
func main() {
|
||||
r := mux.NewRouter()
|
||||
logger := func(h http.Handler) http.Handler {
|
||||
return handlers.LoggingHandler(os.Stdout, h)
|
||||
}
|
||||
r.Use(mux.MiddlewareFunc(logger))
|
||||
r.HandleFunc("/weekly.svg", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("GET /weekly.svg", func(w http.ResponseWriter, r *http.Request) {
|
||||
author := r.URL.Query().Get("author")
|
||||
highlight := r.URL.Query().Get("highlight")
|
||||
shouldHighlight := highlight != ""
|
||||
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
repoPaths, err := commits.GetRepos()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, "failed to get repos", http.StatusInternalServerError)
|
||||
log.Printf("error getting repos: %v", err)
|
||||
return
|
||||
}
|
||||
week, err := repoPaths.GetWeekFreq([]string{author})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, "failed to get weekly frequency", http.StatusInternalServerError)
|
||||
log.Printf("error getting weekly freq: %v", err)
|
||||
return
|
||||
}
|
||||
svg := svg.GetWeekSVG(week, shouldHighlight)
|
||||
svg.WriteTo(w)
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
svgData := svg.GetWeekSVG(week, shouldHighlight)
|
||||
svgData.WriteTo(w)
|
||||
})
|
||||
r.HandleFunc("/stats.json", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
mux.HandleFunc("GET /stats.json", func(w http.ResponseWriter, r *http.Request) {
|
||||
year := time.Now().Year()
|
||||
yst := r.URL.Query().Get("year")
|
||||
author := r.URL.Query().Get("author")
|
||||
y, err := strconv.Atoi(yst)
|
||||
if err == nil {
|
||||
if y, err := strconv.Atoi(yst); err == nil {
|
||||
year = y
|
||||
}
|
||||
repoPaths, err := commits.GetRepos()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, "failed to get repos", http.StatusInternalServerError)
|
||||
log.Printf("error getting repos: %v", err)
|
||||
return
|
||||
}
|
||||
freq, err := repoPaths.FrequencyChan(year, []string{author})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, "failed to get frequency", http.StatusInternalServerError)
|
||||
log.Printf("error getting freq: %v", err)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(freq); err != nil {
|
||||
log.Printf("error encoding response: %v", err)
|
||||
}
|
||||
b, _ := json.Marshal(freq)
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
})
|
||||
r.HandleFunc("/yearly.svg", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
mux.HandleFunc("GET /yearly.svg", func(w http.ResponseWriter, r *http.Request) {
|
||||
year := time.Now().Year()
|
||||
yst := r.URL.Query().Get("year")
|
||||
author := r.URL.Query().Get("author")
|
||||
highlight := r.URL.Query().Get("highlight")
|
||||
shouldHighlight := highlight != ""
|
||||
y, err := strconv.Atoi(yst)
|
||||
if err == nil {
|
||||
if y, err := strconv.Atoi(yst); err == nil {
|
||||
if year != y {
|
||||
shouldHighlight = false
|
||||
}
|
||||
@@ -74,19 +75,23 @@ func main() {
|
||||
}
|
||||
repoPaths, err := commits.GetRepos()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, "failed to get repos", http.StatusInternalServerError)
|
||||
log.Printf("error getting repos: %v", err)
|
||||
return
|
||||
}
|
||||
freq, err := repoPaths.FrequencyChan(year, []string{author})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, "failed to get frequency", http.StatusInternalServerError)
|
||||
log.Printf("error getting freq: %v", err)
|
||||
return
|
||||
}
|
||||
svg := svg.GetYearSVG(freq, shouldHighlight)
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
svg.WriteTo(w)
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
svgData := svg.GetYearSVG(freq, shouldHighlight)
|
||||
svgData.WriteTo(w)
|
||||
})
|
||||
|
||||
err := http.ListenAndServe(":8822", r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Println("gico-server listening on :8822")
|
||||
if err := http.ListenAndServe(":8822", mux); err != nil {
|
||||
log.Fatalf("server error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
3
go.mod
3
go.mod
@@ -8,8 +8,6 @@ require (
|
||||
github.com/charmbracelet/bubbletea v1.3.10
|
||||
github.com/charmbracelet/lipgloss v1.1.0
|
||||
github.com/go-git/go-git/v5 v5.16.5
|
||||
github.com/gorilla/handlers v1.5.2
|
||||
github.com/gorilla/mux v1.8.1
|
||||
github.com/muesli/termenv v0.16.0
|
||||
github.com/taigrr/mg v0.1.1
|
||||
github.com/taigrr/simplecolorpalettes v0.9.8
|
||||
@@ -31,7 +29,6 @@ require (
|
||||
github.com/cyphar/filepath-securejoin v0.6.1 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||
github.com/go-git/go-billy/v5 v5.7.0 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
|
||||
|
||||
6
go.sum
6
go.sum
@@ -53,8 +53,6 @@ github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
||||
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
|
||||
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
|
||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
|
||||
@@ -69,10 +67,6 @@ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8J
|
||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
|
||||
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||
github.com/kevinburke/ssh_config v1.6.0 h1:J1FBfmuVosPHf5GRdltRLhPJtJpTlMdKTBjRgTaQBFY=
|
||||
|
||||
Reference in New Issue
Block a user