-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompactbro.go
More file actions
149 lines (130 loc) · 4.06 KB
/
compactbro.go
File metadata and controls
149 lines (130 loc) · 4.06 KB
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
package main
import (
"fmt"
"html"
"html/template"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/eknkc/amber"
"github.com/kirsle/configdir"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rdtmaster/go-reddit/v4/reddit"
)
const emptyHTML = template.HTML("")
var (
config CompactConfig
server *echo.Echo
tpls map[string]*template.Template
client *reddit.Client
)
// Shut the server down
func shutdown(c echo.Context) error {
go func() {
time.Sleep(500 * time.Millisecond)
server.Close()
os.Exit(0)
}()
return c.HTML(200, `<html><head><title>Goodbye</title><body bgcolor="#000123" text="#cdedfe"><h1 style="font-family: tahoma;right: 50%;bottom: 50%;transform: translate(50%,50%);position: absolute">Stopping server...</h1></body></html>`)
}
func main() {
configPath := configdir.LocalConfig("compactbro")
err := configdir.MakePath(configPath) // Ensure it exists.
if err != nil {
fmt.Println(err)
return
}
configFile := filepath.Join(configPath, "compactbro.toml")
fmt.Println("using config file ", configFile)
_, err = toml.DecodeFile(configFile, &config)
if err != nil {
fmt.Println(err)
return
}
amber.FuncMap["linkFromContext"] = linkFromContext
amber.FuncMap["cleanCommentID"] = cleanCommentID
amber.FuncMap["cleanLink"] = cleanLink
amber.FuncMap["isPostID"] = isPostID
amber.FuncMap["cVersion"] = cVersion
amber.FuncMap["emoji"] = emoji
amber.FuncMap["cssTheme"] = cssTheme
amber.FuncMap["getThumb"] = getThumb
amber.FuncMap["getDistinguished"] = getDistinguished
amber.FuncMap["isMine"] = isMine
amber.FuncMap["html"] = func(s string) template.HTML {
return template.HTML(html.UnescapeString(s))
}
amber.FuncMap["dateAgo"] = dateAgo
amber.FuncMap["likesInt"] = likesInt
amber.FuncMap["strNotEmpty"] = strNotEmpty
amber.FuncMap["hasReplies"] = hasReplies
amber.FuncMap["processReplies"] = processReplies
amber.FuncMap["getMe"] = getMe
tpls, err = amber.CompileDir("templates",
amber.DirOptions{Ext: ".amber", Recursive: true},
config.TemplateOptions)
if err != nil {
fmt.Println("Error compiling templates ", err.Error())
return
}
client, err = reddit.NewClient(config.Credentials)
if err != nil {
fmt.Println("Error Initializing client ", err.Error())
return
}
server = echo.New()
server.HideBanner = true
server.Static("/static", "static")
server.File("/favicon.ico", "static/favicon.ico")
if config.Logging {
server.Use(middleware.Logger())
}
server.Use(middleware.Recover())
if config.Auth.Use {
server.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
return strings.EqualFold(username, config.Auth.Username) && password == config.Auth.Password, nil
}))
}
// Routes
server.GET("/stop*", shutdown)
server.GET("/", frontpage)
server.GET("/message/", messageInbox)
server.GET("/message/:page/", messageInbox)
server.GET("/pt/message/:page/", messageInboxPT)
server.GET("/pt/", frontpagePT)
server.GET("/r/:sub", subDefault)
server.GET("/r/:sub/", subDefault)
server.GET("/r/:sub/:sorting", subSorted)
server.GET("/r/:sub/:sorting/", subSorted)
server.GET("/pt/r/:sub/", subPT)
server.GET("/r/:sub/comments/:id/", submission)
server.GET("/r/:sub/comments/:id/:permalink/", submission)
server.GET("/user/:sub/comments/:id/:permalink/", submission)
server.GET("/u/:username/", overview)
server.GET("/u/:username", overview)
server.GET("/u/:username/:page/", overview)
server.GET("/pt/u/:username/:page/", overviewPT)
server.POST("/edit/", editThing)
server.POST("/comment/", submitComment)
server.GET("/vote/:direction/:thing_id/", vote)
server.GET("/r/:sub/comments/:postID/:permalink/:commentID/", commentThread)
server.HEAD("/checkunread/", checkUnread)
// Start server
go func() {
if err := server.Start(config.LocalAddress); err != http.ErrServerClosed {
fmt.Println(err)
}
}()
if config.HTTPS.Use {
if err := server.StartTLS(config.HTTPS.LocalAddress,
config.HTTPS.CRTPath,
config.HTTPS.KeyPath); err != http.ErrServerClosed {
fmt.Println(err)
os.Exit(0)
}
}
}