forked from libreofficedocker/unoserver-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unoserver-rest-api.go
82 lines (72 loc) · 1.86 KB
/
unoserver-rest-api.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
package main
import (
"log"
"net"
"os"
"time"
"github.com/libreoffice-docker/unoserver-rest-api/api"
"github.com/libreoffice-docker/unoserver-rest-api/depot"
"github.com/libreoffice-docker/unoserver-rest-api/unoconvert"
"github.com/urfave/cli"
)
var Version = "unstable"
func init() {
log.SetPrefix("unoserver-rest-api ")
}
func main() {
app := cli.NewApp()
app.Name = "unoserver-rest-api"
app.Version = Version
app.Usage = "The simple REST API for unoserver and unoconvert"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "addr",
Value: "0.0.0.0:2004",
Usage: "The addr used by the unoserver api server",
},
cli.StringFlag{
Name: "unoserver-addr",
Value: "127.0.0.1:2002",
Usage: "The unoserver addr used by the unoconvert",
EnvVar: "UNOSERVER_ADDR",
},
cli.StringFlag{
Name: "unoconvert-bin",
Value: "unoconvert",
Usage: "Set the unoconvert executable path.",
EnvVar: "UNOCONVERT_BIN",
},
cli.DurationFlag{
Name: "unoconvert-timeout",
Value: 0 * time.Minute,
Usage: "Set the unoconvert run timeout",
EnvVar: "UNOCONVERT_TIMEOUT",
},
}
app.Authors = []cli.Author{
{
Name: "libreoffice-docker",
Email: "https://github.com/libreoffice-docker/unoserver-rest-api",
},
}
app.Action = mainAction
if err := app.Run(os.Args); err != nil {
os.Exit(1)
}
}
func mainAction(c *cli.Context) {
// Create temporary working directory
depot.MkdirTemp()
// Cleanup temporary working directory after finished
defer depot.CleanTemp()
// Configure unoconvert options
unoAddr := c.String("unoserver-addr")
host, port, _ := net.SplitHostPort(unoAddr)
unoconvert.SetInterface(host)
unoconvert.SetPort(port)
unoconvert.SetExecutable(c.String("unoconvert-bin"))
unoconvert.SetContextTimeout(c.Duration("unoconvert-timeout"))
// Start the API server
addr := c.String("addr")
api.ListenAndServe(addr)
}