-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
137 lines (120 loc) · 3.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"flag"
"instafix/handlers"
scraper "instafix/handlers/scraper"
"instafix/utils"
"instafix/views"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
bolt "go.etcd.io/bbolt"
)
func init() {
// Create static folder if not exists
os.Mkdir("static", 0755)
}
func main() {
listenAddr := flag.String("listen", "0.0.0.0:3000", "Address to listen on")
gridCacheMaxFlag := flag.String("grid-cache-entries", "1024", "Maximum number of grid images to cache")
remoteScraperAddr := flag.String("remote-scraper", "", "Remote scraper address (https://github.com/Wikidepia/InstaFix-remote-scraper)")
videoProxyAddr := flag.String("video-proxy-addr", "", "Video proxy address (https://github.com/Wikidepia/InstaFix-proxy)")
flag.Parse()
// Initialize remote scraper
if *remoteScraperAddr != "" {
if !strings.HasPrefix(*remoteScraperAddr, "http") {
panic("Remote scraper address must start with http:// or https://")
}
scraper.RemoteScraperAddr = *remoteScraperAddr
}
// Initialize video proxy
if *videoProxyAddr != "" {
if !strings.HasPrefix(*videoProxyAddr, "http") {
panic("Video proxy address must start with http:// or https://")
}
handlers.VideoProxyAddr = *videoProxyAddr
if !strings.HasSuffix(handlers.VideoProxyAddr, "/") {
handlers.VideoProxyAddr += "/"
}
}
// Initialize logging
slog.SetLogLoggerLevel(slog.LevelError)
// Initialize LRU
gridCacheMax, err := strconv.Atoi(*gridCacheMaxFlag)
if err != nil || gridCacheMax <= 0 {
panic(err)
}
scraper.InitLRU(gridCacheMax)
// Initialize cache / DB
scraper.InitDB()
defer scraper.DB.Close()
// Evict cache every minute
go func() {
for {
evictCache()
time.Sleep(5 * time.Minute)
}
}()
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(middleware.StripSlashes)
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)
})
println("Starting up on", *listenAddr)
if err := http.ListenAndServe(*listenAddr, r); err != nil {
slog.Error("Failed to listen", "err", err)
}
}
// Remove cache from Pebble if already expired
func evictCache() {
curTime := time.Now().UnixNano()
err := scraper.DB.Batch(func(tx *bolt.Tx) error {
ttlBucket := tx.Bucket([]byte("ttl"))
if ttlBucket == nil {
return nil
}
dataBucket := tx.Bucket([]byte("data"))
if dataBucket == nil {
return nil
}
c := ttlBucket.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if n, err := strconv.ParseInt(utils.B2S(k), 10, 64); err == nil {
if n < curTime {
ttlBucket.Delete(k)
dataBucket.Delete(v)
}
} else {
slog.Error("Failed to parse expire timestamp in cache", "err", err)
}
}
return nil
})
if err != nil {
slog.Error("Failed to evict cache", "err", err)
}
}