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"` |
| 28 | Skip int `default:"1"` |
| 29 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 30 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | type Context struct { |
| 34 | storage Storage |
| 35 | } |
| 36 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 37 | var log = logrus.New() |
| 38 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 39 | func main() { |
| 40 | context := &Context{} |
| 41 | |
| 42 | config := Config{} |
| 43 | err := envconfig.Process("ALLOCATE", &config) |
| 44 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 45 | log.Fatalf("Unable to parse configuration options : %s", err) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 46 | } |
| 47 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 48 | 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. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 65 | Listen: %s |
| 66 | Port: %d |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 67 | Network: %s |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 68 | 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. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 72 | |
| 73 | context.storage = &MemoryStorage{} |
David K. Bainbridge | 252cc2c | 2016-06-02 22:28:59 -0700 | [diff] [blame] | 74 | context.storage.Init(config.Network, config.Skip) |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 75 | |
| 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 | } |