This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
160 lines (139 loc) · 3.76 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
package main
import (
pb "github.com/OUCC/SyaroNote/syaro/gitservice"
"github.com/OUCC/SyaroNote/syaro/markdown"
"html/template"
"os"
"os/exec"
"path/filepath"
"strconv"
"time"
)
const (
PUBLIC_DIR = "public"
TEMPLATE_DIR = "template"
ENV_SYARODIR = "SYARODIR"
)
var (
version string
tmpl *template.Template
)
func main() {
parseFlags()
setupLogger()
// print welcome message
log.Notice("===== Syaro Wiki Server %s =====", version)
loadYaml()
findsyaroDir()
if setting.syaroDir == "" {
log.Fatal("Error: Can't find template file directory.")
}
log.Notice("WikiRoot: %s", setting.wikiRoot)
log.Notice("Syaro dir: %s", setting.syaroDir)
if setting.fcgi {
log.Notice("Fast CGI mode: ON")
} else {
log.Notice("Fast CGI mode: OFF")
}
log.Notice("Port: %d", setting.port)
log.Notice("URL prefix: %s", setting.urlPrefix)
if setting.gitMode {
log.Info("Loading Git plugin...")
cmd := exec.Command(filepath.Join(setting.syaroDir, "gitplugin"),
":"+strconv.Itoa(setting.port+1), setting.wikiRoot)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func() {
if err := cmd.Run(); err != nil { // blocking
log.Fatalf("Git plugin unexpectedly crashed: %s", err)
}
log.Fatal("Git plugin unexpectedly crashed")
}()
defer cmd.Process.Kill()
log.Notice("Git mode: ON")
} else {
log.Notice("Git mode: OFF")
}
log.Notice("Verbose output: %t\n", setting.verbose)
log.Notice("MathJax: %t", setting.Markdown.MathJax)
log.Notice("Highlight: %t", setting.Markdown.Highlight)
log.Notice("Emoji: %t", setting.Markdown.Emoji)
log.Info("Parsing template...")
err := setupViews()
if err != nil {
log.Fatalf("Failed to parse template: %s", err)
}
log.Info("Template parsed")
log.Info("Setting up fs watcher...")
go fsWatcher()
log.Info("Setting up file index...")
go indexLoop()
// log.Info("Setting up websockets...") // TODO
// set link worker
markdown.LinkWorker = linkWorker
startServer()
}
// findTemplateDir finds template directory contains html, css, etc...
// dir is directory specified by user as template dir.
// This search several directory and return right dir.
// If not found, return empty string.
func findsyaroDir() {
// if syaro dir is specified by user, search this dir
env := os.Getenv(ENV_SYARODIR)
if env != "" {
_, err := os.Stat(filepath.Join(env, TEMPLATE_DIR))
// if directory isn't exist
if err != nil {
log.Error("Can't find template file dir specified by env")
setting.syaroDir = ""
return
}
setting.syaroDir = env
} else { // directory isn't specified by user so search it by myself
paths := []string{
".",
"/usr/share/syaro",
"/usr/local/share/syaro",
`\Program Files\Syaro`,
}
for _, path := range paths {
_, err := os.Stat(filepath.Join(path, TEMPLATE_DIR))
if err == nil {
setting.syaroDir = path
return
}
}
// can't find syaro dir
setting.syaroDir = ""
return
}
}
func setupViews() error {
// funcs for template
tmpl = template.New("").Funcs(template.FuncMap{
"op": func(op pb.Change_Op) string {
switch op {
case pb.Change_OpAdd:
return "Add"
case pb.Change_OpUpdate:
return "Edit"
case pb.Change_OpRename:
return "Rename"
}
return ""
},
"timef": func(t time.Time) string { return t.Format("Mon _2 Jan 2006") },
"urlPrefix": func() string { return setting.urlPrefix },
"gitmode": func() bool { return setting.gitMode },
"search": func() bool { return setting.search },
"mathjax": func() bool { return setting.Markdown.MathJax },
"highlight": func() bool { return setting.Markdown.Highlight },
"emoji": func() bool { return setting.Markdown.Emoji },
})
var err error
tmpl, err = tmpl.ParseGlob(filepath.Join(setting.syaroDir, TEMPLATE_DIR, "*.html"))
if err != nil {
return err
}
return nil
}