forked from go101/go101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go101-notembed.go
55 lines (44 loc) · 1.15 KB
/
go101-notembed.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
// +build !embed
package main
import (
"go/build"
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
)
var staticFilesHandler = http.FileServer(http.Dir(filepath.Join(rootPath, "web", "static")))
var resFilesHandler = http.FileServer(http.Dir(filepath.Join(rootPath, "articles", "res")))
func loadArticleFile(file string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(rootPath, "articles", file))
}
func parseTemplate(commonPath string, files ...string) *template.Template {
ts := make([]string, len(files))
for i, f := range files {
ts[i] = filepath.Join(rootPath, commonPath, f)
}
return template.Must(template.ParseFiles(ts...))
}
func updateGo101() {
pullGo101Project(rootPath)
}
//=================================
var rootPath = findGo101ProjectRoot()
func findGo101ProjectRoot() string {
if _, err := os.Stat(filepath.Join(".", "go101.go")); err == nil {
return "."
}
for _, name := range []string{
"gitlab.com/go101/go101",
"gitlab.com/Go101/go101",
"github.com/go101/go101",
"github.com/Go101/go101",
} {
pkg, err := build.Import(name, "", build.FindOnly)
if err == nil {
return pkg.Dir
}
}
return "."
}