blob: 1156d8d681525a9a8fc8445525cd6cc5cd4ecbab [file] [log] [blame]
gioe72b54f2024-04-22 10:44:41 +04001package main
2
3import (
4 "encoding/json"
gio381c7672026-07-23 15:28:26 +04005 "io"
gioe72b54f2024-04-22 10:44:41 +04006 "fmt"
7 "net/http"
8 "strings"
9)
10
11type Server struct {
12 s *http.Server
13 m *http.ServeMux
14 store RecordStore
15 zone string
16 ds string
17 nameserver []string
18}
19
20func NewServer(port int, zone string, ds string, store RecordStore, nameserver []string) *Server {
21 m := http.NewServeMux()
22 s := &Server{
23 s: &http.Server{
24 Addr: fmt.Sprintf(":%d", port),
25 Handler: m,
26 },
27 m: m,
28 store: store,
29 zone: zone,
30 ds: ds,
31 nameserver: nameserver,
32 }
33 m.HandleFunc("/records-to-publish", s.recordsToPublish)
34 m.HandleFunc("/create-txt-record", s.createTxtRecord)
giof6ad2982024-08-23 17:42:49 +040035 m.HandleFunc("/create-a-record", s.createARecord)
36 m.HandleFunc("/delete-a-record", s.deleteARecord)
gioe72b54f2024-04-22 10:44:41 +040037 m.HandleFunc("/delete-txt-record", s.deleteTxtRecord)
gio381c7672026-07-23 15:28:26 +040038 m.HandleFunc("/", s.getAll)
gioe72b54f2024-04-22 10:44:41 +040039 return s
40}
41
42func (s *Server) Start() error {
43 return s.s.ListenAndServe()
44}
45
gio381c7672026-07-23 15:28:26 +040046func (s *Server) getAll(w http.ResponseWriter, r *http.Request) {
47 all, err := s.store.All()
48 if err != nil {
49 http.Error(w, err.Error(), http.StatusBadRequest)
50 return
51 }
52 io.WriteString(w, all)
gioe72b54f2024-04-22 10:44:41 +040053}
54
55func (s *Server) recordsToPublish(w http.ResponseWriter, r *http.Request) {
56 subdomain := strings.Split(s.zone, ".")[0]
57 if _, err := fmt.Fprintln(w, s.ds); err != nil {
58 http.Error(w, err.Error(), http.StatusInternalServerError)
59 return
60 }
61 for i, ip := range s.nameserver {
62 if _, err := fmt.Fprintf(w, "ns%d.%s. 10800 IN A %s\n", i+1, s.zone, ip); err != nil {
63 http.Error(w, err.Error(), http.StatusInternalServerError)
64 return
65 }
66 if _, err := fmt.Fprintf(w, "%s 10800 IN NS ns%d.%s.\n", subdomain, i+1, s.zone); err != nil {
67 http.Error(w, err.Error(), http.StatusInternalServerError)
68 return
69 }
70 }
71}
72
gio381c7672026-07-23 15:28:26 +040073type record struct {
74 Domain string `json:"domain,omitempty"`
75 Entry string `json:"entry,omitempty"`
76 Text string `json:"text,omitempty"`
77}
78
gioe72b54f2024-04-22 10:44:41 +040079func (s *Server) createTxtRecord(w http.ResponseWriter, r *http.Request) {
80 var req record
81 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
82 http.Error(w, err.Error(), http.StatusBadRequest)
83 return
84 }
gioe72b54f2024-04-22 10:44:41 +040085 if err := s.store.Add(req.Entry, req.Text); err != nil {
86 http.Error(w, err.Error(), http.StatusInternalServerError)
87 return
88 }
89}
90
giof6ad2982024-08-23 17:42:49 +040091func (s *Server) createARecord(w http.ResponseWriter, r *http.Request) {
92 var req record
93 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
94 http.Error(w, err.Error(), http.StatusBadRequest)
95 return
96 }
97 if err := s.store.AddARecord(req.Entry, req.Text); err != nil {
98 http.Error(w, err.Error(), http.StatusInternalServerError)
99 return
100 }
101}
102
gioe72b54f2024-04-22 10:44:41 +0400103func (s *Server) deleteTxtRecord(w http.ResponseWriter, r *http.Request) {
104 var req record
105 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
106 http.Error(w, err.Error(), http.StatusBadRequest)
107 return
108 }
gioe72b54f2024-04-22 10:44:41 +0400109 if err := s.store.Delete(req.Entry, req.Text); err != nil {
110 http.Error(w, err.Error(), http.StatusInternalServerError)
111 return
112 }
113}
giof6ad2982024-08-23 17:42:49 +0400114
115func (s *Server) deleteARecord(w http.ResponseWriter, r *http.Request) {
116 var req record
117 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
118 http.Error(w, err.Error(), http.StatusBadRequest)
119 return
120 }
121 if err := s.store.DeleteARecord(req.Entry, req.Text); err != nil {
122 http.Error(w, err.Error(), http.StatusInternalServerError)
123 return
124 }
125}