blob: 03dc9e80cfcf3b5b582f2f85127bc1ba701a1d88 [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.
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070014package main
15
16import (
17 "fmt"
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070018 "github.com/Sirupsen/logrus"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070019 "github.com/gorilla/mux"
20 "github.com/kelseyhightower/envconfig"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070021 "net/http"
22)
23
24type Config struct {
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070025 Port int `default:"4242"`
26 Listen string `default:"0.0.0.0"`
27 Network string `default:"10.0.0.0/24"`
David K. Bainbridgeca68f062016-10-27 11:04:33 -070028 RangeLow string `default:"10.0.0.2" envconfig:"RANGE_LOW"`
29 RangeHigh string `default:"10.0.0.253" envconfig:"RANGE_HIGH"`
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070030 LogLevel string `default:"warning" envconfig:"LOG_LEVEL"`
31 LogFormat string `default:"text" envconfig:"LOG_FORMAT"`
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070032}
33
34type Context struct {
35 storage Storage
36}
37
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070038var log = logrus.New()
39
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070040func main() {
41 context := &Context{}
42
43 config := Config{}
44 err := envconfig.Process("ALLOCATE", &config)
45 if err != nil {
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070046 log.Fatalf("Unable to parse configuration options : %s", err)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070047 }
48
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070049 switch config.LogFormat {
50 case "json":
51 log.Formatter = &logrus.JSONFormatter{}
52 default:
53 log.Formatter = &logrus.TextFormatter{
54 FullTimestamp: true,
55 ForceColors: true,
56 }
57 }
58
59 level, err := logrus.ParseLevel(config.LogLevel)
60 if err != nil {
61 level = logrus.WarnLevel
62 }
63 log.Level = level
64
65 log.Infof(`Configuration:
David K. Bainbridgeca68f062016-10-27 11:04:33 -070066 LISTEN: %s
67 PORT: %d
68 NETWORK: %s
69 RANGE_LOW: %s
70 RANGE_HIGH: %s
71 LOG_LEVEL: %s
72 LOG_FORMAT: %s`,
73 config.Listen, config.Port,
74 config.Network, config.RangeLow, config.RangeHigh,
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070075 config.LogLevel, config.LogFormat)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070076
77 context.storage = &MemoryStorage{}
David K. Bainbridgeca68f062016-10-27 11:04:33 -070078 context.storage.Init(config.Network, config.RangeLow, config.RangeHigh)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070079
80 router := mux.NewRouter()
81 router.HandleFunc("/allocations/{mac}", context.ReleaseAllocationHandler).Methods("DELETE")
82 router.HandleFunc("/allocations/{mac}", context.AllocationHandler).Methods("GET")
83 router.HandleFunc("/allocations/", context.ListAllocationsHandler).Methods("GET")
84 router.HandleFunc("/addresses/{ip}", context.FreeAddressHandler).Methods("DELETE")
85 http.Handle("/", router)
86
87 http.ListenAndServe(fmt.Sprintf("%s:%d", config.Listen, config.Port), nil)
88}