blob: b2c56ec2df0ffc82ae18740cc55af47045422e3c [file] [log] [blame]
Jonathan Hartf86817b2018-08-17 10:35:54 -07001// Copyright 2018 Open Networking Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package main
15
16import (
17 "flag"
18 "fmt"
19 "net/http"
20 "os"
21
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080022 "github.com/sirupsen/logrus"
Jonathan Hartf86817b2018-08-17 10:35:54 -070023
24 "github.com/gorilla/mux"
25 "github.com/kelseyhightower/envconfig"
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080026 lkh "github.com/gfremex/logrus-kafka-hook"
Jonathan Hartf86817b2018-08-17 10:35:54 -070027)
28
29const appName = "SADISSERVER"
30
31type Config struct {
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080032 Port int `default:"8000" desc:"port on which to listen for requests"`
33 Xos string `default:"127.0.0.1:8181" desc:"connection string with which to connect to XOS"`
34 Username string `default:"admin@opencord.org" desc:"username with which to connect to XOS"`
35 Password string `default:"letmein" desc:"password with which to connect to XOS"`
36 LogLevel string `default:"info" envconfig:"LOG_LEVEL" desc:"detail level for logging"`
37 LogFormat string `default:"text" envconfig:"LOG_FORMAT" desc:"log output format, text or json"`
38 KafkaBroker string `default:"" desc:"url of the kafka broker"`
Jonathan Hartf86817b2018-08-17 10:35:54 -070039
40 connect string
41}
42
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080043var logger = logrus.New()
44var log *logrus.Entry
Jonathan Hartf86817b2018-08-17 10:35:54 -070045var appFlags = flag.NewFlagSet("", flag.ContinueOnError)
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080046var config Config
Jonathan Hartf86817b2018-08-17 10:35:54 -070047
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080048func init() {
49 config = Config{}
Jonathan Hartf86817b2018-08-17 10:35:54 -070050 appFlags.Usage = func() {
51 envconfig.Usage(appName, &config)
52 }
53 if err := appFlags.Parse(os.Args[1:]); err != nil {
54 if err != flag.ErrHelp {
55 os.Exit(1)
56 } else {
57 return
58 }
59 }
60
61 err := envconfig.Process(appName, &config)
62 if err != nil {
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080063 logger.Fatalf("[ERROR] Unable to parse configuration options : %s", err)
Jonathan Hartf86817b2018-08-17 10:35:54 -070064 }
65
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080066 if len(config.KafkaBroker) > 0 {
67 logger.Debug("Setting up kafka integration")
68 hook, err := lkh.NewKafkaHook(
69 "kh",
70 []logrus.Level{logrus.DebugLevel, logrus.InfoLevel, logrus.WarnLevel, logrus.ErrorLevel},
71 &logrus.JSONFormatter{
72 FieldMap: logrus.FieldMap{
73 logrus.FieldKeyTime: "@timestamp",
74 logrus.FieldKeyLevel: "level",
75 logrus.FieldKeyMsg: "message",
76 },
77 },
78 []string{config.KafkaBroker},
79 )
80
81 if err != nil {
82 logger.Error(err)
Jonathan Hartf86817b2018-08-17 10:35:54 -070083 }
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080084
85 logger.Hooks.Add(hook)
Jonathan Hartf86817b2018-08-17 10:35:54 -070086 }
87
88 level, err := logrus.ParseLevel(config.LogLevel)
89 if err != nil {
90 level = logrus.WarnLevel
91 }
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080092 logger.Level = level
Jonathan Hartf86817b2018-08-17 10:35:54 -070093
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080094 switch config.LogFormat {
95 case "json":
96 logger.Formatter = &logrus.JSONFormatter{}
97 default:
98 logger.Formatter = &logrus.TextFormatter{
99 FullTimestamp: true,
100 ForceColors: true,
101 }
102 }
103
104 log = logger.WithField("topics", []string{"sadis-server.log"})
105}
106
107func main() {
108
109
110
111 log.WithFields(logrus.Fields{
112 "PORT": config.Port,
113 "XOS": config.Xos,
114 "USERNAME": config.Username,
115 "PASSWORD": config.Password,
116 "LOG_LEVEL": config.LogLevel,
117 "LOG_FORMAT": config.LogFormat,
118 "KAFKA_BROKER": config.KafkaBroker,
119 }).Infof(`Sadis-server started`)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700120
121 router := mux.NewRouter()
122 router.HandleFunc("/subscriber/{id}", config.getSubscriberHandler)
123 http.Handle("/", router)
124
125 connectStringFormat := "http://%s:%s@%s"
126 config.connect = fmt.Sprintf(connectStringFormat, config.Username, config.Password, config.Xos)
127
128 panic(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
129}