github.com/raphaelreyna/latte@v0.11.2-0.20220317193248-98e2fcef4eef/cmd/latte/main.go (about) 1 package main 2 3 import ( 4 "log" 5 "net/http" 6 "os" 7 "os/exec" 8 "strconv" 9 10 "github.com/gorilla/handlers" 11 "github.com/raphaelreyna/latte/internal/server" 12 ) 13 14 const ( 15 // If cache sizes is not provided by environment, default to 15 for both 16 defaultTCS = 15 17 defaultRCS = 15 18 ) 19 20 var db server.DB 21 22 func main() { 23 var err error 24 errLog := log.New(os.Stderr, "ERROR: ", log.Lshortfile|log.LstdFlags) 25 infoLog := log.New(os.Stdout, "INFO: ", log.Lshortfile|log.LstdFlags) 26 27 // Check for pdfLaTeX (pdfTex will do in a pinch) 28 cmd := "pdflatex" 29 if _, err := exec.LookPath(cmd); err != nil { 30 errLog.Printf("error while searching checking pdflatex binary: %v\n\tchecking for pdftex binary", err) 31 if _, err := exec.LookPath("pdftex"); err != nil { 32 errLog.Fatal("neither pdflatex nor pdftex binary found in your $PATH") 33 } 34 infoLog.Printf("found pdftex binary; falling back to using pdftex instead of pdflatex") 35 cmd = "pdftex" 36 } 37 38 // If user provides a directory path or a tex file, then run as cli tool and not as http server 39 if len(os.Args) > 1 { 40 if os.Args[1] != "server" { 41 cli(errLog, infoLog) 42 os.Exit(0) 43 } 44 } 45 root := os.Getenv("LATTE_ROOT") 46 if root == "" { 47 root, err = os.UserCacheDir() 48 if err != nil { 49 errLog.Fatalf("error creating root cache directory: %v", err) 50 } 51 } 52 infoLog.Printf("root cache directory: %s", root) 53 54 tCacheSize := os.Getenv("LATTE_TMPL_CACHE_SIZE") 55 tcs, err := strconv.Atoi(tCacheSize) 56 if err != nil { 57 infoLog.Printf("couldn't pull templates cache size from environment: defaulting to %d", defaultTCS) 58 tcs = defaultTCS 59 } 60 s, err := server.NewServer(root, cmd, db, errLog, infoLog, tcs) 61 if err != nil { 62 errLog.Fatal(err) 63 } 64 65 port := os.Getenv("PORT") 66 if port == "" { 67 port = "27182" 68 } 69 infoLog.Printf("listening for HTTP traffic on port: %s ...", port) 70 errLog.Fatal(http.ListenAndServe(":"+port, handlers.CORS( 71 handlers.AllowedHeaders([]string{ 72 "Origin", 73 "X-Requested-With", 74 "Content-Type", 75 "Authorization", 76 "Access-Control-Allow-Origin", 77 "Access-Control-Request-Headers", 78 "Access-Control-Request-Method", 79 }), 80 handlers.AllowedMethods([]string{ 81 "GET", "POST", "PUT", 82 "HEAD", "OPTIONS", 83 }), 84 handlers.AllowedOrigins([]string{"*"}))(s)), 85 ) 86 }