-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (53 loc) · 1.55 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
package main
import (
"flag"
"fmt"
"github.com/NickPresta/handlers" // My fork of gorilla/handlers with Apache Combined Log Format
"github.com/NickPresta/scribe/goscribe"
"github.com/gorilla/mux"
"io"
"log"
"net/http"
"os"
"runtime"
)
var (
port = flag.Int("port", 8080, "HTTP listen port")
binary = flag.String("binary", "", "PhantomJS binary location")
script = flag.String("script", "", "PhantomJS script location")
httpLogLocation = flag.String("log", "/tmp/scribe.log", "HTTP log file")
httpLogger io.Writer
)
func stderr(s interface{}) {
fmt.Fprintln(os.Stderr, s)
}
func setupLogging(location string) {
var err error
httpLogger, err = os.OpenFile(location, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
log.Fatal(err)
}
}
func main() {
flag.Parse()
if *binary == "" || *script == "" {
stderr("Missing binary or script argument")
stderr("goscribed -binary BINARY -script SCRIPT [-port PORT]\nFlags:")
flag.PrintDefaults()
os.Exit(1)
}
log.Printf("Now serving on http://localhost:%d\n", *port)
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
log.Printf("Setting GOMAXPROCS to %d", numCPU)
goscribe.SetPDFBinaryLocation(*binary)
goscribe.SetPDFScriptLocation(*script)
setupLogging(*httpLogLocation)
router := mux.NewRouter()
router.HandleFunc("/", goscribe.RequestHandler).Methods("GET")
http.Handle("/", router)
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), handlers.CombinedLoggingHandler(httpLogger, http.DefaultServeMux))
if err != nil {
log.Fatal(err)
}
}