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. |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 18 | "github.com/Sirupsen/logrus" |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 19 | "github.com/gorilla/mux" |
| 20 | "github.com/kelseyhightower/envconfig" |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 21 | "net/http" |
| 22 | ) |
| 23 | |
| 24 | type Config struct { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 25 | Port int `default:"4242"` |
| 26 | Listen string `default:"0.0.0.0"` |
| 27 | Network string `default:"10.0.0.0/24"` |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 28 | RangeLow string `default:"10.0.0.2" envconfig:"RANGE_LOW"` |
| 29 | RangeHigh string `default:"10.0.0.253" envconfig:"RANGE_HIGH"` |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 30 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 31 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type Context struct { |
| 35 | storage Storage |
| 36 | } |
| 37 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 38 | var log = logrus.New() |
| 39 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 40 | func main() { |
| 41 | context := &Context{} |
| 42 | |
| 43 | config := Config{} |
| 44 | err := envconfig.Process("ALLOCATE", &config) |
| 45 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 46 | log.Fatalf("Unable to parse configuration options : %s", err) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 47 | } |
| 48 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 49 | 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. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 66 | 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. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 75 | config.LogLevel, config.LogFormat) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 76 | |
| 77 | context.storage = &MemoryStorage{} |
David K. Bainbridge | ca68f06 | 2016-10-27 11:04:33 -0700 | [diff] [blame^] | 78 | context.storage.Init(config.Network, config.RangeLow, config.RangeHigh) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 79 | |
| 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 | } |