forked from bertbaron/intravatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
240 lines (205 loc) · 7.32 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
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
package main
import (
"crypto/md5"
"flag"
"fmt"
"github.com/bertbaron/iniflags"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"path/filepath"
)
// Options
var (
dataDir = flag.String("data", "data", "Path to data files relative to current working dir.")
port = flag.Int("port", 8080, "Webserver port number.")
webroot = flag.String("webroot", "", "The webroot of the service, defaults to http://localhost:<port>")
logfile = flag.String("logfile", "", "Path to log file, if empty, the log will go to stderr of the process")
remote = flag.String("remote", "https://gravatar.com/avatar", "Comma-separated list of gravatar-compatible avatar\n"+
" services to use if no avatar is found.")
dflt = flag.String("default", "remote:monsterid", "Default avatar. Use 'remote' to use the default of the (last) remote\n"+
" service, or 'remote:<option>' to use a builtin default. For example: 'remote:monsterid'. This is passed as\n"+
" '?d=monsterid' to the remote service. See https://nl.gravatar.com/site/implement/images/.\n"+
" If no remote and no local default is configured, resources/mm is used as default.")
smtpHost = flag.String("smtp-host", "", "SMTP host used for email confirmation, if not configured no confirmation emails will be required")
smtpPort = flag.Int("smtp-port", 25, "SMTP port")
smtpUser = flag.String("smtp-user", "", "SMTP user")
smtpPassword = flag.String("smtp-password", "", "SMTP password")
sender = flag.String("sender", "", "Senders email address")
noTls = flag.Bool("no-tls", false, "Disable tls encription for email, less secure! Can be useful if certificates of in-house mailhost are expired.")
testMailAddr = flag.String("test-mail-addr", "", "If specified, sends a test email on startup to the given email address")
)
var (
defaultImage = "resources/mm"
remoteUrls = []string{}
remoteDefault = ""
templates *template.Template
)
const (
minSize = 8
maxSize = 512
configFile = "config.ini"
)
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil { return true }
if os.IsNotExist(err) { return false }
log.Fatalf("Error looking up directory %s", path)
return false
}
func mkdir(path string) {
if !exists(path) {
log.Printf("Creating directory %s", path)
os.Mkdir(path, 0700)
}
}
func createDirectoryStructure() {
mkdir(*dataDir)
mkdir(filepath.Join(*dataDir, "avatars"))
mkdir(filepath.Join(*dataDir, "unconfirmed"))
}
func createAvatarPath(hash string) string {
//return fmt.Sprintf("%s/avatars/%s", *dataDir, hash)
return filepath.Join(*dataDir, "avatars", hash)
}
func createUnconfirmedAvatarPath(hash string, token string) string {
//return fmt.Sprintf("%s/unconfirmed/%s-%s", *dataDir, token, hash)
return filepath.Join(*dataDir, "unconfirmed", fmt.Sprintf("%s-%s", token, hash))
}
func getUnconfirmedDir() string {
return fmt.Sprintf("%s/unconfirmed", *dataDir)
}
func setHeaderField(w http.ResponseWriter, key string, value string) {
if value != "" {
w.Header().Set(key, value)
}
}
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
err := templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func createHash(email string) string {
h := md5.New()
io.WriteString(h, strings.TrimSpace(strings.ToLower(email)))
return fmt.Sprintf("%x", h.Sum(nil))
}
func getServiceUrl() string {
url := *webroot
if url == "" {
portName := ""
if *port != 80 {
portName = fmt.Sprintf(":%d", *port)
}
url = "http://localhost" + portName
}
return url + "/"
}
func homeHandler(w http.ResponseWriter, r *http.Request, title string) {
url := getServiceUrl() + "avatar/"
renderTemplate(w, "index", map[string]string{"AvatarLink": url})
}
// Creates a http request handler
// pattern is a regular expression that validates the URL. The first matching group is passes to the handler as title
func makeHandler(fn func(http.ResponseWriter, *http.Request, string), pattern string) http.HandlerFunc {
regex := regexp.MustCompile(pattern)
return func(w http.ResponseWriter, r *http.Request) {
// log.Printf("Request: %v", r)
m := regex.FindStringSubmatch(r.URL.Path)
if m == nil {
log.Print("Invalid request: ", r.URL)
http.NotFound(w, r)
return
}
log.Printf("Handling request %v %v from %v", r.Method, r.URL, strings.Split(r.RemoteAddr, ":")[0])
start := time.Now()
fn(w, r, m[1])
log.Printf("Handled request %v %v in %v", r.Method, r.URL, time.Since(start))
}
}
func initTemplates() {
files, err := ioutil.ReadDir("resources/templates")
if err != nil {
log.Fatal(err)
}
var fileNames []string
for _, file := range files {
fileNames = append(fileNames, "resources/templates/"+file.Name())
}
templates = template.Must(template.ParseFiles(fileNames...))
}
// serves a single file
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
}
func main() {
if _, e := os.Stat(configFile); e == nil {
log.Printf("Default configuration file %v", configFile)
iniflags.SetConfigFile(configFile)
}
iniflags.Parse()
if *logfile != "" {
file, err := os.OpenFile(*logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file for logging: %v", err)
}
log.Printf("Logging will be redirected to %v", *logfile)
defer file.Close()
log.SetOutput(file)
}
if *smtpHost != "" && *sender == "" {
if *sender == "" {
log.Fatal("It is required to configure 'sender' when smtp host is not empty!")
}
}
initTemplates()
log.Printf("data dir = %s\n", *dataDir)
address := fmt.Sprintf(":%d", *port)
if *remote == "" {
remoteUrls = []string{}
} else {
remoteUrls = strings.Split(*remote, ",")
log.Printf("Missing avatars will be redirected to %s", remoteUrls)
}
remoteFallbackPattern := regexp.MustCompile("^remote:([a-zA-Z]+)$")
if *dflt == "fallback" {
log.Printf("Default image will be provided by the remote service if configured")
remoteDefault = ""
} else if builtin := remoteFallbackPattern.FindStringSubmatch(*dflt); builtin != nil {
remoteDefault = builtin[1]
log.Printf("Default image will be provided by the remote service using '?d=%s' if a remote is configured", remoteDefault)
} else {
defaultImage = *dflt
remoteDefault = "404"
log.Printf("Using %s as default image", defaultImage)
}
if *testMailAddr != "" {
if err := sendTestMail(*testMailAddr); err != nil {
log.Fatalf("Failed to send test email to %s: %v", *testMailAddr, err)
}
}
createDirectoryStructure()
log.Printf("Listening on %s\n", address)
log.Printf("Service url: %s\n", getServiceUrl())
http.HandleFunc("/", makeHandler(homeHandler, "^/()$"))
// Mandatory root-based resources
serveSingle("/favicon.ico", "resources/favicon.ico")
// Other static resources
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("resources/static/"))))
// Application
http.HandleFunc("/avatar/", makeHandler(avatarHandler, "^/avatar/([a-zA-Z0-9]+)$"))
http.HandleFunc("/upload/", makeHandler(uploadHandler, "^/(upload)/$"))
http.HandleFunc("/save/", makeHandler(saveHandler, "^/(save)/$"))
http.HandleFunc("/confirm/", makeHandler(confirmHandler, "^/confirm/([a-zA-Z0-9]+)$"))
x := http.ListenAndServe(address, nil)
fmt.Println("Result: ", x)
}