This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
forked from dustin-decker/saml-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
75 lines (67 loc) · 1.84 KB
/
server.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
package main
import (
"flag"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"time"
)
type ServerConfig struct {
ListenInterface string `yaml:"listen_interface"`
ListenPort int `yaml:"listen_port"`
CertPath string `yaml:"cert_path"`
KeyPath string `yaml:"key_path"`
CookieMaxAge time.Duration `yaml:"cookie_max_age"`
LogLevel string `yaml:"log_level"`
Hosts []HostConfig `yaml:"hosts"`
}
type Server struct {
config ServerConfig
Echo *echo.Echo
}
func (C *ServerConfig) getConf(configPath string) {
yamlFile, err := ioutil.ReadFile(configPath)
if err != nil {
log.WithFields(log.Fields{
"config_path": configPath,
"error": err.Error()}).Fatal("could not read config")
}
err = yaml.Unmarshal(yamlFile, C)
if err != nil {
log.WithFields(log.Fields{
"config_path": configPath,
"error": err.Error()}).Fatal("could not parse config")
}
}
func NewServer() *Server {
var configPath string
flag.StringVar(&configPath, "c", "config.yaml", "path to the config file")
flag.Parse()
var C ServerConfig
absPath, err := filepath.Abs(configPath)
if err != nil {
log.WithFields(log.Fields{
"config_path": configPath,
"error": err.Error()}).Fatal("could not determine absolute path for config")
}
C.getConf(absPath)
log.Print("config loaded")
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
logLevel, err := log.ParseLevel(C.LogLevel)
if err != nil {
log.WithFields(log.Fields{
"log_level": C.LogLevel,
"error": err.Error()}).Fatal("could not parse log level")
}
log.SetLevel(logLevel)
e := echo.New()
e.Use(middleware.Recover())
e.Use(middleware.Logger())
s := Server{config: C, Echo: e}
return &s
}