Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit 323293e

Browse files
committed
pdnsにはAPIでアクセスする
1 parent 8e7af18 commit 323293e

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

webapp/go/user_handler.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10+
"io"
1011
"log"
1112
"net/http"
13+
"net/url"
1214
"os"
13-
"os/exec"
1415
"path/filepath"
1516
"strings"
1617
"time"
@@ -295,19 +296,30 @@ func registerHandler(c echo.Context) error {
295296
// return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert user theme: "+err.Error())
296297
// }
297298

298-
if out, err := exec.Command("pdnsutil", "add-record", "u.isucon.dev", req.Name, "A", "10", powerDNSSubdomainAddress).CombinedOutput(); err != nil {
299-
return echo.NewHTTPError(http.StatusInternalServerError, string(out)+": "+err.Error())
299+
// if out, err := exec.Command("pdnsutil", "add-record", "u.isucon.dev", req.Name, "A", "10", powerDNSSubdomainAddress).CombinedOutput(); err != nil {
300+
// return echo.NewHTTPError(http.StatusInternalServerError, string(out)+": "+err.Error())
301+
// }
302+
303+
url, err := url.Parse("http://" + powerDNSSubdomainAddress + ":8081/api/v1/servers/localhost/zones/u.isucon.dev.")
304+
if err != nil {
305+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to parse url: "+err.Error())
300306
}
301307

302-
// url, err := url.Parse("http://" + powerDNSSubdomainAddress + "/api/v1/servers/localhost/zones/u.isucon.dev.")
303-
// client := &http.Client{}
304-
// client.Do(&http.Request{
305-
// Method: "PATCH",
306-
// URL: url,
307-
// Header: http.Header{
308-
// "X-API-Key": []string{"secret"},
309-
// },
310-
// })
308+
payload := `{"rrsets": [{"name": "` + req.Name + `", "type": "A", "ttl": 10, "changetype": "REPLACE", "records": [{"content": "` + powerDNSSubdomainAddress + `", "disabled": false}]}]}`
309+
310+
client := &http.Client{}
311+
_, err = client.Do(&http.Request{
312+
Method: "PATCH",
313+
URL: url,
314+
Header: http.Header{
315+
"X-API-Key": []string{"isudns"},
316+
},
317+
Body: io.NopCloser(strings.NewReader(payload)),
318+
})
319+
if err != nil {
320+
log.Println("failed to request to powerdns: ", err)
321+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to request to powerdns: "+err.Error())
322+
}
311323

312324
user, err := fillUserResponse(ctx, tx, userModel)
313325
if err != nil {

0 commit comments

Comments
 (0)