forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
103 lines (86 loc) · 2.06 KB
/
docs.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
package main
import (
"fmt"
"net"
"net/http"
"os"
)
var (
// Built-in documentation content, N sections
docs [3]string
)
/*
* Serve '/docs' page with a built-in documentation
*/
func docsHandler(w http.ResponseWriter, r *http.Request) {
// Get client's IP
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Error().Msg("Can't get IP for the docs page: " + err.Error())
return
}
// Check existing session
username, err := sessions.exists(w, r)
if err != nil {
log.Error().
Str("ip", ip).
Msg(err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// Get account from a database
account, err := db.getAccount(username)
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't GetAccount for the docs page: " + err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// All are admins in service's DEV mode
if config.Environment != "prod" {
account.Admin = true
}
templateData := &TemplateData{
Account: account,
Docs: docs,
}
renderTemplate(w, "docs", templateData, nil)
log.Info().
Str("ip", ip).
Str("username", username).
Msg("Docs page requested")
}
/*
* Load all documentation from the markdown files.
* One file for one documentation section
*/
func loadDocs() error {
for i, name := range []string{"ui", "search", "admin"} {
md, err := loadDoc(name)
if err != nil {
return err
}
docs[i] = md
}
log.Debug().Msg("Documentation is parsed")
return nil
}
/*
* Load documentation from a specific markdown file.
* Receives a name of file to load, its extension has to be ".md"
*/
func loadDoc(name string) (string, error) {
mdFile, err := os.Open(config.Docs + "/" + name + ".md")
if err != nil {
return "", fmt.Errorf("Failed to open '%s/%s.md' doc: %s", config.Docs, name, err.Error())
}
fi, _ := mdFile.Stat()
buffer := make([]byte, fi.Size())
_, err = mdFile.Read(buffer)
if err != nil {
return "", fmt.Errorf("Failed to read '%s/%s.md' doc: %s", config.Docs, name, err.Error())
}
return string(buffer), nil
}