Skip to content

Commit 0198b0e

Browse files
committed
refactor the FE
1 parent 48c2537 commit 0198b0e

File tree

6 files changed

+658
-3
lines changed

6 files changed

+658
-3
lines changed

internal/server/controllers/provider.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controllers
22

33
import (
4+
"github.com/TimothyYe/godns/internal/settings"
45
"github.com/TimothyYe/godns/internal/utils"
56
"github.com/gofiber/fiber/v2"
67
log "github.com/sirupsen/logrus"
@@ -54,3 +55,66 @@ func (c *Controller) UpdateProvider(ctx *fiber.Ctx) error {
5455

5556
return ctx.SendStatus(fiber.StatusOK)
5657
}
58+
59+
func (c *Controller) GetMultiProviders(ctx *fiber.Ctx) error {
60+
return ctx.JSON(c.config.Providers)
61+
}
62+
63+
func (c *Controller) UpdateMultiProviders(ctx *fiber.Ctx) error {
64+
var providers map[string]*settings.ProviderConfig
65+
if err := ctx.BodyParser(&providers); err != nil {
66+
return err
67+
}
68+
69+
c.config.Providers = providers
70+
71+
if err := c.config.SaveSettings(c.configPath); err != nil {
72+
log.Errorf("Failed to save settings: %s", err.Error())
73+
return ctx.Status(500).SendString("Failed to save settings")
74+
}
75+
76+
return ctx.SendStatus(fiber.StatusOK)
77+
}
78+
79+
func (c *Controller) AddProviderConfig(ctx *fiber.Ctx) error {
80+
providerName := ctx.Params("provider")
81+
if providerName == "" {
82+
return ctx.Status(400).SendString("Provider name is required")
83+
}
84+
85+
var config settings.ProviderConfig
86+
if err := ctx.BodyParser(&config); err != nil {
87+
return err
88+
}
89+
90+
if c.config.Providers == nil {
91+
c.config.Providers = make(map[string]*settings.ProviderConfig)
92+
}
93+
94+
c.config.Providers[providerName] = &config
95+
96+
if err := c.config.SaveSettings(c.configPath); err != nil {
97+
log.Errorf("Failed to save settings: %s", err.Error())
98+
return ctx.Status(500).SendString("Failed to save settings")
99+
}
100+
101+
return ctx.SendStatus(fiber.StatusOK)
102+
}
103+
104+
func (c *Controller) DeleteProviderConfig(ctx *fiber.Ctx) error {
105+
providerName := ctx.Params("provider")
106+
if providerName == "" {
107+
return ctx.Status(400).SendString("Provider name is required")
108+
}
109+
110+
if c.config.Providers != nil {
111+
delete(c.config.Providers, providerName)
112+
113+
if err := c.config.SaveSettings(c.configPath); err != nil {
114+
log.Errorf("Failed to save settings: %s", err.Error())
115+
return ctx.Status(500).SendString("Failed to save settings")
116+
}
117+
}
118+
119+
return ctx.SendStatus(fiber.StatusOK)
120+
}

internal/server/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ func (s *Server) initRoutes() {
117117
route.Get("/provider/settings", s.controller.GetProviderSettings)
118118
route.Put("/provider", s.controller.UpdateProvider)
119119

120+
// Multi-provider routes
121+
route.Get("/providers", s.controller.GetMultiProviders)
122+
route.Put("/providers", s.controller.UpdateMultiProviders)
123+
route.Put("/providers/:provider", s.controller.AddProviderConfig)
124+
route.Delete("/providers/:provider", s.controller.DeleteProviderConfig)
125+
120126
// Network related routes
121127
route.Get("/network", s.controller.GetNetworkSettings)
122128
route.Put("/network", s.controller.UpdateNetworkSettings)

web/api/provider.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ export interface Provider {
2222
consumer_key: string;
2323
}
2424

25+
export interface MultiProviderConfig {
26+
[providerName: string]: {
27+
email?: string;
28+
password?: string;
29+
login_token?: string;
30+
app_key?: string;
31+
app_secret?: string;
32+
consumer_key?: string;
33+
};
34+
}
35+
2536
export async function get_provider_settings(credentials: string): Promise<ProviderSetting[]> {
2637
if (credentials) {
2738
const resp = await fetch(get_api_server() + '/api/v1/provider/settings', {
@@ -72,5 +83,77 @@ export async function update_provider(credentials: string, provider: Provider):
7283
}
7384
}
7485

86+
return false;
87+
}
88+
89+
export async function get_multi_providers(credentials: string): Promise<MultiProviderConfig> {
90+
if (credentials) {
91+
const resp = await fetch(get_api_server() + '/api/v1/providers', {
92+
method: 'GET',
93+
headers: {
94+
'Authorization': `Basic ${credentials}`
95+
}
96+
})
97+
98+
if (resp.status === 200) {
99+
return resp.json();
100+
}
101+
}
102+
103+
return {} as MultiProviderConfig;
104+
}
105+
106+
export async function update_multi_providers(credentials: string, providers: MultiProviderConfig): Promise<boolean> {
107+
if (credentials) {
108+
const resp = await fetch(get_api_server() + '/api/v1/providers', {
109+
method: 'PUT',
110+
headers: {
111+
'Authorization': `Basic ${credentials}`,
112+
'Content-Type': 'application/json'
113+
},
114+
body: JSON.stringify(providers)
115+
})
116+
117+
if (resp.status === 200) {
118+
return true;
119+
}
120+
}
121+
122+
return false;
123+
}
124+
125+
export async function add_provider_config(credentials: string, providerName: string, config: any): Promise<boolean> {
126+
if (credentials) {
127+
const resp = await fetch(get_api_server() + `/api/v1/providers/${providerName}`, {
128+
method: 'PUT',
129+
headers: {
130+
'Authorization': `Basic ${credentials}`,
131+
'Content-Type': 'application/json'
132+
},
133+
body: JSON.stringify(config)
134+
})
135+
136+
if (resp.status === 200) {
137+
return true;
138+
}
139+
}
140+
141+
return false;
142+
}
143+
144+
export async function delete_provider_config(credentials: string, providerName: string): Promise<boolean> {
145+
if (credentials) {
146+
const resp = await fetch(get_api_server() + `/api/v1/providers/${providerName}`, {
147+
method: 'DELETE',
148+
headers: {
149+
'Authorization': `Basic ${credentials}`
150+
}
151+
})
152+
153+
if (resp.status === 200) {
154+
return true;
155+
}
156+
}
157+
75158
return false;
76159
}

web/app/domains/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DomainControl } from '@/components/domain-control';
77
import { ToastContainer } from 'react-toastify';
88
import { get_info } from '@/api/info';
99
import 'react-toastify/dist/ReactToastify.css';
10-
import { ProviderControl } from '@/components/provider';
10+
import { MultiProviderControl } from '@/components/multi-provider-control';
1111

1212
export default function Domains() {
1313
const router = useRouter();
@@ -30,7 +30,7 @@ export default function Domains() {
3030
<main className="flex min-h-screen flex-col items-center justify-start pt-10 max-w-screen-xl">
3131
<ToastContainer />
3232
<div className="flex flex-col items-center w-full bg-base-100 p-10">
33-
<ProviderControl />
33+
<MultiProviderControl />
3434
<div className="divider"></div>
3535
<DomainControl />
3636
</div>

web/components/icons.tsx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,48 @@ export const SearchIcon = (props: IconSvgProps) => (
244244
</svg>
245245
);
246246

247+
export const PlusIcon = (props: IconSvgProps) => (
248+
<svg
249+
aria-hidden="true"
250+
fill="none"
251+
focusable="false"
252+
height="1em"
253+
role="presentation"
254+
viewBox="0 0 24 24"
255+
width="1em"
256+
{...props}
257+
>
258+
<path
259+
d="M12 5V19M5 12H19"
260+
stroke="currentColor"
261+
strokeLinecap="round"
262+
strokeLinejoin="round"
263+
strokeWidth="2"
264+
/>
265+
</svg>
266+
);
267+
268+
export const TrashIcon = (props: IconSvgProps) => (
269+
<svg
270+
aria-hidden="true"
271+
fill="none"
272+
focusable="false"
273+
height="1em"
274+
role="presentation"
275+
viewBox="0 0 24 24"
276+
width="1em"
277+
{...props}
278+
>
279+
<path
280+
d="M3 6H5H21M8 6V4C8 3.44772 8.44772 3 9 3H15C15.5523 3 16 3.44772 16 4V6M19 6V20C19 20.5523 18.5523 21 18 21H6C5.44772 21 5 20.5523 5 20V6H19ZM10 11V17M14 11V17"
281+
stroke="currentColor"
282+
strokeLinecap="round"
283+
strokeLinejoin="round"
284+
strokeWidth="2"
285+
/>
286+
</svg>
287+
);
288+
247289
export const NextUILogo = (props: IconSvgProps) => {
248290
const { width, height = 40 } = props;
249291

@@ -258,7 +300,7 @@ export const NextUILogo = (props: IconSvgProps) => {
258300
>
259301
<path
260302
className="fill-black dark:fill-white"
261-
d="M55.6827 5V26.6275H53.7794L41.1235 8.51665H40.9563V26.6275H39V5H40.89L53.5911 23.1323H53.7555V5H55.6827ZM67.4831 26.9663C66.1109 27.0019 64.7581 26.6329 63.5903 25.9044C62.4852 25.185 61.6054 24.1633 61.0537 22.9582C60.4354 21.5961 60.1298 20.1106 60.1598 18.6126C60.132 17.1113 60.4375 15.6228 61.0537 14.2563C61.5954 13.0511 62.4525 12.0179 63.5326 11.268C64.6166 10.5379 65.8958 10.16 67.1986 10.1852C68.0611 10.1837 68.9162 10.3468 69.7187 10.666C70.5398 10.9946 71.2829 11.4948 71.8992 12.1337C72.5764 12.8435 73.0985 13.6889 73.4318 14.6152C73.8311 15.7483 74.0226 16.9455 73.9968 18.1479V19.0773H63.2262V17.4194H72.0935C72.1083 16.4456 71.8952 15.4821 71.4714 14.6072C71.083 13.803 70.4874 13.1191 69.7472 12.6272C68.9887 12.1348 68.1022 11.8812 67.2006 11.8987C66.2411 11.8807 65.3005 12.1689 64.5128 12.7223C63.7332 13.2783 63.1083 14.0275 62.6984 14.8978C62.2582 15.8199 62.0314 16.831 62.0352 17.8546V18.8476C62.009 20.0078 62.2354 21.1595 62.6984 22.2217C63.1005 23.1349 63.7564 23.9108 64.5864 24.4554C65.4554 24.9973 66.4621 25.2717 67.4831 25.2448C68.1676 25.2588 68.848 25.1368 69.4859 24.8859C70.0301 24.6666 70.5242 24.3376 70.9382 23.919C71.3183 23.5345 71.6217 23.0799 71.8322 22.5799L73.5995 23.1604C73.3388 23.8697 72.9304 24.5143 72.4019 25.0506C71.8132 25.6529 71.1086 26.1269 70.3314 26.4434C69.4258 26.8068 68.4575 26.9846 67.4831 26.9663V26.9663ZM78.8233 10.4075L82.9655 17.325L87.1076 10.4075H89.2683L84.1008 18.5175L89.2683 26.6275H87.103L82.9608 19.9317L78.8193 26.6275H76.6647L81.7711 18.5169L76.6647 10.4062L78.8233 10.4075ZM99.5142 10.4075V12.0447H91.8413V10.4075H99.5142ZM94.2427 6.52397H96.1148V22.3931C96.086 22.9446 96.2051 23.4938 96.4597 23.9827C96.6652 24.344 96.9805 24.629 97.3589 24.7955C97.7328 24.9548 98.1349 25.0357 98.5407 25.0332C98.7508 25.0359 98.9607 25.02 99.168 24.9857C99.3422 24.954 99.4956 24.9205 99.6283 24.8853L100.026 26.5853C99.8062 26.6672 99.5805 26.7327 99.3511 26.7815C99.0274 26.847 98.6977 26.8771 98.3676 26.8712C97.6854 26.871 97.0119 26.7156 96.3973 26.4166C95.7683 26.1156 95.2317 25.6485 94.8442 25.0647C94.4214 24.4018 94.2097 23.6242 94.2374 22.8363L94.2427 6.52397ZM118.398 5H120.354V19.3204C120.376 20.7052 120.022 22.0697 119.328 23.2649C118.644 24.4235 117.658 25.3698 116.477 26.0001C115.168 26.6879 113.708 27.0311 112.232 26.9978C110.759 27.029 109.302 26.6835 107.996 25.9934C106.815 25.362 105.827 24.4161 105.141 23.2582C104.447 22.0651 104.092 20.7022 104.115 19.319V5H106.08V19.1831C106.061 20.2559 106.324 21.3147 106.843 22.2511C107.349 23.1459 108.094 23.8795 108.992 24.3683C109.993 24.9011 111.111 25.1664 112.242 25.139C113.373 25.1656 114.493 24.9003 115.495 24.3683C116.395 23.8815 117.14 23.1475 117.644 22.2511C118.16 21.3136 118.421 20.2553 118.402 19.1831L118.398 5ZM128 5V26.6275H126.041V5H128Z"
303+
d="M55.6827 5V26.6275H53.7794L41.1235 8.51665H40.9563V26.6275H39V5H40.89L53.5911 23.1323H53.7555V5H55.6827ZM67.4831 26.9663C66.1109 27.0019 64.7581 26.6329 63.5903 25.9044C62.4852 25.185 61.6054 24.1633 61.0537 22.9582C60.4354 21.5961 60.1298 20.1106 60.1598 18.6126C60.132 17.1113 60.4375 15.6228 61.0537 14.2563C61.5954 13.0511 62.4525 12.0179 63.5326 11.268C64.6166 10.5379 65.8958 10.16 67.1986 10.1852C68.0611 10.1837 68.9162 10.3468 69.7187 10.666C70.5398 10.9946 71.2829 11.4948 71.8992 12.1337C72.5764 12.8435 73.0985 13.6889 73.4318 14.6152C73.8311 15.7483 74.0226 16.9455 73.9968 18.1479V19.0773H63.2262V17.4194H72.0935C72.1083 16.4456 71.8952 15.4821 71.4714 14.6072C71.083 13.803 70.4874 13.1191 69.7472 12.6272C68.9887 12.1348 68.1022 11.8812 67.2006 11.8987C66.2411 11.8807 65.3005 12.1689 64.5128 12.7223C63.7332 13.2783 63.1083 14.0275 62.6984 14.8978C62.2582 15.8199 62.0314 16.831 62.0352 17.8546V18.8476C62.009 20.0078 62.2354 21.1595 62.6984 22.2217C63.1005 23.1349 63.7564 23.9108 64.5864 24.4554C65.4554 24.9973 66.4621 25.2717 67.4831 25.2448C68.1676 25.2588 68.848 25.1368 69.4859 24.8859C70.0301 24.6666 70.5242 24.3376 70.9382 23.919C71.3183 23.5345 71.6217 23.0799 71.8322 22.5799L73.5995 23.1604C73.3388 23.8697 72.9304 24.5143 72.4019 25.0506C71.8132 25.6529 71.1086 26.1269 70.3314 26.4434C69.4258 26.8068 68.4575 26.9846 67.4831 26.9663V26.9663ZM78.8233 10.4075L82.9655 17.325L87.1076 10.4075H89.2683L84.1008 18.5175L89.2683 26.6275H87.103L82.9608 19.9317L78.8193 26.6275H76.6647L81.7711 18.5169L76.6647 10.4062L78.8233 10.4075ZM99.5142 10.4075V12.0447H91.8413V10.4075H99.5142ZM94.2427 6.52397H96.1148V22.3931C96.086 22.9446 96.2051 23.4938 96.4597 23.9827C96.6652 24.344 96.9805 24.629 97.3589 24.7955C97.7328 24.9548 98.1349 25.0357 98.5407 25.0332C98.7508 25.0359 98.9607 25.02 99.168 24.9857C99.3422 24.954 99.4956 24.9205 99.6283 24.8853L100.026 26.5853C99.8062 26.6672 99.5805 26.7327 99.3511 26.7815C99.0274 26.847 98.6977 26.8771 98.3676 26.8712C97.6854 26.871 97.0119 26.7156 96.3973 26.4166C95.7683 26.1156 95.2317 25.6485 94.8442 25.0647C94.4214 24.4018 94.2097 23.6242 94.2374 22.8363L94.2427 6.52397ZM118.398 5H120.354V19.3204C120.376 20.7052 120.022 22.0697 119.328 23.2649C118.644 24.4235 117.658 25.3698 116.477 26.0001C115.168 26.6879 113.708 27.0311 112.232 26.9978C110.759 27.029 109.302 26.6835 107.996 25.9934C106.815 25.362 105.827 24.4161 105.141 23.2582C104.447 22.0651 104.092 20.7022 104.115 19.319V5H106.08V19.1831C106.061 20.2559 106.324 21.3147 106.843 22.2511C107.349 23.1459 108.094 23.8795 108.992 24.3683C109.993 24.9011 111.111 25.1664 112.242 25.139C113.373 25.1656 114.493 24.9003 115.495 24.3683C116.395 23.8815 117.14 23.1475 117.644 22.2511C118.160 21.3136 118.421 20.2553 118.402 19.1831L118.398 5ZM128 5V26.6275H126.041V5H128Z"
262304
/>
263305
<path
264306
className="fill-black dark:fill-white"

0 commit comments

Comments
 (0)