DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 4992844348f2ecabf096daf60a71f737a928683a felu/internal/handlers/domains.go -rw-r--r-- 4.3 KiB
49928443Jonni Liljamo docs: update README.md 23 hours 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * 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 handlers

import (
	"net/http"
	"regexp"
	"strings"

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

type postDomainData struct {
	Domain  string `form:"domain"`
	ARecord string `form:"a_record"`
}

// PostDomain returns a gin handler
func PostDomain() gin.HandlerFunc {
	return func(c *gin.Context) {
		data := &postDomainData{}
		if err := c.Bind(data); err != nil {
			c.String(http.StatusBadRequest, "Could not bind domain data")
			c.Abort()
			return
		}

		if data.Domain == "" {
			c.String(http.StatusBadRequest, "Domain can't be empty")
			c.Abort()
			return
		}
		if strings.Contains(data.Domain, ".") {
			c.String(http.StatusBadRequest, "Domain can't contain full stops")
			c.Abort()
			return
		}
		// NOTE: I doubt doing a little regex here will matter, just the easiest for now.
		if !regexp.MustCompile(`^[A-Za-z0-9]*$`).MatchString(data.Domain) {
			c.String(http.StatusBadRequest, "Domain contains invalid chars")
			c.Abort()
			return
		}
		if err := util.CheckARecord(data.ARecord); err != nil {
			c.String(http.StatusBadRequest, err.Error())
			c.Abort()
			return
		}

		userID, exists := c.Get("user_id")
		if !exists {
			c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
			c.Abort()
			return
		}

		err := db.CreateDomain(data.Domain, data.ARecord, userID.(string))
		if err != nil {
			// FIXME: Handle better
			c.String(http.StatusInternalServerError, "Something went wrong while creating a new domain")
			c.Abort()
			return
		}

		c.Header("HX-Trigger", "update-domain-list")
	}
}

// PatchDomain returns a gin handler
func PatchDomain() gin.HandlerFunc {
	return func(c *gin.Context) {
		userID, exists := c.Get("user_id")
		if !exists {
			c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
			c.Abort()
			return
		}

		id := c.Param("id")

		aRecord := c.PostForm("a_record")
		if aRecord != "" {
			if err := util.CheckARecord(aRecord); err != nil {
				c.String(http.StatusBadRequest, err.Error())
				c.Abort()
				return
			}
			err := db.UpdateDomainARecordManual(id, userID.(string), aRecord)
			if err != nil {
				// FIXME: Handle better
				c.String(http.StatusInternalServerError, "Something went wrong while updating the a record")
				c.Abort()
				return
			}
		}

		c.Header("HX-Trigger", "update-domain-list")
	}
}

// DeleteDomain returns a gin handler
func DeleteDomain() gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Param("id")

		userID, exists := c.Get("user_id")
		if !exists {
			c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
			c.Abort()
			return
		}

		err := db.DeleteDomain(id, userID.(string))
		if err != nil {
			// FIXME: Handle better
			c.String(http.StatusInternalServerError, "Something went wrong while deleting the domain")
			c.Abort()
			return
		}

		c.Header("HX-Trigger", "update-domain-list")
	}
}

// RefreshDomainAPIKey returns a gin handler
func RefreshDomainAPIKey() gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Param("id")

		userID, exists := c.Get("user_id")
		if !exists {
			c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
			c.Abort()
			return
		}

		err := db.RefreshDomainAPIKey(id, userID.(string))
		if err != nil {
			// FIXME: Handle better
			c.String(http.StatusInternalServerError, "Something went wrong while updating the api key")
			c.Abort()
			return
		}

		c.Header("HX-Trigger", "update-domain-list")
	}
}

// RefreshDomainTsigKey returns a gin handler
func RefreshDomainTsigKey() gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Param("id")

		userID, exists := c.Get("user_id")
		if !exists {
			c.String(http.StatusInternalServerError, "This should not be possible, but don't quote me on that")
			c.Abort()
			return
		}

		err := db.RefreshDomainTsigKey(id, userID.(string))
		if err != nil {
			// FIXME: Handle better
			c.String(http.StatusInternalServerError, "Something went wrong while updating the tsig key")
			c.Abort()
			return
		}

		c.Header("HX-Trigger", "update-domain-list")
	}
}