-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Config API Service & Refactor Config Implementation
- Loading branch information
1 parent
9d26b5d
commit 700852a
Showing
5 changed files
with
109 additions
and
28 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,57 @@ | ||
package web_service | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/jackdallas/premiumizearr/internal/config" | ||
) | ||
|
||
type ConfigChangeResponse struct { | ||
Succeeded bool `json:"succeeded"` | ||
Status string `json:"status"` | ||
} | ||
|
||
func (s *server) ConfigHandler(w http.ResponseWriter, r *http.Request) { | ||
|
||
switch r.Method { | ||
case http.MethodGet: | ||
data, err := json.Marshal(s.config) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Write(data) | ||
case http.MethodPost: | ||
var newConfig config.Config | ||
err := json.NewDecoder(r.Body).Decode(&newConfig) | ||
if err != nil { | ||
EncodeAndWriteConfigChangeResponse(w, &ConfigChangeResponse{ | ||
Succeeded: false, | ||
Status: fmt.Sprintf("Config failed to update %s", err.Error()), | ||
}) | ||
return | ||
} | ||
s.config = &newConfig | ||
s.config.Save() | ||
EncodeAndWriteConfigChangeResponse(w, &ConfigChangeResponse{ | ||
Succeeded: true, | ||
Status: "Config updated", | ||
}) | ||
default: | ||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
} | ||
|
||
} | ||
|
||
func EncodeAndWriteConfigChangeResponse(w http.ResponseWriter, resp *ConfigChangeResponse) { | ||
data, err := json.Marshal(resp) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Write(data) | ||
} |
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