blob: e5a64cbedef9d04165b6575ac31d7e0bb55cd286 [file] [log] [blame]
David K. Bainbridge8bc905c2016-05-31 14:07:10 -07001package main
2
3import (
4 "fmt"
5 "github.com/gorilla/mux"
6 "github.com/kelseyhightower/envconfig"
7 "log"
8 "net/http"
9)
10
11type Config struct {
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070012 Port int `default:"4242"`
13 Listen string `default:"0.0.0.0"`
14 Network string `default:"10.0.0.0/24"`
15 Skip int `default:"1"`
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070016}
17
18type Context struct {
19 storage Storage
20}
21
22func main() {
23 context := &Context{}
24
25 config := Config{}
26 err := envconfig.Process("ALLOCATE", &config)
27 if err != nil {
28 log.Fatalf("[error] Unable to parse configuration options : %s", err)
29 }
30
31 log.Printf(`Configuration:
32 Listen: %s
33 Port: %d
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070034 Network: %s
35 SKip: %d`, config.Listen, config.Port, config.Network, config.Skip)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070036
37 context.storage = &MemoryStorage{}
David K. Bainbridge252cc2c2016-06-02 22:28:59 -070038 context.storage.Init(config.Network, config.Skip)
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070039
40 router := mux.NewRouter()
41 router.HandleFunc("/allocations/{mac}", context.ReleaseAllocationHandler).Methods("DELETE")
42 router.HandleFunc("/allocations/{mac}", context.AllocationHandler).Methods("GET")
43 router.HandleFunc("/allocations/", context.ListAllocationsHandler).Methods("GET")
44 router.HandleFunc("/addresses/{ip}", context.FreeAddressHandler).Methods("DELETE")
45 http.Handle("/", router)
46
47 http.ListenAndServe(fmt.Sprintf("%s:%d", config.Listen, config.Port), nil)
48}