http.go (view raw)
1package main
2
3import (
4 "bytes"
5 "fmt"
6 gmi "git.sr.ht/~adnano/go-gemini"
7 "github.com/gorilla/handlers"
8 "github.com/gorilla/sessions"
9 _ "github.com/mattn/go-sqlite3"
10 "golang.org/x/crypto/bcrypt"
11 "html/template"
12 "io"
13 "io/ioutil"
14 "log"
15 "net/http"
16 "net/url"
17 "os"
18 "path"
19 "path/filepath"
20 "strings"
21 "time"
22)
23
24var t *template.Template
25var SessionStore *sessions.CookieStore
26
27func renderDefaultError(w http.ResponseWriter, statusCode int) {
28 errorMsg := http.StatusText(statusCode)
29 renderError(w, errorMsg, statusCode)
30}
31
32func serverError(w http.ResponseWriter, err error) {
33 log.Print(err)
34 renderDefaultError(w, 500)
35}
36
37func renderError(w http.ResponseWriter, errorMsg string, statusCode int) {
38 data := struct {
39 StatusCode int
40 ErrorMsg string
41 Config Config
42 }{statusCode, errorMsg, c}
43 w.WriteHeader(statusCode)
44 err := t.ExecuteTemplate(w, "error.html", data)
45 if err != nil { // Shouldn't happen probably
46 http.Error(w, errorMsg, statusCode)
47 }
48}
49
50func rootHandler(w http.ResponseWriter, r *http.Request) {
51 // serve everything inside static directory
52 if r.URL.Path != "/" {
53 fileName := path.Join(c.TemplatesDirectory, "static", filepath.Clean(r.URL.Path))
54 _, err := os.Stat(fileName)
55 if err != nil {
56 renderDefaultError(w, http.StatusNotFound)
57 return
58 }
59 http.ServeFile(w, r, fileName) // TODO better error handling
60 return
61 }
62
63 user := getAuthUser(r)
64 indexFiles, err := getIndexFiles(user.IsAdmin)
65 if err != nil {
66 serverError(w, err)
67 return
68 }
69 allUsers, err := getActiveUserNames()
70 if err != nil {
71 serverError(w, err)
72 return
73 }
74 data := struct {
75 Config Config
76 AuthUser AuthUser
77 Files []*File
78 Users []string
79 }{c, user, indexFiles, allUsers}
80 err = t.ExecuteTemplate(w, "index.html", data)
81 if err != nil {
82 serverError(w, err)
83 return
84 }
85}
86
87func editFileHandler(w http.ResponseWriter, r *http.Request) {
88 user := getAuthUser(r)
89 if !user.LoggedIn {
90 renderDefaultError(w, http.StatusForbidden)
91 return
92 }
93 fileName := filepath.Clean(r.URL.Path[len("/edit/"):])
94 filePath := path.Join(c.FilesDirectory, user.Username, fileName)
95 isText := isTextFile(filePath)
96 alert := ""
97 var warnings []string
98 if r.Method == "POST" {
99 // get post body
100 alert = "saved"
101 r.ParseForm()
102 fileText := r.Form.Get("file_text")
103 // Web form by default gives us CR LF newlines.
104 // Unix files use just LF
105 fileText = strings.ReplaceAll(fileText, "\r\n", "\n")
106 fileBytes := []byte(fileText)
107 err := checkIfValidFile(user.Username, filePath, fileBytes)
108 if err != nil {
109 log.Println(err)
110 renderError(w, err.Error(), http.StatusBadRequest)
111 return
112 }
113 sfl := getSchemedFlounderLinkLines(strings.NewReader(fileText))
114 if len(sfl) > 0 {
115 warnings = append(warnings, "Warning! Some of your links to pages use schemas. This means that they may break when viewed in Gemini or over HTTPS. Plase remove gemini: or https: from the start of these links:\n")
116 for _, l := range sfl {
117 warnings = append(warnings, l)
118 }
119 }
120 // create directories if dne
121 os.MkdirAll(path.Dir(filePath), os.ModePerm)
122 newName := filepath.Clean(r.Form.Get("rename"))
123 err = checkIfValidFile(user.Username, newName, fileBytes)
124 if err != nil {
125 log.Println(err)
126 renderError(w, err.Error(), http.StatusBadRequest)
127 return
128 }
129 if isText { // Cant edit binary files here
130 err = ioutil.WriteFile(filePath, fileBytes, 0644)
131 if err != nil {
132 log.Println(err)
133 renderError(w, err.Error(), http.StatusBadRequest)
134 }
135 }
136 if newName != fileName {
137 newPath := path.Join(c.FilesDirectory, user.Username, newName)
138 os.MkdirAll(path.Dir(newPath), os.ModePerm)
139 os.Rename(filePath, newPath)
140 fileName = newName
141 filePath = newPath
142 alert += " and renamed"
143 }
144 }
145
146 err := checkIfValidFile(user.Username, filePath, nil)
147 if err != nil {
148 log.Println(err)
149 renderError(w, err.Error(), http.StatusBadRequest)
150 return
151 }
152 // Create directories if dne
153 f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
154 var fileBytes []byte
155 if os.IsNotExist(err) || !isText {
156 fileBytes = []byte{}
157 err = nil
158 } else {
159 defer f.Close()
160 fileBytes, err = ioutil.ReadAll(f)
161 }
162 if err != nil {
163 serverError(w, err)
164 return
165 }
166 data := struct {
167 FileName string
168 FileText string
169 Config Config
170 AuthUser AuthUser
171 Host string
172 IsText bool
173 IsGemini bool
174 IsGemlog bool
175 Alert string
176 Warnings []string
177 }{fileName, string(fileBytes), c, user, c.Host, isText, isGemini(fileName), strings.HasPrefix(fileName, "gemlog"), alert, warnings}
178 err = t.ExecuteTemplate(w, "edit_file.html", data)
179 if err != nil {
180 serverError(w, err)
181 return
182 }
183}
184
185func uploadFilesHandler(w http.ResponseWriter, r *http.Request) {
186 if r.Method == "POST" {
187 user := getAuthUser(r)
188 if !user.LoggedIn {
189 renderDefaultError(w, http.StatusForbidden)
190 return
191 }
192 r.ParseMultipartForm(10 << 6) // why does this not work
193 file, fileHeader, err := r.FormFile("file")
194 if err != nil {
195 log.Println(err)
196 renderError(w, "No file selected. Please go back and select a file.", http.StatusBadRequest)
197 return
198 }
199 fileName := filepath.Clean(fileHeader.Filename)
200 defer file.Close()
201 dest, _ := ioutil.ReadAll(file)
202 err = checkIfValidFile(user.Username, fileName, dest)
203 if err != nil {
204 log.Println(err)
205 renderError(w, err.Error(), http.StatusBadRequest)
206 return
207 }
208 destPath := path.Join(c.FilesDirectory, user.Username, fileName)
209
210 f, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE, 0644)
211 if err != nil {
212 serverError(w, err)
213 return
214 }
215 defer f.Close()
216 io.Copy(f, bytes.NewReader(dest))
217 }
218 http.Redirect(w, r, "/my_site", http.StatusSeeOther)
219}
220
221type AuthUser struct {
222 LoggedIn bool
223 Username string
224 IsAdmin bool
225 ImpersonatingUser string // used if impersonating
226}
227
228func getAuthUser(r *http.Request) AuthUser {
229 session, _ := SessionStore.Get(r, "cookie-session")
230 user, ok := session.Values["auth_user"].(string)
231 impers, _ := session.Values["impersonating_user"].(string)
232 isAdmin, _ := session.Values["admin"].(bool)
233 return AuthUser{
234 LoggedIn: ok,
235 Username: user,
236 IsAdmin: isAdmin,
237 ImpersonatingUser: impers,
238 }
239}
240
241func mySiteHandler(w http.ResponseWriter, r *http.Request) {
242 user := getAuthUser(r)
243 if !user.LoggedIn {
244 renderDefaultError(w, http.StatusForbidden)
245 return
246 }
247 // check auth
248 userFolder := getUserDirectory(user.Username)
249 files, _ := getMyFilesRecursive(userFolder, user.Username)
250 currentDate := time.Now().Format("2006-01-02")
251 data := struct {
252 Config Config
253 Files []File
254 AuthUser AuthUser
255 CurrentDate string
256 }{c, files, user, currentDate}
257 _ = t.ExecuteTemplate(w, "my_site.html", data)
258}
259
260func myAccountHandler(w http.ResponseWriter, r *http.Request) {
261 user := getAuthUser(r)
262 authUser := user.Username
263 if !user.LoggedIn {
264 renderDefaultError(w, http.StatusForbidden)
265 return
266 }
267 me, _ := getUserByName(user.Username)
268 type pageData struct {
269 Config Config
270 AuthUser AuthUser
271 MyUser *User
272 Errors []string
273 }
274 data := pageData{c, user, me, nil}
275
276 if r.Method == "GET" {
277 err := t.ExecuteTemplate(w, "me.html", data)
278 if err != nil {
279 serverError(w, err)
280 return
281 }
282 } else if r.Method == "POST" {
283 r.ParseForm()
284 newUsername := r.Form.Get("username")
285 errors := []string{}
286 newEmail := r.Form.Get("email")
287 newDomain := r.Form.Get("domain")
288 newUsername = strings.ToLower(newUsername)
289 var err error
290 _, exists := domains[newDomain]
291 if newDomain != me.Domain && !exists {
292 _, err = DB.Exec("update user set domain = ? where username = ?", newDomain, me.Username) // TODO use transaction
293 if err != nil {
294 errors = append(errors, err.Error())
295 } else {
296 refreshDomainMap()
297 log.Printf("Changed domain for %s from %s to %s", authUser, me.Domain, newDomain)
298 }
299 }
300 if newEmail != me.Email {
301 _, err = DB.Exec("update user set email = ? where username = ?", newEmail, me.Username)
302 if err != nil {
303 // TODO better error not sql
304 errors = append(errors, err.Error())
305 } else {
306 log.Printf("Changed email for %s from %s to %s", authUser, me.Email, newEmail)
307 }
308 }
309 if newUsername != authUser {
310 // Rename User
311 err = renameUser(authUser, newUsername)
312 if err != nil {
313 log.Println(err)
314 errors = append(errors, "Could not rename user")
315 } else {
316 session, _ := SessionStore.Get(r, "cookie-session")
317 session.Values["auth_user"] = newUsername
318 session.Save(r, w)
319 }
320 }
321 // reset auth
322 user = getAuthUser(r)
323 data.Errors = errors
324 data.AuthUser = user
325 data.MyUser.Email = newEmail
326 data.MyUser.Domain = newDomain
327 _ = t.ExecuteTemplate(w, "me.html", data)
328 }
329}
330
331func archiveHandler(w http.ResponseWriter, r *http.Request) {
332 authUser := getAuthUser(r)
333 if !authUser.LoggedIn {
334 renderDefaultError(w, http.StatusForbidden)
335 return
336 }
337 if r.Method == "GET" {
338 userFolder := getUserDirectory(authUser.Username)
339 err := zipit(userFolder, w)
340 if err != nil {
341 serverError(w, err)
342 return
343 }
344
345 }
346}
347func loginHandler(w http.ResponseWriter, r *http.Request) {
348 if r.Method == "GET" {
349 // show page
350 data := struct {
351 Error string
352 Config Config
353 }{"", c}
354 err := t.ExecuteTemplate(w, "login.html", data)
355 if err != nil {
356 serverError(w, err)
357 return
358 }
359 } else if r.Method == "POST" {
360 r.ParseForm()
361 name := strings.ToLower(r.Form.Get("username"))
362 password := r.Form.Get("password")
363 row := DB.QueryRow("SELECT username, password_hash, active, admin FROM user where username = $1 OR email = $1", name)
364 var db_password []byte
365 var username string
366 var active bool
367 var isAdmin bool
368 err := row.Scan(&username, &db_password, &active, &isAdmin)
369 if err != nil {
370 if strings.Contains(err.Error(), "no rows") {
371 data := struct {
372 Error string
373 Config Config
374 }{"Username or email '" + name + "' does not exist", c}
375 w.WriteHeader(401)
376 t.ExecuteTemplate(w, "login.html", data)
377 return
378 } else {
379 serverError(w, err)
380 return
381 }
382 }
383 if db_password != nil && !active {
384 data := struct {
385 Error string
386 Config Config
387 }{"Your account is not active yet. Pending admin approval", c}
388 w.WriteHeader(401)
389 t.ExecuteTemplate(w, "login.html", data)
390 return
391 }
392 if bcrypt.CompareHashAndPassword(db_password, []byte(password)) == nil {
393 log.Println("logged in")
394 session, _ := SessionStore.Get(r, "cookie-session")
395 session.Values["auth_user"] = username
396 session.Values["admin"] = isAdmin
397 session.Save(r, w)
398 http.Redirect(w, r, "/my_site", http.StatusSeeOther)
399 } else {
400 data := struct {
401 Error string
402 Config Config
403 }{"Invalid login or password", c}
404 w.WriteHeader(401)
405 err := t.ExecuteTemplate(w, "login.html", data)
406 if err != nil {
407 serverError(w, err)
408 return
409 }
410 }
411 }
412}
413
414func logoutHandler(w http.ResponseWriter, r *http.Request) {
415 session, _ := SessionStore.Get(r, "cookie-session")
416 impers, ok := session.Values["impersonating_user"].(string)
417 if ok {
418 session.Values["auth_user"] = impers
419 session.Values["impersonating_user"] = nil // TODO expire this automatically
420 // session.Values["admin"] = nil // TODO fix admin
421 } else {
422 session.Options.MaxAge = -1
423 }
424 session.Save(r, w)
425 http.Redirect(w, r, "/", http.StatusSeeOther)
426}
427
428const ok = "-0123456789abcdefghijklmnopqrstuvwxyz"
429
430var bannedUsernames = []string{"www", "proxy", "grafana"}
431
432func registerHandler(w http.ResponseWriter, r *http.Request) {
433 if r.Method == "GET" {
434 data := struct {
435 Errors []string
436 Config Config
437 }{nil, c}
438 err := t.ExecuteTemplate(w, "register.html", data)
439 if err != nil {
440 serverError(w, err)
441 return
442 }
443 } else if r.Method == "POST" {
444 r.ParseForm()
445 email := strings.ToLower(r.Form.Get("email"))
446 password := r.Form.Get("password")
447 errors := []string{}
448 if r.Form.Get("password") != r.Form.Get("password2") {
449 errors = append(errors, "Passwords don't match")
450 }
451 if len(password) < 6 {
452 errors = append(errors, "Password is too short")
453 }
454 username := strings.ToLower(r.Form.Get("username"))
455 err := isOkUsername(username)
456 if err != nil {
457 errors = append(errors, err.Error())
458 }
459 _, err = os.Stat(getUserDirectory(username))
460 if !os.IsNotExist(err) {
461 // Don't allow user to create account if folder dne
462 errors = append(errors, "Invalid username")
463 }
464 hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 8) // TODO handle error
465 if err != nil {
466 serverError(w, err)
467 return
468 }
469 reference := r.Form.Get("reference")
470 if len(errors) == 0 {
471 _, err = DB.Exec("insert into user (username, email, password_hash, reference) values ($1, $2, $3, $4)", username, email, string(hashedPassword), reference)
472 if err != nil {
473 errors = append(errors, "Username or email is already used")
474 }
475 }
476 if len(errors) > 0 {
477 data := struct {
478 Config Config
479 Errors []string
480 }{c, errors}
481 w.WriteHeader(400)
482 t.ExecuteTemplate(w, "register.html", data)
483 } else {
484 data := struct {
485 Config Config
486 Message string
487 Title string
488 }{c, "Registration complete! The server admin will approve your request before you can log in.", "Registration Complete"}
489 t.ExecuteTemplate(w, "message.html", data)
490 }
491 }
492}
493
494func deleteFileHandler(w http.ResponseWriter, r *http.Request) {
495 user := getAuthUser(r)
496 if !user.LoggedIn {
497 renderDefaultError(w, http.StatusForbidden)
498 return
499 }
500 filePath := safeGetFilePath(user.Username, r.URL.Path[len("/delete/"):])
501 if r.Method == "POST" {
502 os.Remove(filePath) // TODO handle error
503 }
504 http.Redirect(w, r, "/my_site", http.StatusSeeOther)
505}
506
507func adminHandler(w http.ResponseWriter, r *http.Request) {
508 user := getAuthUser(r)
509 if !user.IsAdmin {
510 renderDefaultError(w, http.StatusForbidden)
511 return
512 }
513 allUsers, err := getUsers()
514 if err != nil {
515 log.Println(err)
516 renderDefaultError(w, http.StatusInternalServerError)
517 return
518 }
519 data := struct {
520 Users []User
521 AuthUser AuthUser
522 Config Config
523 }{allUsers, user, c}
524 err = t.ExecuteTemplate(w, "admin.html", data)
525 if err != nil {
526 serverError(w, err)
527 return
528 }
529}
530
531func getFavicon(user string) string {
532 faviconPath := path.Join(c.FilesDirectory, filepath.Clean(user), "favicon.txt")
533 content, err := ioutil.ReadFile(faviconPath)
534 if err != nil {
535 return ""
536 }
537 strcontent := []rune(string(content))
538 if len(strcontent) > 0 {
539 return string(strcontent[0])
540 }
541 return ""
542}
543
544// Server a user's file
545// TODO replace with gemini proxy
546// Here be dragons
547func userFile(w http.ResponseWriter, r *http.Request) {
548 var userName string
549 custom := domains[r.Host]
550 if custom != "" {
551 userName = custom
552 } else {
553 userName = filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary
554 }
555 p := filepath.Clean(r.URL.Path)
556 var isDir bool
557 fullPath := path.Join(c.FilesDirectory, userName, p) // TODO rename filepath
558 stat, err := os.Stat(fullPath)
559 if stat != nil {
560 isDir = stat.IsDir()
561 }
562 if strings.HasSuffix(p, "index.gmi") {
563 http.Redirect(w, r, path.Dir(p), http.StatusMovedPermanently)
564 }
565
566 if strings.HasPrefix(p, "/"+HiddenFolder) {
567 renderDefaultError(w, http.StatusForbidden)
568 return
569 }
570 if r.URL.Path == "/gemlog/atom.xml" && os.IsNotExist(err) {
571 w.Header().Set("Content-Type", "application/atom+xml")
572 // TODO set always somehow
573 feed := generateFeedFromUser(userName)
574 atomString := feed.toAtomFeed()
575 io.Copy(w, strings.NewReader(atomString))
576 return
577 }
578
579 var geminiContent string
580 _, err = os.Stat(path.Join(fullPath, "index.gmi"))
581 if isDir {
582 // redirect slash
583 if !strings.HasSuffix(r.URL.Path, "/") {
584 http.Redirect(w, r, p+"/", http.StatusSeeOther)
585 }
586 if os.IsNotExist(err) {
587 if p == "/gemlog" {
588 geminiContent = generateGemfeedPage(userName)
589 } else {
590 geminiContent = generateFolderPage(fullPath)
591 }
592 } else {
593 fullPath = path.Join(fullPath, "index.gmi")
594 }
595 }
596 if geminiContent == "" && os.IsNotExist(err) {
597 renderDefaultError(w, http.StatusNotFound)
598 return
599 }
600 // Dumb content negotiation
601 _, raw := r.URL.Query()["raw"]
602 acceptsGemini := strings.Contains(r.Header.Get("Accept"), "text/gemini")
603 if !raw && !acceptsGemini && (isGemini(fullPath) || geminiContent != "") {
604 var htmlDoc ConvertedGmiDoc
605 if geminiContent == "" {
606 file, _ := os.Open(fullPath)
607 parse, _ := gmi.ParseText(file)
608 htmlDoc = textToHTML(nil, parse)
609 defer file.Close()
610 } else {
611 parse, _ := gmi.ParseText(strings.NewReader(geminiContent))
612 htmlDoc = textToHTML(nil, parse)
613 }
614 favicon := getFavicon(userName)
615 hostname := strings.Split(r.Host, ":")[0]
616 uri := url.URL{
617 Scheme: "gemini",
618 Host: hostname,
619 Path: p,
620 }
621 if htmlDoc.Title == "" {
622 htmlDoc.Title = userName + p
623 }
624 data := struct {
625 SiteBody template.HTML
626 Favicon string
627 PageTitle string
628 URI *url.URL
629 GeminiURI *url.URL
630 Config Config
631 }{template.HTML(htmlDoc.Content), favicon, htmlDoc.Title, &uri, &uri, c}
632 buff := bytes.NewBuffer([]byte{})
633 err = t.ExecuteTemplate(buff, "user_page.html", data)
634 if err != nil {
635 serverError(w, err)
636 return
637 }
638 breader := bytes.NewReader(buff.Bytes())
639 http.ServeContent(w, r, "", stat.ModTime(), breader)
640 } else {
641 http.ServeFile(w, r, fullPath)
642 }
643}
644
645func deleteAccountHandler(w http.ResponseWriter, r *http.Request) {
646 user := getAuthUser(r)
647 if r.Method == "POST" {
648 r.ParseForm()
649 validate := r.Form.Get("validate-delete")
650 if validate == user.Username {
651 err := deleteUser(user.Username)
652 if err != nil {
653 log.Println(err)
654 renderDefaultError(w, http.StatusInternalServerError)
655 return
656 }
657 logoutHandler(w, r)
658 } else {
659 http.Redirect(w, r, "/me", http.StatusSeeOther)
660 }
661 }
662}
663
664func resetPasswordHandler(w http.ResponseWriter, r *http.Request) {
665 user := getAuthUser(r)
666 data := struct {
667 Config Config
668 AuthUser AuthUser
669 Error string
670 }{c, user, ""}
671 if r.Method == "GET" {
672 err := t.ExecuteTemplate(w, "reset_pass.html", data)
673 if err != nil {
674 serverError(w, err)
675 return
676 }
677 } else if r.Method == "POST" {
678 r.ParseForm()
679 enteredCurrPass := r.Form.Get("password")
680 password1 := r.Form.Get("new_password1")
681 password2 := r.Form.Get("new_password2")
682 if password1 != password2 {
683 data.Error = "New passwords do not match"
684 } else if len(password1) < 6 {
685 data.Error = "Password is too short"
686 } else {
687 err := checkAuth(user.Username, enteredCurrPass)
688 if err == nil {
689 hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password1), 8)
690 if err != nil {
691 serverError(w, err)
692 return
693 }
694 _, err = DB.Exec("update user set password_hash = ? where username = ?", hashedPassword, user.Username)
695 if err != nil {
696 serverError(w, err)
697 return
698 }
699 log.Printf("User %s reset password", user.Username)
700 http.Redirect(w, r, "/me", http.StatusSeeOther)
701 return
702 } else {
703 data.Error = "That's not your current password"
704 }
705 }
706 err := t.ExecuteTemplate(w, "reset_pass.html", data)
707 if err != nil {
708 serverError(w, err)
709 return
710 }
711 }
712}
713
714func adminUserHandler(w http.ResponseWriter, r *http.Request) {
715 user := getAuthUser(r)
716 if r.Method == "POST" {
717 if !user.IsAdmin {
718 renderDefaultError(w, http.StatusForbidden)
719 return
720 }
721 components := strings.Split(r.URL.Path, "/")
722 if len(components) < 5 {
723 renderError(w, "Invalid action", http.StatusBadRequest)
724 return
725 }
726 userName := components[3]
727 action := components[4]
728 var err error
729 if action == "activate" {
730 err = activateUser(userName)
731 } else if action == "impersonate" {
732 if user.ImpersonatingUser != "" {
733 // Don't allow nested impersonation
734 renderError(w, "Cannot nest impersonation, log out from impersonated user first.", 400)
735 return
736 }
737 session, _ := SessionStore.Get(r, "cookie-session")
738 session.Values["auth_user"] = userName
739 session.Values["impersonating_user"] = user.Username
740 session.Save(r, w)
741 log.Printf("User %s impersonated %s", user.Username, userName)
742 http.Redirect(w, r, "/", http.StatusSeeOther)
743 return
744 }
745 if err != nil {
746 log.Println(err)
747 renderDefaultError(w, http.StatusInternalServerError)
748 return
749 }
750 http.Redirect(w, r, "/admin", http.StatusSeeOther)
751 }
752}
753
754func checkDomainHandler(w http.ResponseWriter, r *http.Request) {
755 domain := r.URL.Query().Get("domain")
756 if domain != "" && domains[domain] != "" {
757 w.Write([]byte(domain))
758 return
759 }
760 if domain == c.Host || strings.HasSuffix(domain, "."+c.Host) {
761 w.Write([]byte(domain))
762 }
763 http.Error(w, "Not Found", 404)
764}
765
766func runHTTPServer() {
767 log.Printf("Running http server with hostname %s on port %d.", c.Host, c.HttpPort)
768 var err error
769 t = template.New("main").Funcs(template.FuncMap{
770 "unixTime": time.Unix,
771 "parent": path.Dir, "hasSuffix": strings.HasSuffix,
772 "safeGeminiURL": func(u string) template.URL {
773 if strings.HasPrefix(u, "gemini://") {
774 return template.URL(u)
775 }
776 return ""
777 }})
778 t, err = t.ParseGlob(path.Join(c.TemplatesDirectory, "*.html"))
779 if err != nil {
780 log.Fatal(err)
781 }
782 serveMux := http.NewServeMux()
783
784 s := strings.SplitN(c.Host, ":", 2)
785 hostname := s[0]
786
787 serveMux.HandleFunc(hostname+"/", rootHandler)
788 serveMux.HandleFunc(hostname+"/my_site", mySiteHandler)
789 serveMux.HandleFunc(hostname+"/me", myAccountHandler)
790 serveMux.HandleFunc(hostname+"/my_site/flounder-archive.zip", archiveHandler)
791 serveMux.HandleFunc(hostname+"/admin", adminHandler)
792 serveMux.HandleFunc(hostname+"/edit/", editFileHandler)
793 serveMux.HandleFunc(hostname+"/upload", uploadFilesHandler)
794 serveMux.Handle(hostname+"/login", limit(http.HandlerFunc(loginHandler)))
795 serveMux.Handle(hostname+"/register", limit(http.HandlerFunc(registerHandler)))
796 serveMux.HandleFunc(hostname+"/logout", logoutHandler)
797 serveMux.HandleFunc(hostname+"/delete/", deleteFileHandler)
798 serveMux.HandleFunc(hostname+"/delete-account", deleteAccountHandler)
799 serveMux.HandleFunc(hostname+"/reset-password", resetPasswordHandler)
800
801 // Used by Caddy
802 serveMux.HandleFunc(hostname+"/check-domain", checkDomainHandler)
803
804 // admin commands
805 serveMux.HandleFunc(hostname+"/admin/user/", adminUserHandler)
806
807 wrapped := handlers.CustomLoggingHandler(log.Writer(), serveMux, logFormatter)
808
809 // handle user files based on subdomain or custom domains
810 // also routes to proxy
811 serveMux.HandleFunc("proxy."+hostname+"/", proxyGemini)
812 serveMux.HandleFunc("/", userFile)
813 srv := &http.Server{
814 ReadTimeout: 5 * time.Second,
815 WriteTimeout: 10 * time.Second,
816 IdleTimeout: 120 * time.Second,
817 Addr: fmt.Sprintf(":%d", c.HttpPort),
818 Handler: wrapped,
819 }
820 log.Fatal(srv.ListenAndServe())
821}