Skip to content

Commit

Permalink
chi
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jul 17, 2024
1 parent 369ab1e commit 7e8bf4f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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/go-chi/chi/v5 v5.1.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: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/elastic/go-freelru v0.13.0 h1:TKKY6yCfNNNky7Pj9xZAOEpBcdNgZJfihEftOb5
github.com/elastic/go-freelru v0.13.0/go.mod h1:bSdWT4M0lW79K8QbX6XY2heQYSCqD7THoYf82pT/H3I=
github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k=
github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down
15 changes: 8 additions & 7 deletions handlers/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/url"
"strconv"
"strings"

"github.com/go-chi/chi/v5"
)

func mediaidToCode(mediaID int) string {
Expand All @@ -30,11 +32,10 @@ func Embed(w http.ResponseWriter, r *http.Request) {

var err error
var mediaNum int
urlQuery := r.URL.Query()
postID := r.PathValue("postID")
mediaNumParams := r.PathValue("mediaNum")
postID := chi.URLParam(r, "postID")
mediaNumParams := chi.URLParam(r, "mediaNum")
if mediaNumParams == "" {
imgIndex := urlQuery.Get("img_index")
imgIndex := r.URL.Query().Get("img_index")
if imgIndex != "" {
mediaNumParams = imgIndex
}
Expand All @@ -48,8 +49,8 @@ func Embed(w http.ResponseWriter, r *http.Request) {
}
}

isDirect, _ := strconv.ParseBool(urlQuery.Get("direct"))
isGallery, _ := strconv.ParseBool(urlQuery.Get("gallery"))
isDirect, _ := strconv.ParseBool(r.URL.Query().Get("direct"))
isGallery, _ := strconv.ParseBool(r.URL.Query().Get("gallery"))

// Stories use mediaID (int) instead of postID
if strings.Contains(r.URL.Path, "/stories/") {
Expand All @@ -64,7 +65,7 @@ func Embed(w http.ResponseWriter, r *http.Request) {

// If User-Agent is not bot, redirect to Instagram
viewsData.Title = "InstaFix"
viewsData.URL = "https://instagram.com" + r.URL.Path
viewsData.URL = "https://instagram.com/p/" + r.URL.RequestURI()
if !utils.IsBot(r.Header.Get("User-Agent")) {
http.Redirect(w, r, viewsData.URL, http.StatusFound)
return
Expand Down
3 changes: 2 additions & 1 deletion handlers/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/RyanCarrier/dijkstra/v2"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
"golang.org/x/image/draw"
"golang.org/x/sync/singleflight"
Expand Down Expand Up @@ -117,7 +118,7 @@ func GenerateGrid(images []image.Image) (image.Image, error) {
}

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

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

"github.com/go-chi/chi/v5"
)

func Images(w http.ResponseWriter, r *http.Request) {
postID := r.PathValue("postID")
mediaNum, err := strconv.Atoi(r.PathValue("mediaNum"))
postID := chi.URLParam(r, "postID")
mediaNum, err := strconv.Atoi(chi.URLParam(r, "mediaNum"))
if err != nil {
return
}
Expand Down
6 changes: 4 additions & 2 deletions handlers/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"net/http"
"strconv"
"strings"

"github.com/go-chi/chi/v5"
)

func Videos(w http.ResponseWriter, r *http.Request) {
postID := r.PathValue("postID")
mediaNum, err := strconv.Atoi(r.PathValue("mediaNum"))
postID := chi.URLParam(r, "postID")
mediaNum, err := strconv.Atoi(chi.URLParam(r, "mediaNum"))
if err != nil {
return
}
Expand Down
41 changes: 23 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"time"

"github.com/cockroachdb/pebble"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -65,27 +67,30 @@ func main() {
http.ListenAndServe("0.0.0.0:6060", nil)
}()

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) {
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Mount("/debug", middleware.Profiler())

r.Get("/tv/{postID}/", handlers.Embed)
r.Get("/reel/{postID}/", handlers.Embed)
r.Get("/reels/{postID}/", handlers.Embed)
r.Get("/stories/{username}/{postID}/", handlers.Embed)
r.Get("/p/{postID}/", handlers.Embed)
r.Get("/p/{postID}/{mediaNum}/", handlers.Embed)

r.Get("/{username}/p/{postID}", handlers.Embed)
r.Get("/{username}/p/{postID}/{mediaNum}", handlers.Embed)
r.Get("/{username}/reel/{postID}", handlers.Embed)

r.Get("/images/{postID}/{mediaNum}/", handlers.Images)
r.Get("/videos/{postID}/{mediaNum}/", handlers.Videos)
r.Get("/grid/{postID}/", handlers.Grid)
r.Get("/oembed/", handlers.OEmbed)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
views.Home(w)
})
if err := http.ListenAndServe(*listenAddr, mux); err != nil {
if err := http.ListenAndServe(*listenAddr, r); err != nil {
log.Fatal().Err(err).Msg("Failed to listen")
}
}
Expand Down

0 comments on commit 7e8bf4f

Please sign in to comment.