This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tikad.go
152 lines (124 loc) · 3.56 KB
/
tikad.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/dannav/starship/services/cmd/tikad/handlers"
"github.com/google/go-tika/tika"
_ "github.com/lib/pq" // postgres sql driver
log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
)
const (
// readTimeout is timeout for reading the request
readTimeout = 30 * time.Second
// writeTimeout is timeout for reading the response
writeTimeout = 30 * time.Second
// shutdownTimeout is the timeout for shutdown
shutdownTimeout = 30 * time.Second
// defaultAppPort is the port that the application listens on if not PORT env is set
defaultAppPort = 8080
// appPortEnv is the name of the environment variable that contains the port to run searchd on
appPortEnv = "PORT"
// tikaVersion is the version of apache tika to download
tikaVersion = "1.16"
// tikaPort is the port that apache tika runs on
tikaPort = "9998"
)
func main() {
var runPort int
// if there was an error, exit main with non-zero status code.
var mainErr error
defer func() {
if mainErr != nil {
log.WithFields(log.Fields{
"error": mainErr,
}).Error("error in main")
os.Exit(1)
}
}()
// parse app config from environment variables
setPort := os.Getenv(appPortEnv)
if setPort == "" {
runPort = defaultAppPort
} else {
p, err := strconv.Atoi(setPort)
if err != nil {
mainErr = errors.Errorf("cannot convert PORT environment variable to int %s", err.Error())
return
}
runPort = p
}
port := fmt.Sprintf(":%d", runPort)
// check if tika jar exists or download it
tikaJar := fmt.Sprintf("tika-server-%v.jar", tikaVersion)
if _, err := os.Stat(tikaJar); os.IsNotExist(err) {
log.Infof("apache tika not found, downloading version %v", tikaVersion)
err := tika.DownloadServer(context.Background(), tika.Version116, tikaJar)
if err != nil {
mainErr = errors.Wrap(err, "downloading apache tika")
return
}
log.Info("apache tika downloaded successfully")
}
// force 30 second timeouts on all http requests
client := &http.Client{
Timeout: time.Second * 30,
}
opts := tika.WithPort(tikaPort)
s, err := tika.NewServer(tikaJar, opts)
if err != nil {
mainErr = errors.Wrap(err, "instantiating new apache tika server")
return
}
// start tika server
shutdownTika, err := s.Start(context.Background())
if err != nil {
mainErr = errors.Wrap(err, "starting apache tika")
return
}
// create the API
tikaClient := tika.NewClient(client, s.URL())
app := handlers.NewApp(tikaClient)
server := http.Server{
Addr: port,
Handler: app,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: 1 << 20,
}
// start the API server
serverErrors := make(chan error, 1)
go func() {
log.Infof("server started, listening on %s", port)
serverErrors <- server.ListenAndServe()
}()
// blocking main and waiting for shutdown
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
// wait for osSignal or error starting server
select {
case e := <-serverErrors:
mainErr = e
return
case <-osSignals:
}
// shutdown server
// create context for shutdown call
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
// shutdown apache tika
shutdownTika()
// asking listener to shutdown
if err := server.Shutdown(ctx); err != nil {
mainErr = errors.Wrapf(err, "shutdown: graceful shutdown did not complete in %v", shutdownTimeout)
if err := server.Close(); err != nil {
mainErr = errors.Wrapf(err, "shutdown: error killing server")
}
}
}