Skip to content

Commit

Permalink
move to gin templates and update go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdy committed Oct 1, 2023
1 parent 22a8c3b commit 7b95b9c
Show file tree
Hide file tree
Showing 24 changed files with 282 additions and 262 deletions.
4 changes: 4 additions & 0 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Configuration struct {
Statistics bool

BootstrapNodeFile string

OnlyWebServer bool
}

func ParseArguments() *Configuration {
Expand All @@ -51,6 +53,8 @@ func ParseArguments() *Configuration {

flag.StringVar(&config.BootstrapNodeFile, "BootstrapNodeFile", "bootstrap-nodes.txt", "bootstrap nodes to use")

flag.BoolVar(&config.OnlyWebServer, "OnlyWebServer", false, "only start the web-server")

flag.Parse()

return &config
Expand Down
6 changes: 3 additions & 3 deletions db/blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ostafen/clover/v2"
"github.com/ostafen/clover/v2/document"
"github.com/ostafen/clover/v2/query"
"github.com/rs/zerolog/log"
"regexp"
)

Expand All @@ -25,10 +26,9 @@ func AddToBlacklist(db *clover.DB, filters []string, entryType string) bool {
doc.Set("Filter", filter)
_, err := db.InsertOne(BlacklistTable, doc)
if err != nil {
fmt.Println(err)
rVal = true
break
log.Error().Err(err)
}
rVal = err == nil
}
}

Expand Down
18 changes: 7 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ require (
github.com/anacrolix/missinggo/v2 v2.7.2
github.com/anacrolix/torrent v1.52.5
github.com/deckarep/golang-set/v2 v2.3.1
github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2
github.com/gin-gonic/gin v1.9.1
github.com/nikolalohinski/gonja v1.5.3
github.com/ostafen/clover/v2 v2.0.0-alpha.3.0.20230909104211-0726a6c7d4e5
github.com/leekchan/gtf v0.0.0-20190214083521-5fba33c5b00b
github.com/ostafen/clover/v2 v2.0.0-alpha.3.0.20230927171505-aa688ad9b8b2
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.30.0
github.com/rs/zerolog v1.31.0
github.com/willf/bloom v2.0.3+incompatible
gopkg.in/telebot.v3 v3.1.3
)
Expand All @@ -19,11 +20,10 @@ require (
github.com/anacrolix/missinggo v1.3.0 // indirect
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
github.com/dgraph-io/badger/v4 v4.2.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand All @@ -32,15 +32,14 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.4 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/gofrs/uuid/v5 v5.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/goph/emperror v0.17.2 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect
Expand All @@ -51,19 +50,16 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/willf/bitset v1.1.11 // indirect
github.com/yargevad/filepathx v1.0.0 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ func main() {
db.AddToBlacklist(database, ReadFileLines(cfg.NameBlacklist), "0")
db.AddToBlacklist(database, ReadFileLines(cfg.FileBlacklist), "1")

bot := notifier.SetupTelegramBot(cfg)
if !cfg.OnlyWebServer {
bot := notifier.SetupTelegramBot(cfg)

for i := 0; i < cfg.CrawlerThreads; i++ {
go crawl(cfg, database, bot)
for i := 0; i < cfg.CrawlerThreads; i++ {
go crawl(cfg, database, bot)
}
}

ui.RunWebServer(cfg, database)
Expand Down
14 changes: 6 additions & 8 deletions ui/blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package ui
import (
"dhtc/db"
"github.com/gin-gonic/gin"
"github.com/nikolalohinski/gonja"
"github.com/rs/zerolog/log"
"net/http"
)

var blacklistTplBytes, _ = templates.ReadFile("ui/templates/blacklist.html")
var blacklistTpl = gonja.Must(gonja.FromBytes(blacklistTplBytes))

func (c *Controller) BlacklistGet(ctx *gin.Context) {
out, _ := blacklistTpl.Execute(gonja.Context{
ctx.HTML(http.StatusOK, "blacklist", gin.H{
"path": ctx.FullPath(),
"results": db.GetBlacklistEntries(c.Database),
})
ctx.Data(http.StatusOK, "text/html", []byte(out))
}

func (c *Controller) BlacklistPost(ctx *gin.Context) {
Expand All @@ -32,11 +28,13 @@ func (c *Controller) BlacklistPost(ctx *gin.Context) {
c.Configuration.EnableBlacklist = false
opOk = true
}
out, _ := blacklistTpl.Execute(gonja.Context{

log.Print(opOk)

ctx.HTML(http.StatusOK, "blacklist", gin.H{
"path": ctx.FullPath(),
"op": op,
"opOk": opOk,
"results": db.GetBlacklistEntries(c.Database),
})
ctx.Data(http.StatusOK, "text/html", []byte(out))
}
7 changes: 1 addition & 6 deletions ui/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ package ui
import (
"dhtc/db"
"github.com/gin-gonic/gin"
"github.com/nikolalohinski/gonja"
"net/http"
)

var dashboardTplBytes, _ = templates.ReadFile("ui/templates/dashboard.html")
var dashboardTpl = gonja.Must(gonja.FromBytes(dashboardTplBytes))

func (c *Controller) Dashboard(ctx *gin.Context) {
out, _ := dashboardTpl.Execute(gonja.Context{
ctx.HTML(http.StatusOK, "dashboard", gin.H{
"info_hash_count": db.GetInfoHashCount(c.Database),
"path": ctx.FullPath(),
"statistics": c.Configuration.Statistics,
})
ctx.Data(http.StatusOK, "text/html", []byte(out))
}
11 changes: 3 additions & 8 deletions ui/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@ package ui
import (
"dhtc/db"
"github.com/gin-gonic/gin"
"github.com/nikolalohinski/gonja"
"net/http"
"strconv"
)

var discoverTplBytes, _ = templates.ReadFile("ui/templates/discover.html")
var discoverTpl = gonja.Must(gonja.FromBytes(discoverTplBytes))

func (c *Controller) DiscoverGet(ctx *gin.Context) {
out, _ := discoverTpl.Execute(gonja.Context{
ctx.HTML(http.StatusOK, "discover", gin.H{
"results": db.GetNRandomEntries(c.Database, 50),
"path": ctx.FullPath(),
})
ctx.Data(http.StatusOK, "text/html", []byte(out))
}

func (c *Controller) DiscoverPost(ctx *gin.Context) {
N, err := strconv.Atoi(ctx.PostForm("limit"))
if err != nil {
N = 50
}
out, _ := discoverTpl.Execute(gonja.Context{

ctx.HTML(http.StatusOK, "discover", gin.H{
"results": db.GetNRandomEntries(c.Database, N),
"path": ctx.FullPath(),
})
ctx.Data(http.StatusOK, "text/html", []byte(out))
}
2 changes: 1 addition & 1 deletion ui/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ui

import "embed"

//go:embed templates
//go:embed templates/**/*.html
var templates embed.FS

//go:embed static
Expand Down
12 changes: 4 additions & 8 deletions ui/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@ package ui
import (
"dhtc/db"
"github.com/gin-gonic/gin"
"github.com/nikolalohinski/gonja"
"net/http"
)

var searchTplBytes, _ = templates.ReadFile("ui/templates/search.html")
var searchTpl = gonja.Must(gonja.FromBytes(searchTplBytes))

func (c *Controller) SearchGet(ctx *gin.Context) {
out, _ := searchTpl.Execute(gonja.Context{"path": ctx.FullPath()})
ctx.Data(http.StatusOK, "text/html", []byte(out))
ctx.HTML(http.StatusOK, "search", gin.H{
"path": ctx.FullPath(),
})
}

func (c *Controller) SearchPost(ctx *gin.Context) {
out, _ := searchTpl.Execute(gonja.Context{
ctx.HTML(http.StatusOK, "search", gin.H{
"results": db.FindBy(
c.Database,
ctx.PostForm("key"),
ctx.PostForm("match-type"),
ctx.PostForm("search-input")),
"path": ctx.FullPath(),
})
ctx.Data(http.StatusOK, "text/html", []byte(out))
}
100 changes: 0 additions & 100 deletions ui/templates/blacklist.html

This file was deleted.

14 changes: 0 additions & 14 deletions ui/templates/dashboard.html

This file was deleted.

16 changes: 0 additions & 16 deletions ui/templates/discover.html

This file was deleted.

Loading

0 comments on commit 7b95b9c

Please sign in to comment.