forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge in DNS/adguard-home from 613-dark-theme to master Squashed commit of the following: commit 1a286e9 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 22:37:36 2023 +0700 home: imp docs commit 45c4fac Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 18:35:58 2023 +0700 home: imp docs commit e23b375 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:05:41 2023 +0700 home: imp docs commit 85d2cd6 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:04:28 2023 +0700 home: imp code commit bb00bfd Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:37:41 2023 +0700 home: imp docs commit 140fd07 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:34:27 2023 +0700 home: imp code commit 4e866b7 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 19:11:33 2023 +0700 home: imp docs commit 0f13248 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:53:57 2023 +0700 home: imp code commit e04181f Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:48:44 2023 +0700 home: imp code commit bb9989b Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 12:09:20 2023 +0700 all: control/profile API
- Loading branch information
Showing
6 changed files
with
168 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package home | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp" | ||
"github.com/AdguardTeam/golibs/log" | ||
) | ||
|
||
// Theme is an enum of all allowed UI themes. | ||
type Theme string | ||
|
||
// Allowed [Theme] values. | ||
// | ||
// Keep in sync with client/src/helpers/constants.js. | ||
const ( | ||
ThemeAuto Theme = "auto" | ||
ThemeLight Theme = "light" | ||
ThemeDark Theme = "dark" | ||
) | ||
|
||
// UnmarshalText implements [encoding.TextUnmarshaler] interface for *Theme. | ||
func (t *Theme) UnmarshalText(b []byte) (err error) { | ||
switch string(b) { | ||
case "auto": | ||
*t = ThemeAuto | ||
case "dark": | ||
*t = ThemeDark | ||
case "light": | ||
*t = ThemeLight | ||
default: | ||
return fmt.Errorf("invalid theme %q, supported: %q, %q, %q", b, ThemeAuto, ThemeDark, ThemeLight) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// profileJSON is an object for /control/profile and /control/profile/update | ||
// endpoints. | ||
type profileJSON struct { | ||
Name string `json:"name"` | ||
Language string `json:"language"` | ||
Theme Theme `json:"theme"` | ||
} | ||
|
||
// handleGetProfile is the handler for GET /control/profile endpoint. | ||
func handleGetProfile(w http.ResponseWriter, r *http.Request) { | ||
u := Context.auth.getCurrentUser(r) | ||
|
||
var resp profileJSON | ||
func() { | ||
config.RLock() | ||
defer config.RUnlock() | ||
|
||
resp = profileJSON{ | ||
Name: u.Name, | ||
Language: config.Language, | ||
Theme: config.Theme, | ||
} | ||
}() | ||
|
||
_ = aghhttp.WriteJSONResponse(w, r, resp) | ||
} | ||
|
||
// handlePutProfile is the handler for PUT /control/profile/update endpoint. | ||
func handlePutProfile(w http.ResponseWriter, r *http.Request) { | ||
if aghhttp.WriteTextPlainDeprecated(w, r) { | ||
return | ||
} | ||
|
||
profileReq := &profileJSON{} | ||
err := json.NewDecoder(r.Body).Decode(profileReq) | ||
if err != nil { | ||
aghhttp.Error(r, w, http.StatusBadRequest, "reading req: %s", err) | ||
|
||
return | ||
} | ||
|
||
lang := profileReq.Language | ||
if !allowedLanguages.Has(lang) { | ||
aghhttp.Error(r, w, http.StatusBadRequest, "unknown language: %q", lang) | ||
|
||
return | ||
} | ||
|
||
theme := profileReq.Theme | ||
|
||
func() { | ||
config.Lock() | ||
defer config.Unlock() | ||
|
||
config.Language = lang | ||
config.Theme = theme | ||
log.Printf("home: language is set to %s", lang) | ||
log.Printf("home: theme is set to %s", theme) | ||
}() | ||
|
||
onConfigModified() | ||
aghhttp.OK(w) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters