blob: 99bdd112bcfb45ddf8b685144052a6dd35b03901 [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
Matteo Scandolo60676562019-03-15 14:56:25 -070024 lkh "github.com/gfremex/logrus-kafka-hook"
Jonathan Hartf86817b2018-08-17 10:35:54 -070025 "github.com/gorilla/mux"
26 "github.com/kelseyhightower/envconfig"
27)
28
29const appName = "SADISSERVER"
30
31type Config struct {
Matteo Scandolo60676562019-03-15 14:56:25 -070032 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"`
Matteo Scandoloddaa32c2019-05-22 10:55:45 -070036 LogLevel string `default:"debug" envconfig:"LOG_LEVEL" desc:"detail level for logging"`
Matteo Scandolo60676562019-03-15 14:56:25 -070037 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 Scandolo60676562019-03-15 14:56:25 -070048func init() {
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080049 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
Matteo Scandolo9a2772a2018-11-19 14:56:26 -0800109 log.WithFields(logrus.Fields{
Matteo Scandolo60676562019-03-15 14:56:25 -0700110 "PORT": config.Port,
111 "XOS": config.Xos,
112 "USERNAME": config.Username,
113 "PASSWORD": config.Password,
114 "LOG_LEVEL": config.LogLevel,
115 "LOG_FORMAT": config.LogFormat,
Matteo Scandolo9a2772a2018-11-19 14:56:26 -0800116 "KAFKA_BROKER": config.KafkaBroker,
117 }).Infof(`Sadis-server started`)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700118
119 router := mux.NewRouter()
120 router.HandleFunc("/subscriber/{id}", config.getSubscriberHandler)
Matteo Scandolo60676562019-03-15 14:56:25 -0700121 router.HandleFunc("/bandwidthprofiles/{id}", config.getBandwidthProfileHandler)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700122 http.Handle("/", router)
123
124 connectStringFormat := "http://%s:%s@%s"
125 config.connect = fmt.Sprintf(connectStringFormat, config.Username, config.Password, config.Xos)
126
127 panic(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
128}