-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
133 lines (114 loc) · 3.66 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
package main
import (
"context"
"embed"
"fmt"
"html/template"
"net/http"
"os"
"strings"
eventstore_badger "github.com/fiatjaf/eventstore/badger"
"github.com/fiatjaf/khatru"
"github.com/kelseyhightower/envconfig"
"github.com/nbd-wtf/go-nostr"
"github.com/rs/cors"
"github.com/rs/zerolog"
)
type Settings struct {
Port string `envconfig:"PORT" default:"2999"`
Domain string `envconfig:"DOMAIN" default:"njump.me"`
DiskCachePath string `envconfig:"DISK_CACHE_PATH" default:"/tmp/njump-internal"`
EventStorePath string `envconfig:"EVENT_STORE_PATH" default:"/tmp/njump-db"`
TailwindDebug bool `envconfig:"TAILWIND_DEBUG"`
}
//go:embed static/*
var static embed.FS
//go:embed templates/*
var templates embed.FS
var (
s Settings
log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()
tailwindDebugStuff template.HTML
)
func main() {
err := envconfig.Process("", &s)
if err != nil {
log.Fatal().Err(err).Msg("couldn't process envconfig")
return
} else {
if canonicalHost := os.Getenv("CANONICAL_HOST"); canonicalHost != "" {
s.Domain = canonicalHost
}
}
// if we're in tailwind debug mode, initialize the runtime tailwind stuff
if s.TailwindDebug {
configb, err := os.ReadFile("tailwind.config.js")
if err != nil {
log.Fatal().Err(err).Msg("failed to load tailwind.config.js")
return
}
config := strings.Replace(
strings.Replace(
string(configb),
"plugins: [require('@tailwindcss/typography')]", "", 1,
),
"module.exports", "tailwind.config", 1,
)
styleb, err := os.ReadFile("tailwind.css")
if err != nil {
log.Fatal().Err(err).Msg("failed to load tailwind.css")
return
}
style := string(styleb)
tailwindDebugStuff = template.HTML(fmt.Sprintf("<script src=\"https://cdn.tailwindcss.com?plugins=typography\"></script><script>\n%s</script><style type=\"text/tailwindcss\">%s</style>", config, style))
}
initCache()
// initialize routines
ctx := context.Background()
go updateArchives(ctx)
go deleteOldCachedEvents(ctx)
// expose our internal cache as a relay (mostly for debugging purposes)
relay := khatru.NewRelay()
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.RejectEvent = append(relay.RejectEvent,
func(context.Context, *nostr.Event) (bool, string) {
return true, "this relay is not writable"
},
)
// routes
mux := relay.Router()
mux.Handle("/njump/static/", http.StripPrefix("/njump/", http.FileServer(http.FS(static))))
mux.HandleFunc("/relays-archive.xml", renderArchive)
mux.HandleFunc("/npubs-archive.xml", renderArchive)
mux.HandleFunc("/services/oembed", renderOEmbed)
mux.HandleFunc("/relays-archive/", renderArchive)
mux.HandleFunc("/npubs-archive/", renderArchive)
mux.HandleFunc("/njump/image/", renderImage)
mux.HandleFunc("/njump/proxy/", proxy)
mux.HandleFunc("/robots.txt", renderRobots)
mux.HandleFunc("/r/", renderRelayPage)
mux.HandleFunc("/random", redirectToRandom)
mux.HandleFunc("/e/", redirectFromESlash)
mux.HandleFunc("/p/", redirectFromPSlash)
mux.HandleFunc("/favicon.ico", redirectToFavicon)
mux.HandleFunc("/", renderEvent)
log.Print("listening at http://0.0.0.0:" + s.Port)
if err := http.ListenAndServe("0.0.0.0:"+s.Port, cors.Default().Handler(relay)); err != nil {
log.Fatal().Err(err).Msg("")
}
select {}
}
func initCache() func() {
// initialize disk cache
deinit := cache.initialize()
// initialize eventstore database
if badgerBackend, ok := db.(*eventstore_badger.BadgerBackend); ok {
// it may be NullStore, in which case we do nothing
badgerBackend.Path = s.EventStorePath
}
db.Init()
return func() {
deinit()
db.Close()
}
}