David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "github.com/gorilla/mux" |
| 6 | "net/http" |
| 7 | ) |
| 8 | |
| 9 | type ErrorMsg struct { |
| 10 | Error string |
| 11 | } |
| 12 | |
| 13 | type AllocationMsg struct { |
| 14 | Mac string |
| 15 | Ip string |
| 16 | } |
| 17 | |
| 18 | func (c *Context) release(mac string, w http.ResponseWriter) { |
| 19 | err := Release(c.storage, mac) |
| 20 | if err != nil { |
| 21 | msg := ErrorMsg{ |
| 22 | Error: err.Error(), |
| 23 | } |
| 24 | bytes, err := json.Marshal(&msg) |
| 25 | if err != nil { |
| 26 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 27 | return |
| 28 | } |
| 29 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 30 | return |
| 31 | } |
| 32 | w.WriteHeader(http.StatusOK) |
| 33 | } |
| 34 | |
| 35 | func (c *Context) ReleaseAllocationHandler(w http.ResponseWriter, r *http.Request) { |
| 36 | vars := mux.Vars(r) |
| 37 | mac := vars["mac"] |
| 38 | c.release(mac, w) |
| 39 | } |
| 40 | |
| 41 | func (c *Context) AllocationHandler(w http.ResponseWriter, r *http.Request) { |
| 42 | vars := mux.Vars(r) |
| 43 | mac := vars["mac"] |
| 44 | |
| 45 | ip, err := Allocate(c.storage, mac) |
| 46 | if err != nil { |
| 47 | msg := ErrorMsg{ |
| 48 | Error: err.Error(), |
| 49 | } |
| 50 | bytes, err := json.Marshal(&msg) |
| 51 | if err != nil { |
| 52 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 53 | return |
| 54 | } |
| 55 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | msg := AllocationMsg{ |
| 60 | Mac: mac, |
| 61 | Ip: ip, |
| 62 | } |
| 63 | bytes, err := json.Marshal(&msg) |
| 64 | if err != nil { |
| 65 | msg := ErrorMsg{ |
| 66 | Error: err.Error(), |
| 67 | } |
| 68 | bytes, err := json.Marshal(&msg) |
| 69 | if err != nil { |
| 70 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 71 | return |
| 72 | } |
| 73 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 74 | return |
| 75 | } |
| 76 | w.Write(bytes) |
| 77 | } |
| 78 | |
| 79 | func (c *Context) ListAllocationsHandler(w http.ResponseWriter, r *http.Request) { |
| 80 | all := c.storage.GetAll() |
| 81 | |
| 82 | list := make([]AllocationMsg, len(all)) |
| 83 | i := 0 |
| 84 | for k, v := range all { |
| 85 | list[i].Mac = k |
| 86 | list[i].Ip = v |
| 87 | i += 1 |
| 88 | } |
| 89 | |
| 90 | bytes, err := json.Marshal(&list) |
| 91 | if err != nil { |
| 92 | msg := ErrorMsg{ |
| 93 | Error: err.Error(), |
| 94 | } |
| 95 | bytes, err := json.Marshal(&msg) |
| 96 | if err != nil { |
| 97 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 98 | return |
| 99 | } |
| 100 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 101 | return |
| 102 | } |
| 103 | w.Write(bytes) |
| 104 | } |
| 105 | |
| 106 | func (c *Context) FreeAddressHandler(w http.ResponseWriter, r *http.Request) { |
| 107 | vars := mux.Vars(r) |
| 108 | ip := vars["ip"] |
| 109 | |
| 110 | all := c.storage.GetAll() |
| 111 | for k, v := range all { |
| 112 | if v == ip { |
| 113 | c.release(k, w) |
| 114 | return |
| 115 | } |
| 116 | } |
| 117 | http.Error(w, "", http.StatusNotFound) |
| 118 | } |