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
|
package main
import (
"encoding/json"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"slices"
"strings"
"time"
)
func LoadPostsForUser(u *User) {
posts := []*NoteMeta{}
WalkCache(u, "notes", "", func(id string, data []byte) {
note := &NoteMeta{}
if err := json.Unmarshal(data, note); err == nil {
note.created, _ = time.Parse("2006-01-02T15:04:05Z", note.Published)
posts = append(posts, note)
}
})
slices.SortFunc(posts, func(a *NoteMeta, b *NoteMeta) int {
return a.created.Compare(b.created)
})
slices.Reverse(posts)
u.LoadPosts(posts)
}
func LoadUsers(config *Config) map[string]*User {
users := make(map[string]*User)
items, _ := os.ReadDir("users")
userList := []*User{}
for _, item := range items {
if item.IsDir() {
} else {
// handle file there
user := UserFromFile("users/"+item.Name(), config)
fmt.Printf("[startup] Loading User %s\n", user.UserName)
userNameNoAt := strings.TrimPrefix(user.UserName, "@")
users["acct:"+userNameNoAt+"@"+user.config.Host] = user
users["acct:"+user.UserName+"@"+user.config.Host] = user
users[userNameNoAt+"@"+user.config.Host] = user
users[user.UserName+"@"+user.config.Host] = user
http.HandleFunc("/"+userNameNoAt+"/inbox", user.processInbox)
http.HandleFunc("/"+userNameNoAt+"/outbox", user.processOutbox)
http.HandleFunc("/"+userNameNoAt+"/followers", user.processFollowers)
http.HandleFunc("/"+userNameNoAt+"/posts/{id}", user.processPost)
http.HandleFunc("/"+userNameNoAt+"/posts/{id}/{collection}", user.processCollection)
http.HandleFunc("/"+userNameNoAt, user.processUserPage)
http.HandleFunc("/"+user.UserName, user.processUserPage)
http.HandleFunc("/"+userNameNoAt+".rss", user.processUserPageRSS)
http.HandleFunc("/"+userNameNoAt+".json", user.processUserPageJson)
http.HandleFunc("/"+userNameNoAt+"/mentions", user.viewMentions)
LoadPostsForUser(user)
userList = append(userList, user)
}
}
slices.SortFunc(userList, func(a *User, b *User) int {
return strings.Compare(a.UserName, b.UserName)
})
config.Users = userList
return users
}
func (config *Config) Misc(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "" || r.URL.Path == "/" || r.URL.Path == "index.html" {
page := config.DefaultPage(r)
page.Accounts = config.Users
config.Templates.ExecuteTemplate(w, "index.tpl.html", page)
} else {
fmt.Printf("[error] unregistered handler: %v\n", r.URL)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("usage: tap <hostname> (<port>)\n")
os.Exit(1)
}
// default to 3009, other use the arg provided...
port := "3009"
if len(os.Args) == 3 {
fmt.Printf("usage: tap <hostname>\n")
port = os.Args[2]
}
// instantiate a configuration for the service.
config, err := InitConfig(os.Args[1])
if err != nil {
fmt.Printf("[startup] Unable to load templates: %v", err)
os.Exit(1)
}
// load user accounts
users := LoadUsers(config)
// Handle the webfinger service
wfs := WebFingerServer{Users: users}
http.HandleFunc("/.well-known/webfinger", wfs.Handle)
nfs := NodeInfoServer{Host: config.Host}
http.HandleFunc("/.well-known/nodeinfo", nfs.HandleWellKnown)
http.HandleFunc("/nodeinfo/2.0", nfs.Handle2)
// Handle static files e.g. css, profile images etc.
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
// Handle authenticated endpoints e.g. new post, like, swtich account etc.
http.HandleFunc("/login", config.loginHandler)
http.HandleFunc("/new", config.newHandler)
http.HandleFunc("/admin", config.newHandler)
http.HandleFunc("/switch", config.switchHandler)
http.HandleFunc("/like", config.likeHandler)
// not found handler for anything not explicitly defined
http.HandleFunc("/", config.Misc)
if _, err := os.Stat("tap-profiling"); err == nil {
fmt.Printf("turning on profiling support\n")
go func() {
http.ListenAndServe("0.0.0.0:6060", nil) // Port for pprof
}()
}
err = http.ListenAndServe("127.0.0.1:"+port, nil)
if err != nil {
fmt.Printf("[startup] Unable to bind to port: %v", err)
}
}
|