blob: a7e1bdce8d312fdab10cfd24119b4d26537a617a [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
22 "github.com/Sirupsen/logrus"
23
24 "github.com/gorilla/mux"
25 "github.com/kelseyhightower/envconfig"
26)
27
28const appName = "SADISSERVER"
29
30type Config struct {
31 Port int `default:"8000" desc:"port on which to listen for requests"`
32 Xos string `default:"127.0.0.1:8181" desc:"connection string with which to connect to XOS"`
33 Username string `default:"admin@opencord.org" desc:"username with which to connect to XOS"`
34 Password string `default:"letmein" desc:"password with which to connect to XOS"`
35 LogLevel string `default:"info" envconfig:"LOG_LEVEL" desc:"detail level for logging"`
36 LogFormat string `default:"text" envconfig:"LOG_FORMAT" desc:"log output format, text or json"`
37
38 connect string
39}
40
41var log = logrus.New()
42var appFlags = flag.NewFlagSet("", flag.ContinueOnError)
43
44func main() {
45 config := Config{}
46 appFlags.Usage = func() {
47 envconfig.Usage(appName, &config)
48 }
49 if err := appFlags.Parse(os.Args[1:]); err != nil {
50 if err != flag.ErrHelp {
51 os.Exit(1)
52 } else {
53 return
54 }
55 }
56
57 err := envconfig.Process(appName, &config)
58 if err != nil {
59 log.Fatalf("[ERROR] Unable to parse configuration options : %s", err)
60 }
61
62 switch config.LogFormat {
63 case "json":
64 log.Formatter = &logrus.JSONFormatter{}
65 default:
66 log.Formatter = &logrus.TextFormatter{
67 FullTimestamp: true,
68 ForceColors: true,
69 }
70 }
71
72 level, err := logrus.ParseLevel(config.LogLevel)
73 if err != nil {
74 level = logrus.WarnLevel
75 }
76 log.Level = level
77
78 log.Infof(`Configuration:
79 PORT: %d
80 XOS: %s
81 USERNAME: %s
82 PASSWORD: %s
83 LOG_LEVEL: %s
84 LOG_FORMAT: %s`,
85 config.Port, config.Xos,
86 config.Username, config.Password,
87 config.LogLevel, config.LogFormat)
88
89 router := mux.NewRouter()
90 router.HandleFunc("/subscriber/{id}", config.getSubscriberHandler)
91 http.Handle("/", router)
92
93 connectStringFormat := "http://%s:%s@%s"
94 config.connect = fmt.Sprintf(connectStringFormat, config.Username, config.Password, config.Xos)
95
96 panic(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
97}