2017-11-03 00:15:13 +01:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2018-04-16 17:40:54 +02:00
|
|
|
|
"encoding/hex"
|
2018-04-25 22:33:50 +02:00
|
|
|
|
"fmt"
|
2017-11-03 00:15:13 +01:00
|
|
|
|
"html/template"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2018-04-25 22:33:50 +02:00
|
|
|
|
"strings"
|
2018-06-19 17:49:46 +02:00
|
|
|
|
"time"
|
2017-11-03 00:15:13 +01:00
|
|
|
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-06-19 17:49:46 +02:00
|
|
|
|
"github.com/gorilla/schema"
|
2017-11-03 00:15:13 +01:00
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
|
2018-04-16 17:40:54 +02:00
|
|
|
|
"github.com/boramalper/magnetico/pkg/persistence"
|
2017-11-03 00:15:13 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const N_TORRENTS = 20
|
|
|
|
|
|
2018-06-19 17:49:46 +02:00
|
|
|
|
// Set a Decoder instance as a package global, because it caches
|
|
|
|
|
// meta-data about structs, and an instance can be shared safely.
|
|
|
|
|
var decoder = schema.NewDecoder()
|
|
|
|
|
|
2017-11-03 00:15:13 +01:00
|
|
|
|
var templates map[string]*template.Template
|
|
|
|
|
var database persistence.Database
|
|
|
|
|
|
2018-06-19 17:49:46 +02:00
|
|
|
|
// ======= Q: Query =======
|
|
|
|
|
type TorrentsQ struct {
|
|
|
|
|
Epoch *int64 `schema:"epoch"`
|
|
|
|
|
Query *string `schema:"query"`
|
|
|
|
|
OrderBy *string `schema:"orderBy"`
|
|
|
|
|
Ascending *bool `schema:"ascending"`
|
|
|
|
|
LastOrderedValue *float64 `schema:"lastOrderedValue"`
|
|
|
|
|
LastID *uint64 `schema:"lastID"`
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 00:15:13 +01:00
|
|
|
|
// ========= TD: TemplateData =========
|
|
|
|
|
type HomepageTD struct {
|
2018-04-25 22:33:50 +02:00
|
|
|
|
NTorrents uint
|
2017-11-03 00:15:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TorrentsTD struct {
|
2018-04-25 22:33:50 +02:00
|
|
|
|
CanLoadMore bool
|
|
|
|
|
Query string
|
|
|
|
|
SubscriptionURL string
|
|
|
|
|
Torrents []persistence.TorrentMetadata
|
|
|
|
|
SortedBy string
|
|
|
|
|
NextPageExists bool
|
|
|
|
|
Epoch int64
|
2018-06-19 17:49:46 +02:00
|
|
|
|
LastOrderedValue float64
|
2018-04-25 22:33:50 +02:00
|
|
|
|
LastID uint64
|
|
|
|
|
|
2017-11-03 00:15:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TorrentTD struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type FeedTD struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StatisticsTD struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
loggerLevel := zap.NewAtomicLevel()
|
|
|
|
|
// Logging levels: ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal").
|
|
|
|
|
logger := zap.New(zapcore.NewCore(
|
|
|
|
|
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
|
|
|
|
|
zapcore.Lock(os.Stderr),
|
|
|
|
|
loggerLevel,
|
|
|
|
|
))
|
|
|
|
|
defer logger.Sync()
|
|
|
|
|
zap.ReplaceGlobals(logger)
|
|
|
|
|
|
|
|
|
|
zap.L().Info("magneticow v0.7.0 has been started.")
|
|
|
|
|
zap.L().Info("Copyright (C) 2017 Mert Bora ALPER <bora@boramalper.org>.")
|
|
|
|
|
zap.L().Info("Dedicated to Cemile Binay, in whose hands I thrived.")
|
|
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
|
router.HandleFunc("/", rootHandler)
|
2018-06-19 17:49:46 +02:00
|
|
|
|
|
|
|
|
|
router.HandleFunc("/api/v0.1/torrents", apiTorrentsHandler)
|
|
|
|
|
router.HandleFunc("/api/v0.1/torrents/{infohash:[a-z0-9]{40}}", apiTorrentsInfohashHandler)
|
|
|
|
|
router.HandleFunc("/api/v0.1/files/{infohash:[a-z0-9]{40}}", apiFilesInfohashHandler)
|
|
|
|
|
router.HandleFunc("/api/v0.1/statistics", apiStatisticsHandler)
|
|
|
|
|
|
2017-11-03 00:15:13 +01:00
|
|
|
|
router.HandleFunc("/torrents", torrentsHandler)
|
|
|
|
|
router.HandleFunc("/torrents/{infohash:[a-z0-9]{40}}", torrentsInfohashHandler)
|
|
|
|
|
router.HandleFunc("/statistics", statisticsHandler)
|
2018-06-19 17:49:46 +02:00
|
|
|
|
router.HandleFunc("/feed", feedHandler)
|
|
|
|
|
|
2017-11-03 00:15:13 +01:00
|
|
|
|
router.PathPrefix("/static").HandlerFunc(staticHandler)
|
|
|
|
|
|
2018-06-19 17:49:46 +02:00
|
|
|
|
|
2017-11-03 00:15:13 +01:00
|
|
|
|
|
|
|
|
|
templateFunctions := template.FuncMap{
|
|
|
|
|
"add": func(augend int, addends int) int {
|
|
|
|
|
return augend + addends
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"subtract": func(minuend int, subtrahend int) int {
|
|
|
|
|
return minuend - subtrahend
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"bytesToHex": func(bytes []byte) string {
|
|
|
|
|
return hex.EncodeToString(bytes)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"unixTimeToYearMonthDay": func(s int64) string {
|
|
|
|
|
tm := time.Unix(s, 0)
|
|
|
|
|
// > Format and Parse use example-based layouts. Usually you’ll use a constant from time
|
|
|
|
|
// > for these layouts, but you can also supply custom layouts. Layouts must use the
|
|
|
|
|
// > reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to
|
|
|
|
|
// > format/parse a given time/string. The example time must be exactly as shown: the
|
|
|
|
|
// > year 2006, 15 for the hour, Monday for the day of the week, etc.
|
|
|
|
|
// https://gobyexample.com/time-formatting-parsing
|
|
|
|
|
// Why you gotta be so weird Go?
|
|
|
|
|
return tm.Format("02/01/2006")
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"humanizeSize": func(s uint64) string {
|
|
|
|
|
return humanize.IBytes(s)
|
|
|
|
|
},
|
2018-04-25 22:33:50 +02:00
|
|
|
|
|
|
|
|
|
"comma": func(s uint) string {
|
|
|
|
|
return humanize.Comma(int64(s))
|
|
|
|
|
},
|
2017-11-03 00:15:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
templates = make(map[string]*template.Template)
|
2018-04-25 22:33:50 +02:00
|
|
|
|
// templates["feed"] = template.Must(template.New("feed").Parse(string(mustAsset("templates/feed.xml"))))
|
|
|
|
|
templates["homepage"] = template.Must(template.New("homepage").Funcs(templateFunctions).Parse(string(mustAsset("templates/homepage.html"))))
|
|
|
|
|
// templates["statistics"] = template.Must(template.New("statistics").Parse(string(mustAsset("templates/statistics.html"))))
|
|
|
|
|
// templates["torrent"] = template.Must(template.New("torrent").Funcs(templateFunctions).Parse(string(mustAsset("templates/torrent.html"))))
|
2018-06-19 17:49:46 +02:00
|
|
|
|
// templates["torrents"] = template.Must(template.New("torrents").Funcs(templateFunctions).Parse(string(mustAsset("templates/torrents.html"))))
|
2017-11-03 00:15:13 +01:00
|
|
|
|
|
|
|
|
|
var err error
|
2018-04-25 22:33:50 +02:00
|
|
|
|
database, err = persistence.MakeDatabase("sqlite3:///home/bora/.local/share/magneticod/database.sqlite3", logger)
|
2017-11-03 00:15:13 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 17:49:46 +02:00
|
|
|
|
decoder.IgnoreUnknownKeys(false)
|
|
|
|
|
decoder.ZeroEmpty(true)
|
|
|
|
|
|
2017-11-03 00:15:13 +01:00
|
|
|
|
zap.L().Info("magneticow is ready to serve!")
|
2018-06-19 17:49:46 +02:00
|
|
|
|
err = http.ListenAndServe(":10101", router)
|
|
|
|
|
if err != nil {
|
|
|
|
|
zap.L().Error("ListenAndServe error", zap.Error(err))
|
|
|
|
|
}
|
2017-11-03 00:15:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DONE
|
|
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
2018-04-25 22:33:50 +02:00
|
|
|
|
nTorrents, err := database.GetNumberOfTorrents()
|
2017-11-03 00:15:13 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
2018-06-19 17:49:46 +02:00
|
|
|
|
|
|
|
|
|
err = templates["homepage"].Execute(w, HomepageTD{
|
2018-04-25 22:33:50 +02:00
|
|
|
|
NTorrents: nTorrents,
|
2017-11-03 00:15:13 +01:00
|
|
|
|
})
|
2018-06-19 17:49:46 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
2017-11-03 00:15:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 17:49:46 +02:00
|
|
|
|
// TODO: I think there is a standard lib. function for this
|
2018-04-25 22:33:50 +02:00
|
|
|
|
func respondError(w http.ResponseWriter, statusCode int, format string, a ...interface{}) {
|
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
|
w.Write([]byte(fmt.Sprintf(format, a...)))
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 17:49:46 +02:00
|
|
|
|
// TODO: we might as well move torrents.html into static...
|
2017-11-03 00:15:13 +01:00
|
|
|
|
func torrentsHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-19 17:49:46 +02:00
|
|
|
|
data := mustAsset("templates/torrents.html")
|
|
|
|
|
w.Header().Set("Content-Type", http.DetectContentType(data))
|
|
|
|
|
w.Write(data)
|
2017-11-03 00:15:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// show torrents/{infohash}
|
|
|
|
|
infoHash, err := hex.DecodeString(mux.Vars(r)["infohash"])
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torrent, err := database.GetTorrent(infoHash)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
templates["torrent"].Execute(w, torrent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func feedHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func staticHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
data, err := Asset(r.URL.Path[1:])
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var contentType string
|
|
|
|
|
if strings.HasSuffix(r.URL.Path, ".css") {
|
|
|
|
|
contentType = "text/css; charset=utf-8"
|
|
|
|
|
} else { // fallback option
|
|
|
|
|
contentType = http.DetectContentType(data)
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
w.Write(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustAsset(name string) []byte {
|
|
|
|
|
data, err := Asset(name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicf("Could NOT access the requested resource `%s`: %s (please inform us, this is a BUG!)", name, err.Error())
|
|
|
|
|
}
|
|
|
|
|
return data
|
|
|
|
|
}
|