This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
100 lines (83 loc) · 2.88 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"flag"
"fmt"
"github.com/jsiebens/faas-nomad/pkg/proxy"
"github.com/jsiebens/faas-nomad/pkg/resolver"
"log"
"net/http"
"os"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/jsiebens/faas-nomad/pkg/handlers"
"github.com/jsiebens/faas-nomad/pkg/services"
"github.com/jsiebens/faas-nomad/pkg/types"
"github.com/jsiebens/faas-nomad/version"
fbootstrap "github.com/openfaas/faas-provider"
ftypes "github.com/openfaas/faas-provider/types"
)
var (
configFile = flag.String("config", "", "Path to the configuration file.")
)
func main() {
flag.Parse()
config, err := types.LoadConfig(*configFile)
if err != nil {
log.Fatal(err)
}
logger := setupLogging(config.Log)
log.SetOutput(logger.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true}))
log.SetPrefix("")
log.SetFlags(0)
secrets, err := services.NewVaultSecrets(config.Vault)
if err != nil {
log.Fatal(err)
}
jobs, err := services.NewNomadJobs(config.Nomad)
if err != nil {
log.Fatal(err)
}
factory := services.NewJobFactory(config)
resolver, err := resolver.NewConsulResolver(config, logger)
if err != nil {
log.Fatal(err)
}
bootstrapHandlers := ftypes.FaaSHandlers{
FunctionProxy: proxy.NewHandlerFunc(config.FaaS, resolver, logger),
FunctionReader: handlers.MakeFunctionReader(config, jobs, logger),
DeployHandler: handlers.MakeDeployHandler(config, factory, jobs, secrets, logger),
DeleteHandler: handlers.MakeDeleteHandler(config, jobs, logger),
ReplicaReader: handlers.MakeReplicaReader(config, jobs, resolver, logger),
ReplicaUpdater: handlers.MakeReplicaUpdater(config, jobs, logger),
SecretHandler: handlers.MakeSecretHandler(secrets, logger),
LogHandler: unimplemented,
UpdateHandler: handlers.MakeDeployHandler(config, factory, jobs, secrets, logger),
HealthHandler: handlers.MakeHealthHandler(),
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
ListNamespaceHandler: handlers.MakeListNamespaceHandler(config),
}
logger.Info(fmt.Sprintf("Listening on TCP port: %d", *config.FaaS.TCPPort))
fbootstrap.Serve(&bootstrapHandlers, &config.FaaS)
}
func unimplemented(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
}
func setupLogging(config types.LogConfig) hclog.Logger {
appLogger := hclog.New(&hclog.LoggerOptions{
Name: "faas-nomad",
Level: hclog.LevelFromString(config.Level),
JSONFormat: strings.ToLower(config.Format) == "json",
Output: createLogFile(config),
})
return appLogger
}
func createLogFile(config types.LogConfig) *os.File {
if config.File != "" {
f, err := os.OpenFile(config.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err == nil {
return f
}
log.Printf("Unable to open file for output, defaulting to std out: %s\n", err.Error())
}
return os.Stdout
}