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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Eko: A terminal based social media platform
// Copyright (C) 2025 Kyren223
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"crypto/sha256"
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/grafana/pyroscope-go"
_ "github.com/grafana/pyroscope-go/godeltaprof/http/pprof"
_ "net/http/pprof"
"github.com/kyren223/eko/embeds"
"github.com/kyren223/eko/internal/server"
"github.com/kyren223/eko/internal/server/api"
"github.com/kyren223/eko/internal/server/ctxkeys"
"github.com/kyren223/eko/internal/webserver"
"github.com/kyren223/eko/pkg/assert"
"gopkg.in/natefinch/lumberjack.v2"
)
const (
port = 7223
TosEnvVar = "EKO_SERVER_TOS_FILE"
PrivacyEnvVar = "EKO_SERVER_PRIVACY_FILE"
LogDirEnvVar = "EKO_SERVER_LOG_DIR"
)
var prod = true
func main() {
if len(os.Args) > 1 && os.Args[1] == "-v" {
fmt.Println("version:", embeds.Version)
fmt.Println("commit:", embeds.Commit)
fmt.Println("build date:", embeds.BuildDate)
return
}
prodFlag := flag.Bool("prod", true, "true for production mode, false for dev mode")
flag.Parse()
prod = *prodFlag
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
setupLogging()
slog.Info("mode", "prod", prod)
handleReload()
handleShutdown(cancel)
startPyroscopeProfiling()
if ok := reloadTosAndPrivacy(); !ok {
return
}
api.ConnectToDatabase()
assert.AddFlush(api.DB())
defer api.DB().Close()
go webserver.ServePrometheusMetrics()
go webserver.ServeEkoWebsite()
server := server.NewServer(ctx, port)
server.Run() // blocks
slog.Info("exited gracefully")
}
func handleReload() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
go func() {
for range c {
slog.Info("SIGHUP received, reloading...")
reloadTosAndPrivacy()
slog.Info("reload completed")
}
}()
slog.Info("reload handler ready")
}
func handleShutdown(cancel context.CancelFunc) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
signal := <-signalChan
slog.Info("shutdown signal received", "signal", signal)
cancel()
}()
slog.Info("shutdown handler ready")
}
func setupLogging() {
logDir := os.Getenv(LogDirEnvVar)
if logDir == "" {
logDir = "logs"
}
err := os.MkdirAll(logDir, 0750)
if err != nil {
log.Fatalln(err)
}
rotator := &lumberjack.Logger{
Filename: filepath.Join(logDir, "server.log"),
MaxSize: 100, // megabytes
MaxAge: 7, // days (see Privacy Section 3: Log Retention)
}
level := slog.LevelDebug
if prod {
level = slog.LevelInfo
}
baseHandler := slog.NewJSONHandler(rotator, &slog.HandlerOptions{
AddSource: true,
Level: level,
})
handler := ctxkeys.WrapLogHandler(baseHandler)
logger := slog.New(handler)
slog.SetDefault(logger)
slog.SetLogLoggerLevel(level) // TODO: remove me after fully migrating to slog
slog.Info("logging handler ready")
go func() {
for {
now := time.Now().UTC() // UTC Time (see Privacy Section 3)
next := now.Truncate(24 * time.Hour).Add(24 * time.Hour)
time.Sleep(time.Until(next)) // sleep until next midnight
err := rotator.Rotate()
if err != nil {
slog.Error("log file rotation failed", "error", err)
} else {
slog.Info("log file rotated")
}
}
}()
}
func reloadTosAndPrivacy() bool {
if embeds.TosPrivacyHash.Load() == nil {
if prod {
embeds.TosPrivacyHash.Store("")
embeds.TermsOfService.Store("")
embeds.PrivacyPolicy.Store("")
} else {
// Set stub values for development
embeds.TermsOfService.Store(embeds.StubTos)
embeds.PrivacyPolicy.Store(embeds.StubPrivacy)
tosPrivacy := []byte(string(embeds.StubTos) + string(embeds.StubPrivacy))
stubHash := fmt.Sprintf("%x", sha256.Sum256(tosPrivacy))
embeds.TosPrivacyHash.Store(stubHash)
return true
}
}
tosFile := os.Getenv(TosEnvVar)
privacyFile := os.Getenv(PrivacyEnvVar)
if tosFile == "" || privacyFile == "" {
if prod {
slog.Error("TOS or Privacy env vars are undefined", TosEnvVar, tosFile, PrivacyEnvVar, privacyFile)
return false
}
}
tos, err := os.ReadFile(tosFile) // #nosec G304
if err != nil {
slog.Error("error reading TOS file", "error", err)
return false
}
privacy, err := os.ReadFile(privacyFile) // #nosec G304
if err != nil {
slog.Error("error reading Privacy file", "error", err)
return false
}
tosPrivacy := []byte(string(tos) + string(privacy))
hash := fmt.Sprintf("%x", sha256.Sum256(tosPrivacy))
oldHash := embeds.TosPrivacyHash.Load().(string)
if oldHash == hash {
slog.Info("updated nothing, tos+privacy hash remained the same", "hash", hash)
return true
}
embeds.TermsOfService.Store(string(tos))
embeds.PrivacyPolicy.Store(string(privacy))
embeds.TosPrivacyHash.Store(hash)
if oldHash == "" {
slog.Info("loaded Terms of Service and Privacy Policy", "hash", hash)
} else {
slog.Info("updated tos+privacy, hash changed", "hash", hash, "old_hash", hash)
}
return true
}
func startPyroscopeProfiling() {
pyroscope.Start(pyroscope.Config{
ApplicationName: "eko",
ServerAddress: "http://localhost:4040",
Logger: pyroscope.StandardLogger, // nil to disable
// by default all profilers are enabled,
// but you can select the ones you want to use:
ProfileTypes: []pyroscope.ProfileType{
pyroscope.ProfileCPU,
pyroscope.ProfileAllocObjects,
pyroscope.ProfileAllocSpace,
pyroscope.ProfileInuseObjects,
pyroscope.ProfileInuseSpace,
pyroscope.ProfileGoroutines,
pyroscope.ProfileMutexCount,
pyroscope.ProfileMutexCount,
pyroscope.ProfileBlockCount,
pyroscope.ProfileBlockDuration,
},
})
}
|