-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (73 loc) · 2.57 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Copyright 2023, Staffbase GmbH and contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"net/http"
"os"
"github.com/Staffbase/spark-submit/pkg/handlers"
"github.com/Staffbase/spark-submit/pkg/spark"
"github.com/alecthomas/kong"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type mainCmd struct {
SparkHome string `required:"" default:"/opt/spark" help:"spark home directory" env:"SPARK_HOME"`
SparkPresetDir string `required:"" help:"directory with spark configuration presets" env:"SPARK_PRESET_DIR"`
Master string `required:"" help:"spark master address" env:"SPARK_MASTER"`
DebugSubmit bool `help:"write spark-submit output to logger" env:"DEBUG_SPARK_SUBMIT"`
DevMode bool `help:"sets the logger output to development config"`
Debug bool `help:"enables debug logs" env:"DEBUG"`
}
var CLI struct {
Main mainCmd `cmd:"" default:"withargs" help:"start the web-server"`
}
func main() {
kong.Parse(&CLI)
CLI.Main.Run()
}
func (cmd *mainCmd) Run() {
cmd.setupLogger()
s, err := spark.New(cmd.SparkHome, cmd.SparkPresetDir, cmd.Master, cmd.DebugSubmit)
if err != nil {
zap.L().Fatal("couldn't initialize spark dependency", zap.Error(err))
}
r := chi.NewRouter()
r.Get("/health", handlers.HandleHealth)
r.Handle("/metrics", promhttp.Handler())
r.Post("/", handlers.HandleSubmit(s))
r.Get("/", handlers.HandleStatus(s))
r.Delete("/", handlers.HandleKill(s))
zap.L().Info("start http server on port 7070")
if err := http.ListenAndServe(":7070", r); err != nil {
zap.L().Fatal("couldn't start webserver", zap.Error(err))
}
}
func (cmd mainCmd) setupLogger() {
config := zap.NewProductionConfig()
if cmd.DevMode {
config = zap.NewDevelopmentConfig()
}
if cmd.Debug {
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
}
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if logger, err := config.Build(); err != nil {
fmt.Printf("unable to setup zap logger %s\n", err)
os.Exit(1)
} else {
zap.ReplaceGlobals(logger)
}
}