From 06386d2b18ccec615803c3517d164ee595175982 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 4 Sep 2021 20:02:08 +0200 Subject: [PATCH] refactor: rewrite with gofiber close #7 --- go.mod | 11 +++++++-- go.sum | 25 +++++++++++++++++++ server.go | 74 +++++++++++++++++++------------------------------------ 3 files changed, 59 insertions(+), 51 deletions(-) diff --git a/go.mod b/go.mod index 72495fb..3a1b769 100644 --- a/go.mod +++ b/go.mod @@ -4,18 +4,25 @@ go 1.17 require ( github.com/PuerkitoBio/goquery v1.5.0 // indirect + github.com/andybalholm/brotli v1.0.2 // indirect github.com/andybalholm/cascadia v1.0.0 // indirect github.com/antchfx/htmlquery v1.0.0 // indirect github.com/antchfx/xmlquery v1.0.0 // indirect github.com/antchfx/xpath v1.0.0 // indirect github.com/gobwas/glob v0.2.3 // indirect github.com/gocolly/colly v1.2.0 // indirect + github.com/gofiber/fiber/v2 v2.18.0 // indirect github.com/golang/protobuf v1.3.1 // indirect github.com/kennygrant/sanitize v1.2.4 // indirect + github.com/klauspost/compress v1.13.4 // indirect github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect github.com/temoto/robotstxt v1.1.1 // indirect github.com/undertideco/bandcamp v0.1.4-0.20191020172239-8f78bd1bb2b3 // indirect - golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect - golang.org/x/text v0.3.2 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.29.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect + golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect + golang.org/x/text v0.3.6 // indirect google.golang.org/appengine v1.6.2 // indirect ) diff --git a/go.sum b/go.sum index 8ba1a9a..af43c87 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk= github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg= +github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E= +github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/antchfx/htmlquery v1.0.0 h1:O5IXz8fZF3B3MW+B33MZWbTHBlYmcfw0BAxgErHuaMA= @@ -13,10 +15,15 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/gocolly/colly v1.2.0 h1:qRz9YAn8FIH0qzgNUw+HT9UN7wm1oF9OBAilwEWpyrI= github.com/gocolly/colly v1.2.0/go.mod h1:Hof5T3ZswNVsOHYmba1u03W65HDWgpV5HifSuueE0EA= +github.com/gofiber/fiber/v2 v2.18.0 h1:xCWYSVoTNibHpzfciPwUSZGiTyTpTXYchCwynuJU09s= +github.com/gofiber/fiber/v2 v2.18.0/go.mod h1:/LdZHMUXZvTTo7gU4+b1hclqCAdoQphNQ9bi9gutPyI= github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o= github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak= +github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= +github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca h1:NugYot0LIVPxTvN8n+Kvkn6TrbMyxQiuvKdEwFdR9vI= github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU= @@ -26,8 +33,15 @@ github.com/temoto/robotstxt v1.1.1 h1:Gh8RCs8ouX3hRSxxK7B1mO5RFByQ4CmJZDwgom++Ja github.com/temoto/robotstxt v1.1.1/go.mod h1:+1AmkuG3IYkh1kv0d2qEB9Le88ehNO0zwOr3ujewlOo= github.com/undertideco/bandcamp v0.1.4-0.20191020172239-8f78bd1bb2b3 h1:ZpiPMsJf38QHC6RgsfzgqV0f7xqUT6WVciHF4a2M6Co= github.com/undertideco/bandcamp v0.1.4-0.20191020172239-8f78bd1bb2b3/go.mod h1:0mHaN/OborGbjcdhqDDtLX27IDrl4iVJ7Srqx/rpyGY= +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/fasthttp v1.29.0 h1:F5GKpytwFk5OhCuRh6H+d4vZAcEeNAwPTdwQnm6IERY= +github.com/valyala/fasthttp v1.29.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -35,13 +49,24 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM= golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/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-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I= +golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= google.golang.org/appengine v1.6.2 h1:j8RI1yW0SkI+paT6uGwMlrMI/6zwYA6/CFil8rxOzGI= diff --git a/server.go b/server.go index b65089d..fad330b 100644 --- a/server.go +++ b/server.go @@ -9,6 +9,7 @@ import ( "strconv" "net/http" "encoding/json" + "github.com/gofiber/fiber/v2" "github.com/undertideco/bandcamp" ) @@ -16,14 +17,9 @@ var MyClient = &http.Client{} var MyResp = &RespBandcamp{} var SpotifyAPI = &TokenUser{} -func loginSpotify(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - fmt.Fprintf(w, "ParseForm() err: %v", err) - return - } - - w.Header().Set("Location", "https://accounts.spotify.com/authorize?client_id="+ClientID+"&response_type=token&redirect_uri="+RedirectURI) - w.WriteHeader(http.StatusSeeOther) +func loginSpotify(c *fiber.Ctx) error { + c.Set("Location", "https://accounts.spotify.com/authorize?client_id="+ClientID+"&response_type=token&redirect_uri="+RedirectURI) + return c.SendStatus(303) } /* @@ -154,59 +150,39 @@ func getListPlaylist(id string) { fmt.Printf("\nFinish\n") } -func formHandler(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - fmt.Fprintf(w, "ParseForm() err: %v", err) - return - } - - w.Header().Set("Location", "/feudecamp.html") - w.WriteHeader(http.StatusSeeOther) - go getListPlaylist(r.FormValue("id")) +func formHandler (c *fiber.Ctx) error { + c.Set("Location", "/feudecamp.html") + go getListPlaylist(c.FormValue("id")) + return c.SendStatus(303) } -func hello(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/hello" { - http.Error(w, "404", http.StatusNotFound) - return - } - - if r.Method != "GET" { - http.Error(w, "Use GET pls", http.StatusNotFound) - return - } - - fmt.Fprintf(w, "Helo!") +func hello(c *fiber.Ctx) error { + return c.SendString("Helo!") } -func getNew(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - json.NewEncoder(w).Encode(MyResp) +func getNew(c *fiber.Ctx) error { + c.JSON(MyResp) MyResp.Albums = nil MyResp.Artists = nil MyResp.Notfound = nil + return c.SendStatus(201) } -func mytoken(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() - err := json.NewDecoder(r.Body).Decode(&SpotifyAPI) - if err != nil { - fmt.Printf("error:", err) - } +func mytoken(c *fiber.Ctx) error { + return c.BodyParser(&SpotifyAPI) } func main() { - fileServer := http.FileServer(http.Dir("./static")) - http.Handle("/", fileServer) - http.HandleFunc("/hello", hello) - http.HandleFunc("/back", formHandler) - http.HandleFunc("/refresh", getNew) - http.HandleFunc("/spotify", loginSpotify) - http.HandleFunc("/mytoken", mytoken) + app := fiber.New() + app.Static("/", "./static") + + app.Get("/hello", hello) + app.Get("/refresh", getNew) + app.Get("/spotify", loginSpotify) + + app.Post("/back", formHandler) + app.Post("/mytoken", mytoken) fmt.Printf("Starting the server…\n") - if err := http.ListenAndServe(":8080", nil); err != nil { - log.Fatal(err) - } + app.Listen(":8080") }