blob: c23ce535346838fcae3080c52b302ce7dfdcb388 [file] [log] [blame]
David K. Bainbridgedf9df632016-07-07 18:47:46 -07001// 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.
gunjan56e19d272016-07-07 14:23:26 -070014package main
15
16import (
gunjan56e19d272016-07-07 14:23:26 -070017 "fmt"
David K. Bainbridgec04fd552016-11-08 18:39:29 -080018 "net/http"
19
20 "github.com/Sirupsen/logrus"
21
gunjan54fbb81c2016-07-09 03:31:27 -070022 "github.com/gorilla/mux"
23 "github.com/kelseyhightower/envconfig"
gunjan56e19d272016-07-07 14:23:26 -070024)
25
gunjan54fbb81c2016-07-09 03:31:27 -070026type Config struct {
David K. Bainbridgec04fd552016-11-08 18:39:29 -080027 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"`
gunjan54fbb81c2016-07-09 03:31:27 -070034
David K. Bainbridgec04fd552016-11-08 18:39:29 -080035 connect string
gunjan54fbb81c2016-07-09 03:31:27 -070036}
37
38var c Config
David K. Bainbridgec04fd552016-11-08 18:39:29 -080039var log = logrus.New()
gunjan54fbb81c2016-07-09 03:31:27 -070040
gunjan56e19d272016-07-07 14:23:26 -070041func main() {
gunjan54fbb81c2016-07-09 03:31:27 -070042
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. Bainbridgec04fd552016-11-08 18:39:29 -080048 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
gunjan54fbb81c2016-07-09 03:31:27 -070076 router := mux.NewRouter()
David K. Bainbridgec04fd552016-11-08 18:39:29 -080077 router.HandleFunc("/config/", c.configGenHandler).Methods("POST")
gunjan54fbb81c2016-07-09 03:31:27 -070078 http.Handle("/", router)
79
David K. Bainbridgec04fd552016-11-08 18:39:29 -080080 c.connect = fmt.Sprintf(c.Controller, c.Username, c.Password)
gunjan54fbb81c2016-07-09 03:31:27 -070081
David K. Bainbridgec04fd552016-11-08 18:39:29 -080082 panic(http.ListenAndServe(fmt.Sprintf(":%d", c.Port), nil))
gunjan56e19d272016-07-07 14:23:26 -070083}