Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 1 | // 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. |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "flag" |
| 18 | "fmt" |
| 19 | "net/http" |
| 20 | "os" |
| 21 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 22 | "github.com/sirupsen/logrus" |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 23 | |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 24 | lkh "github.com/gfremex/logrus-kafka-hook" |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 25 | "github.com/gorilla/mux" |
| 26 | "github.com/kelseyhightower/envconfig" |
| 27 | ) |
| 28 | |
| 29 | const appName = "SADISSERVER" |
| 30 | |
| 31 | type Config struct { |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 32 | 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 Scandolo | ddaa32c | 2019-05-22 10:55:45 -0700 | [diff] [blame^] | 36 | LogLevel string `default:"debug" envconfig:"LOG_LEVEL" desc:"detail level for logging"` |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 39 | |
| 40 | connect string |
| 41 | } |
| 42 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 43 | var logger = logrus.New() |
| 44 | var log *logrus.Entry |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 45 | var appFlags = flag.NewFlagSet("", flag.ContinueOnError) |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 46 | var config Config |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 47 | |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 48 | func init() { |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 49 | config = Config{} |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 50 | 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 Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 63 | logger.Fatalf("[ERROR] Unable to parse configuration options : %s", err) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 66 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 83 | } |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 84 | |
| 85 | logger.Hooks.Add(hook) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | level, err := logrus.ParseLevel(config.LogLevel) |
| 89 | if err != nil { |
| 90 | level = logrus.WarnLevel |
| 91 | } |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 92 | logger.Level = level |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 93 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 94 | 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 | |
| 107 | func main() { |
| 108 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 109 | log.WithFields(logrus.Fields{ |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 110 | "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 Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 116 | "KAFKA_BROKER": config.KafkaBroker, |
| 117 | }).Infof(`Sadis-server started`) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 118 | |
| 119 | router := mux.NewRouter() |
| 120 | router.HandleFunc("/subscriber/{id}", config.getSubscriberHandler) |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 121 | router.HandleFunc("/bandwidthprofiles/{id}", config.getBandwidthProfileHandler) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 122 | 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 | } |