blob: a4757e40a5d56ff931be0b9c8c7c985ac785c13e [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"`
28 Skip int `default:"1"`
29 LogLevel string `default:"warning" envconfig:"LOG_LEVEL"`
30 LogFormat string `default:"text" envconfig:"LOG_FORMAT"`
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070031}
32
33type Context struct {
34 storage Storage
35}
36
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070037var log = logrus.New()
38
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070039func main() {
40 context := &Context{}
41
42 config := Config{}
43 err := envconfig.Process("ALLOCATE", &config)
44 if err != nil {
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070045 log.Fatalf("Unable to parse configuration options : %s", err)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070046 }
47
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070048 switch config.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(config.LogLevel)
59 if err != nil {
60 level = logrus.WarnLevel
61 }
62 log.Level = level
63
64 log.Infof(`Configuration:
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070065 Listen: %s
66 Port: %d
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070067 Network: %s
David K. Bainbridgea9c2e0a2016-07-01 18:33:50 -070068 SKip: %d
69 Log Level: %s
70 Log Format: %s`, config.Listen, config.Port, config.Network, config.Skip,
71 config.LogLevel, config.LogFormat)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070072
73 context.storage = &MemoryStorage{}
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070074 context.storage.Init(config.Network, config.Skip)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070075
76 router := mux.NewRouter()
77 router.HandleFunc("/allocations/{mac}", context.ReleaseAllocationHandler).Methods("DELETE")
78 router.HandleFunc("/allocations/{mac}", context.AllocationHandler).Methods("GET")
79 router.HandleFunc("/allocations/", context.ListAllocationsHandler).Methods("GET")
80 router.HandleFunc("/addresses/{ip}", context.FreeAddressHandler).Methods("DELETE")
81 http.Handle("/", router)
82
83 http.ListenAndServe(fmt.Sprintf("%s:%d", config.Listen, config.Port), nil)
84}