Replace Gorilla Mux with net/http router

This commit is contained in:
Paul-Christian Volkmer 2024-10-26 01:06:24 +02:00
parent f6d8bce2d5
commit 3742acf149
4 changed files with 8 additions and 16 deletions

7
go.mod
View File

@ -1,8 +1,5 @@
module idicon module idicon
go 1.20 go 1.22
require ( require github.com/BurntSushi/toml v1.4.0
github.com/BurntSushi/toml v1.4.0
github.com/gorilla/mux v1.8.1
)

2
go.sum
View File

@ -1,4 +1,2 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=

View File

@ -5,7 +5,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/gorilla/mux"
"idicon/icons" "idicon/icons"
"image/gif" "image/gif"
"image/jpeg" "image/jpeg"
@ -26,7 +25,7 @@ func pageRequestHandler(w http.ResponseWriter, _ *http.Request) {
} }
func requestHandler(w http.ResponseWriter, r *http.Request) { func requestHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"] id := r.PathValue("id")
size, err := strconv.Atoi(r.URL.Query().Get("s")) size, err := strconv.Atoi(r.URL.Query().Get("s"))
if err != nil { if err != nil {
@ -135,9 +134,9 @@ func main() {
configure(*configFile) configure(*configFile)
router := mux.NewRouter() router := http.NewServeMux()
router.HandleFunc("/avatar", pageRequestHandler) router.HandleFunc("GET /avatar", pageRequestHandler)
router.HandleFunc("/avatar/{id}", requestHandler) router.HandleFunc("GET /avatar/{id}", requestHandler)
log.Printf("Starting on port %d ...\n", *port) log.Printf("Starting on port %d ...\n", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), router)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), router))
} }

View File

@ -7,8 +7,6 @@ import (
"os" "os"
"reflect" "reflect"
"testing" "testing"
"github.com/gorilla/mux"
) )
//go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_v1.png //go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_v1.png
@ -26,8 +24,8 @@ var gh2 []byte
//go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_s40.png //go:embed testdata/1a79a4d60de6718e8e5b326e338ae533_s40.png
var s40 []byte var s40 []byte
func testRouter() *mux.Router { func testRouter() *http.ServeMux {
router := mux.NewRouter() router := http.NewServeMux()
router.HandleFunc("/avatar/{id}", requestHandler) router.HandleFunc("/avatar/{id}", requestHandler)
return router return router
} }