DEVELOPMENT ENVIRONMENT

~liljamo/felu

8c173e8fb830f8f08d656e0d6b8ad6d48c74dd1d — Jonni Liljamo 1 year, 22 days ago 0ea20b4 0.1.6
feat: manual updating of A records
M internal/components/managepartials.templ => internal/components/managepartials.templ +13 -5
@@ 42,11 42,19 @@ templ ManagePartialDomains(domains []db.Domain) {
							</div>
						</td>
						<td>
							<div class="p-2">
								<!-- TODO: Make this editable
									And refresh the div with /manage/partials/domains
								-->
								<input class="border" value={ domain.A }/>
							<div class="p-2 flex flex-col">
								<form class="flex items-center gap-1" hx-patch={ fmt.Sprintf("/manage/domains/%s", domain.Id) }
									hx-target={ fmt.Sprintf("#domain_patch_error_%s", domain.Id) }
									hx-confirm="Apply?"
								>
									<input class="border" type="text" name="a_record" value={ domain.A }/>
									<button class="border p-1" type="submit">
										<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
											<path d="M4 12.6111L8.92308 17.5L20 6.5" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
										</svg>
									</button>
								</form>
								<div class="text-rose-600 text-center" id={ fmt.Sprintf("domain_patch_error_%s", domain.Id) }></div>
							</div>
						</td>
						<td>

M internal/components/managepartials_templ.go => internal/components/managepartials_templ.go +24 -11
@@ 93,18 93,23 @@ func ManagePartialDomains(domains []db.Domain) templ.Component {
				if err != nil {
					return err
				}
				_, err = templBuffer.WriteString("</span></div></td><td><div class=\"p-2\"><!--")
				_, err = templBuffer.WriteString("</span></div></td><td><div class=\"p-2 flex flex-col\"><form class=\"flex items-center gap-1\" hx-patch=\"")
				if err != nil {
					return err
				}
				var_7 := ` TODO: Make this editable
									And refresh the div with /manage/partials/domains
								`
				_, err = templBuffer.WriteString(var_7)
				_, err = templBuffer.WriteString(templ.EscapeString(fmt.Sprintf("/manage/domains/%s", domain.Id)))
				if err != nil {
					return err
				}
				_, err = templBuffer.WriteString("\" hx-target=\"")
				if err != nil {
					return err
				}
				_, err = templBuffer.WriteString(templ.EscapeString(fmt.Sprintf("#domain_patch_error_%s", domain.Id)))
				if err != nil {
					return err
				}
				_, err = templBuffer.WriteString("--><input class=\"border\" value=\"")
				_, err = templBuffer.WriteString("\" hx-confirm=\"Apply?\"><input class=\"border\" type=\"text\" name=\"a_record\" value=\"")
				if err != nil {
					return err
				}


@@ 112,7 117,15 @@ func ManagePartialDomains(domains []db.Domain) templ.Component {
				if err != nil {
					return err
				}
				_, err = templBuffer.WriteString("\"></div></td><td><div class=\"p-2 flex items-center gap-1\"><input class=\"border\" disabled type=\"password\" value=\"")
				_, err = templBuffer.WriteString("\"><button class=\"border p-1\" type=\"submit\"><svg class=\"w-5 h-5\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M4 12.6111L8.92308 17.5L20 6.5\" stroke=\"black\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"></path></svg></button></form><div class=\"text-rose-600 text-center\" id=\"")
				if err != nil {
					return err
				}
				_, err = templBuffer.WriteString(templ.EscapeString(fmt.Sprintf("domain_patch_error_%s", domain.Id)))
				if err != nil {
					return err
				}
				_, err = templBuffer.WriteString("\"></div></div></td><td><div class=\"p-2 flex items-center gap-1\"><input class=\"border\" disabled type=\"password\" value=\"")
				if err != nil {
					return err
				}


@@ 140,8 153,8 @@ func ManagePartialDomains(domains []db.Domain) templ.Component {
				if err != nil {
					return err
				}
				var var_8 templ.ComponentScript = toggleApiKeyVisibility(domain.Id)
				_, err = templBuffer.WriteString(var_8.Call)
				var var_7 templ.ComponentScript = toggleApiKeyVisibility(domain.Id)
				_, err = templBuffer.WriteString(var_7.Call)
				if err != nil {
					return err
				}


@@ 181,8 194,8 @@ func ManagePartialDomains(domains []db.Domain) templ.Component {
				if err != nil {
					return err
				}
				var_9 := `Delete`
				_, err = templBuffer.WriteString(var_9)
				var_8 := `Delete`
				_, err = templBuffer.WriteString(var_8)
				if err != nil {
					return err
				}

M internal/db/domains.go => internal/db/domains.go +11 -0
@@ 143,6 143,17 @@ func UpdateDomainARecord(ddns_domain string, providedApiKey string, aRecord stri
	return nil
}

func UpdateDomainARecordManual(id string, userId string, aRecord string) error {
	_, err := DBConn.Exec(`UPDATE domains SET a_record = $1 WHERE id = $2 AND owner = $3`,
		aRecord, id, userId)
	if err != nil {
		return err
	}

	return nil
}


func RefreshDomainApiKey(id string, user_id string) error {
	apiKey := util.GenApiKey()
	_, err := DBConn.Exec(`UPDATE domains SET apikey = $1 WHERE id = $2 AND owner = $3`, apiKey, id, user_id)

M internal/handlers/domains.go => internal/handlers/domains.go +31 -0
@@ 71,6 71,37 @@ func PostDomain() gin.HandlerFunc {
	}
}

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")
	}
}

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

M internal/routers/frontend.go => internal/routers/frontend.go +1 -1
@@ 44,7 44,7 @@ func SetupFrontendRouter(sm *scs.SessionManager) *gin.Engine {
		manage.DELETE("/user", handlers.DeleteUser(sm))

		manage.POST("/domains", handlers.PostDomain())
		manage.PATCH("/domains/:id") // TODO:
		manage.PATCH("/domains/:id", handlers.PatchDomain())
		manage.DELETE("/domains/:id", handlers.DeleteDomain())
		manage.POST("/domains/:id/api_key", handlers.RefreshDomainApiKey())