Skip to content

Commit

Permalink
Use std mux
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jul 15, 2024
1 parent 48a5d87 commit 221b35a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 61 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/cockroachdb/pebble v1.1.1
github.com/elastic/go-freelru v0.13.0
github.com/julienschmidt/httprouter v1.3.0
github.com/kelindar/binary v1.0.19
github.com/rs/zerolog v1.33.0
github.com/tdewolff/parse/v2 v2.7.15
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kelindar/binary v1.0.19 h1:DNyQCtKjkLhBh9pnP49OWREddLB0Mho+1U/AOt/Qzxw=
github.com/kelindar/binary v1.0.19/go.mod h1:/twdz8gRLNMffx0U4UOgqm1LywPs6nd9YK2TX52MDh8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
36 changes: 13 additions & 23 deletions handlers/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"
"strings"

"github.com/julienschmidt/httprouter"
"github.com/valyala/bytebufferpool"
)

Expand All @@ -27,7 +26,7 @@ func mediaidToCode(mediaID int) string {
return shortCode
}

func Embed(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
func Embed(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
viewsData := &model.ViewsData{}
viewsBuf := bytebufferpool.Get()
Expand All @@ -36,34 +35,25 @@ func Embed(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var err error
var mediaNum int
urlQuery := r.URL.Query()
postID := ps.ByName("postID")
mediaNumParams := ps.ByName("mediaNum")
postID := r.PathValue("postID")
mediaNumParams := r.PathValue("mediaNum")
if mediaNumParams == "" {
imgIndex := urlQuery.Get("img_index")
if imgIndex != "" {
mediaNumParams = imgIndex
}
mediaNum, err = strconv.Atoi(mediaNumParams)
if err != nil {
viewsData.Description = "Invalid img_index parameter"
views.Embed(viewsData, w)
return
if mediaNumParams != "" {
mediaNum, err = strconv.Atoi(mediaNumParams)
if err != nil {
viewsData.Description = "Invalid img_index parameter"
views.Embed(viewsData, w)
return
}
}
}

direct, err := strconv.ParseBool(urlQuery.Get("direct"))
if err != nil {
viewsData.Description = "Invalid direct parameter"
views.Embed(viewsData, w)
return
}

isGallery, err := strconv.ParseBool(urlQuery.Get("gallery"))
if err != nil {
viewsData.Description = "Invalid gallery parameter"
views.Embed(viewsData, w)
return
}
isDirect, _ := strconv.ParseBool(urlQuery.Get("direct"))
isGallery, _ := strconv.ParseBool(urlQuery.Get("gallery"))

// Stories use mediaID (int) instead of postID
if strings.Contains(r.URL.Path, "/stories/") {
Expand Down Expand Up @@ -138,7 +128,7 @@ func Embed(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
viewsData.OEmbedURL = r.Host + "/oembed?text=" + url.QueryEscape(viewsData.Description) + "&url=" + viewsData.URL
}

if direct {
if isDirect {
w.Header().Set("Location", sb.String())
return
}
Expand Down
5 changes: 2 additions & 3 deletions handlers/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/RyanCarrier/dijkstra/v2"
"github.com/julienschmidt/httprouter"
"github.com/rs/zerolog/log"
"golang.org/x/image/draw"
)
Expand Down Expand Up @@ -115,8 +114,8 @@ func GenerateGrid(images []image.Image) (image.Image, error) {
return canvas, nil
}

func Grid(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
postID := ps.ByName("postID")
func Grid(w http.ResponseWriter, r *http.Request) {
postID := r.PathValue("postID")
gridFname := filepath.Join("static", postID+".jpeg")

// If already exists, return
Expand Down
8 changes: 3 additions & 5 deletions handlers/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
scraper "instafix/handlers/scraper"
"net/http"
"strconv"

"github.com/julienschmidt/httprouter"
)

func Images(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
postID := ps.ByName("postID")
mediaNum, err := strconv.Atoi(ps.ByName("mediaNum"))
func Images(w http.ResponseWriter, r *http.Request) {
postID := r.PathValue("postID")
mediaNum, err := strconv.Atoi(r.PathValue("mediaNum"))
if err != nil {
return
}
Expand Down
3 changes: 1 addition & 2 deletions handlers/oembed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"net/http"

"github.com/PurpleSec/escape"
"github.com/julienschmidt/httprouter"
)

func OEmbed(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
func OEmbed(w http.ResponseWriter, r *http.Request) {
headingText := r.URL.Query().Get("text")
headingURL := r.URL.Query().Get("url")
if headingText == "" || headingURL == "" {
Expand Down
8 changes: 3 additions & 5 deletions handlers/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"net/http"
"strconv"
"strings"

"github.com/julienschmidt/httprouter"
)

func Videos(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
postID := ps.ByName("postID")
mediaNum, err := strconv.Atoi(ps.ByName("mediaNum"))
func Videos(w http.ResponseWriter, r *http.Request) {
postID := r.PathValue("postID")
mediaNum, err := strconv.Atoi(r.PathValue("mediaNum"))
if err != nil {
return
}
Expand Down
38 changes: 18 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/cockroachdb/pebble"
"github.com/julienschmidt/httprouter"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -61,28 +60,27 @@ func main() {
// Close database when app closes
// defer scraper.DB.Close()

router := httprouter.New()
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
mux := http.NewServeMux()
// mux.HandleFunc("GET /{username}/p/{postID}", handlers.Embed)
// mux.HandleFunc("GET /{username}/p/{postID}/{mediaNum}", handlers.Embed)
// mux.HandleFunc("GET /{username}/reel/{postID}", handlers.Embed)

mux.HandleFunc("GET /tv/{postID}", handlers.Embed)
mux.HandleFunc("GET /reel/{postID}", handlers.Embed)
mux.HandleFunc("GET /reels/{postID}", handlers.Embed)
mux.HandleFunc("GET /stories/{username}/{postID}", handlers.Embed)
mux.HandleFunc("GET /p/{postID}", handlers.Embed)
mux.HandleFunc("GET /p/{postID}/{mediaNum}", handlers.Embed)

mux.HandleFunc("/images/{postID}/{mediaNum}", handlers.Images)
mux.HandleFunc("/videos/{postID}/{mediaNum}", handlers.Videos)
mux.HandleFunc("/grid/{postID}", handlers.Grid)
mux.HandleFunc("/oembed", handlers.OEmbed)
mux.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
views.Home(w)
})
// router.GET("/:username/p/:postID", handlers.Embed)
// router.GET("/:username/p/:postID/:mediaNum", handlers.Embed)
// router.GET("/:username/reel/:postID", handlers.Embed)

router.GET("/p/:postID", handlers.Embed)
router.GET("/tv/:postID", handlers.Embed)
router.GET("/reel/:postID", handlers.Embed)
router.GET("/reels/:postID", handlers.Embed)
router.GET("/stories/:username/:postID", handlers.Embed)
router.GET("/p/:postID/:mediaNum", handlers.Embed)

router.GET("/images/:postID/:mediaNum", handlers.Images)
router.GET("/videos/:postID/:mediaNum", handlers.Videos)
router.GET("/grid/:postID", handlers.Grid)
router.GET("/oembed", handlers.OEmbed)

if err := http.ListenAndServe(*listenAddr, router); err != nil {
if err := http.ListenAndServe(*listenAddr, mux); err != nil {
log.Fatal().Err(err).Msg("Failed to listen")
}
}
Expand Down

0 comments on commit 221b35a

Please sign in to comment.