This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
84 lines (65 loc) · 1.72 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
flag "github.com/ogier/pflag"
)
const Usage = `Usage: gowiki [options...] <path>
Positional arguments:
path directory to serve wiki pages from
Optional arguments:
-h, --help show this help message and exit
-p PORT, --port=PORT listen port (default 8080)
--custom-css=PATH path to custom CSS file
`
var options struct {
Dir string
Port int
CustomCSS string
template *template.Template
git bool
}
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, Usage)
}
flag.IntVarP(&options.Port, "port", "p", 8080, "")
flag.StringVar(&options.CustomCSS, "custom-css", "", "")
flag.Parse()
options.Dir = flag.Arg(0)
if options.Dir == "" {
flag.Usage()
os.Exit(1)
}
log.Println("Serving wiki from", options.Dir)
// Parse base template
var err error
options.template, err = template.New("base").Parse(Template)
if err != nil {
log.Fatalln("Error parsing HTML template:", err)
}
// Trim trailing slash from root path
if strings.HasSuffix(options.Dir, "/") {
options.Dir = options.Dir[:len(options.Dir)-1]
}
// Verify that the wiki folder exists
_, err = os.Stat(options.Dir)
if os.IsNotExist(err) {
log.Fatalln("Directory not found")
}
// Check if the wiki folder is a Git repository
options.git = IsGitRepository(options.Dir)
if options.git {
log.Println("Git repository found in directory")
} else {
log.Println("No git repository found in directory")
}
http.Handle("/api/diff/", commonHandler(DiffHandler))
http.Handle("/", commonHandler(WikiHandler))
log.Println("Listening on:", options.Port)
http.ListenAndServe(fmt.Sprintf(":%d", options.Port), nil)
}