David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Laboratory |
| 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. |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 17 | "fmt" |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 18 | "net/http" |
| 19 | |
| 20 | "github.com/Sirupsen/logrus" |
| 21 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 22 | "github.com/gorilla/mux" |
| 23 | "github.com/kelseyhightower/envconfig" |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 26 | type Config struct { |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 27 | Port int `default:"1337"` |
| 28 | Listen string `default:"0.0.0.0"` |
| 29 | Controller string `default:"http://%s:%s@127.0.0.1:8181"` |
| 30 | Username string `default:"karaf"` |
| 31 | Password string `default:"karaf"` |
| 32 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 33 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 34 | |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 35 | connect string |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var c Config |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 39 | var log = logrus.New() |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 40 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 41 | func main() { |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 42 | |
| 43 | err := envconfig.Process("CONFIGGEN", &c) |
| 44 | if err != nil { |
| 45 | log.Fatalf("[ERROR] Unable to parse configuration options : %s", err) |
| 46 | } |
| 47 | |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 48 | switch c.LogFormat { |
| 49 | case "json": |
| 50 | log.Formatter = &logrus.JSONFormatter{} |
| 51 | default: |
| 52 | log.Formatter = &logrus.TextFormatter{ |
| 53 | FullTimestamp: true, |
| 54 | ForceColors: true, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | level, err := logrus.ParseLevel(c.LogLevel) |
| 59 | if err != nil { |
| 60 | level = logrus.WarnLevel |
| 61 | } |
| 62 | log.Level = level |
| 63 | |
| 64 | log.Infof(`Configuration: |
| 65 | LISTEN: %s |
| 66 | PORT: %d |
| 67 | CONTROLLER: %s |
| 68 | USERNAME: %s |
| 69 | PASSWORD: %s |
| 70 | LOG_LEVEL: %s |
| 71 | LOG_FORMAT: %s`, |
| 72 | c.Listen, c.Port, c.Controller, |
| 73 | c.Username, c.Password, |
| 74 | c.LogLevel, c.LogFormat) |
| 75 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 76 | router := mux.NewRouter() |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 77 | router.HandleFunc("/config/", c.configGenHandler).Methods("POST") |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 78 | http.Handle("/", router) |
| 79 | |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 80 | c.connect = fmt.Sprintf(c.Controller, c.Username, c.Password) |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 81 | |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 82 | panic(http.ListenAndServe(fmt.Sprintf(":%d", c.Port), nil)) |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 83 | } |