-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (54 loc) · 1.31 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
negronilogrus "github.com/meatballhat/negroni-logrus"
"github.com/sirupsen/logrus"
"github.com/urfave/negroni"
"github.com/henrywallace/mdpreview/server"
)
var (
addr = flag.String("addr", ":8080", "address to serve preview like :8080 or 0.0.0.0:7000")
api = flag.Bool("api", false, "whether to render via the Github API")
debug = flag.Bool("debug", false, "debug logging")
)
func main() {
flag.Parse()
log := logrus.New()
if *debug {
log.SetLevel(logrus.DebugLevel)
}
if len(os.Args) < 2 {
log.Fatal("path must be given")
}
path := os.Args[1]
if filepath.Ext(path) != ".md" {
log.Warnf("path %s doesn't look like a Markdown file", path)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatalf("path %s does not exist", path)
}
s, err := server.New(path, log, !*api)
if err != nil {
log.Fatal(err)
}
h, err := s.Run()
if err != nil {
log.Fatal(err)
}
if strings.HasPrefix(*addr, ":") {
*addr = fmt.Sprintf("127.0.0.1%s", *addr)
}
log.Info(fmt.Sprintf("Starting mdpreview server at %s", *addr))
n := negroni.New()
n.Use(negroni.NewRecovery())
n.Use(negronilogrus.NewMiddlewareFromLogger(log, "web"))
n.UseHandler(h)
if err := http.ListenAndServe(*addr, n); err != nil {
log.Fatal(err)
}
}