DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 3cd37b5d7df372ecbf494843ba642e004a54621a felu/internal/api/update.go -rw-r--r-- 1.5 KiB
3cd37b5dJonni Liljamo feat: custom TsigProvider implementation 17 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

// Package api implements API route handlers.
package api

import (
	"net/http"

	"git.src.quest/~liljamo/felu/internal/db"
	"git.src.quest/~liljamo/felu/internal/util"
	"github.com/gin-gonic/gin"
)

// UpdateA updates an A record based on query params.
func UpdateA() gin.HandlerFunc {
	return func(c *gin.Context) {
		domain := c.Query("domain")
		if domain == "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"status": "error",
				"error":  "no domain was provided",
			})
			c.Abort()
			return
		}

		apiKey := c.Query("api_key")
		if apiKey == "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"status": "error",
				"error":  "no api key was provided",
			})
			c.Abort()
			return
		}

		aRecord := c.Query("record")
		if aRecord == "" {
			aRecord = c.ClientIP()
		}

		if err := util.CheckARecord(aRecord); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"status": "error",
				"error":  err.Error(),
			})
			c.Abort()
			return
		}

		err := db.UpdateDomainARecord(domain, apiKey, aRecord)
		if err != nil {
			// FIXME: Handle better, "bad api key" is just the most likely scenario
			c.JSON(http.StatusBadRequest, gin.H{
				"status": "error",
				"error":  "bad api key",
			})
			c.Abort()
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"status":   "success",
			"a_record": aRecord,
		})
	}
}