-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (91 loc) · 2.97 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
package main
import (
"fmt"
"janic0/gemserv/actions"
"janic0/gemserv/generators"
"janic0/gemserv/utils"
"janic0/gemserv/views"
"log"
"strconv"
"strings"
"time"
"github.com/cvhariharan/gemini-server"
"github.com/google/uuid"
)
func main() {
gemini.HandleFunc("/", func(w *gemini.Response, r *gemini.Request) {
start := time.Now()
defer fmt.Println(start.String(), "\t", w.Body.RemoteAddr().String(), "\t", "\t", r.URL.String(), "\t", time.Since(start).Microseconds(), "ms")
adminUrl := fmt.Sprintf("/%s/admin", utils.AdminSession)
if r.URL.Path == "/admin" {
actions.Login(w, r)
} else if strings.HasPrefix(r.URL.Path, adminUrl) || strings.HasPrefix(r.URL.Path, fmt.Sprintf("/%s/admin", utils.LastAdminSession)) {
utils.LastAdminSession = utils.AdminSession
utils.AdminSession = uuid.NewString()
path := utils.SafeTrimRight(r.URL.Path[len(adminUrl):])
if strings.TrimLeft(path, "/") == "" {
views.AdminDashboard(w, r)
} else if path == "/add-page" {
actions.AddPage(w, r)
} else if strings.HasPrefix(path, "/edit-page") {
relativePath := path[len("/edit-page"):]
if relativePath == "" {
relativePath = "/"
}
views.EditPage(w, r, relativePath)
} else if strings.HasPrefix(path, "/page/") {
pageSuffixes := map[string]func(w *gemini.Response, r *gemini.Request, path string){
"/enable": actions.EnablePage,
"/disable": actions.DisablePage,
"/move": actions.MovePage,
"/duplicate": actions.DuplicatePage,
"/delete": actions.DeletePage,
"/add-block": actions.AddBlock,
}
for suffix, handler := range pageSuffixes {
if strings.HasSuffix(path, suffix) {
page := path[len("/page") : len(path)-len(suffix)]
handler(w, r, page)
return
}
}
// Edit block
blockSuffixes := map[string]func(w *gemini.Response, r *gemini.Request, path string, block int64){
"/edit": actions.EditBlock,
"/insert-above": actions.AddBlockAbove,
"/remove": actions.DelteBlock,
}
for suffix, handler := range blockSuffixes {
if strings.HasSuffix(path, suffix) {
segments := strings.Split(path, "/")
if segments[len(segments)-3] != "blocks" {
return
}
pagePath := "/" + strings.Join(segments[2:len(segments)-3], "/")
blockIndexStr := segments[len(segments)-2]
blockIndex, err := strconv.Atoi(blockIndexStr)
if err != nil {
w.SetStatus(gemini.StatusTemporaryFailure, "Invalid URL parameter")
}
handler(w, r, pagePath, int64(blockIndex))
return
}
}
} else {
w.SetStatus(51, "Page not found.")
w.SendStatus()
}
} else {
truncatedPath := utils.SafeTrimRight(r.URL.Path)
content, err := generators.GetPage(truncatedPath)
if err != nil {
w.SetStatus(51, "Page not found.")
w.SendStatus()
return
}
w.SetStatus(gemini.StatusSuccess, "text/gemini")
w.Write(content)
}
})
log.Fatal(gemini.ListenAndServeTLS(":1965", "cert.crt", "cert.key"))
}