aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe <me@christine.website>2023-01-01 15:21:30 -0500
committerXe <me@christine.website>2023-01-01 15:21:30 -0500
commitdaf61f6fd70af707baee30b61e7aee5375bb4888 (patch)
tree01c253d67f6ae61915dca6d7ef85cda2dda55c86
parent88ab7e70b442a45a67e4a9f4f1ee79b0aa622273 (diff)
downloadx-daf61f6fd70af707baee30b61e7aee5375bb4888.tar.xz
x-daf61f6fd70af707baee30b61e7aee5375bb4888.zip
add whoisfront package
Signed-off-by: Xe <me@christine.website>
-rw-r--r--cmd/whoisfront/main.go51
-rw-r--r--flake.nix6
2 files changed, 16 insertions, 41 deletions
diff --git a/cmd/whoisfront/main.go b/cmd/whoisfront/main.go
index 5817800..fce70bd 100644
--- a/cmd/whoisfront/main.go
+++ b/cmd/whoisfront/main.go
@@ -2,20 +2,19 @@
package main
import (
- "bytes"
"flag"
"fmt"
"io"
- "io/ioutil"
"log"
"net/http"
"net/http/cgi"
+ "os"
"within.website/x/internal"
)
var (
- miToken = flag.String("mi-token", "", "Mi token")
+ miTokenPath = flag.String("mi-token-path", "", "Mi token path")
)
func main() {
@@ -27,54 +26,30 @@ func main() {
}
}
-func miSwitch(to string) error {
- req, err := http.NewRequest(http.MethodGet, "https://mi.within.website/api/switches/switch", bytes.NewBuffer([]byte(to)))
+func handle(w http.ResponseWriter, r *http.Request) {
+ req, err := http.NewRequest(http.MethodGet, "https://mi.within.website/api/switches/current/text", nil)
if err != nil {
- return err
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
}
- req.Header.Add("Authorization", *miToken)
- resp, err := http.DefaultClient.Do(req)
+ token, err := os.ReadFile(*miTokenPath)
if err != nil {
- return err
- }
- if resp.StatusCode != http.StatusOK {
- return fmt.Errorf("wanted %d, got: %s", http.StatusOK, resp.Status)
- }
- return nil
-}
-
-func handle(w http.ResponseWriter, r *http.Request) {
- if r.Method == http.MethodPost {
- front, err := ioutil.ReadAll(r.Body)
- if err != nil {
- panic(err)
- }
- defer r.Body.Close()
-
- err = miSwitch(string(front))
- if err != nil {
- panic(err)
- }
-
- w.Header().Set("Content-Type", "text/plain")
- fmt.Fprint(w, string(front))
+ http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
- req, err := http.NewRequest(http.MethodGet, "https://mi.within.website/api/switches/current/text", nil)
- if err != nil {
- panic(err)
- }
- req.Header.Add("Authorization", *miToken)
+ req.Header.Add("Authorization", string(token))
req.Header.Add("Accept", "text/plain")
resp, err := http.DefaultClient.Do(req)
if err != nil {
- panic(err)
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
}
if resp.StatusCode != http.StatusOK {
- log.Panicf("bad status code: %d", resp.StatusCode)
+ http.Error(w, fmt.Sprintf("bad status code: %d", resp.StatusCode), http.StatusInternalServerError)
+ return
}
w.Header().Set("Content-Type", "text/plain")
diff --git a/flake.nix b/flake.nix
index 3d6cb92..02eace4 100644
--- a/flake.nix
+++ b/flake.nix
@@ -18,8 +18,7 @@
};
};
- outputs =
- { self, nixpkgs, utils, gomod2nix, rust-overlay }@attrs:
+ outputs = { self, nixpkgs, utils, gomod2nix, rust-overlay }@attrs:
utils.lib.eachSystem [
"x86_64-linux"
"aarch64-linux"
@@ -87,6 +86,7 @@
quickserv = copyFile { pname = "quickserv"; };
todayinmarch2020 = copyFile { pname = "todayinmarch2020"; };
uploud = copyFile { pname = "uploud"; };
+ whoisfront = copyFile { pname = "whoisfront"; };
within-website = copyFile { pname = "within.website"; };
};
@@ -170,7 +170,7 @@
basic = pkgs.nixosTest ({
name = "basic-tests";
nodes.default = { config, pkgs, ... }: {
- imports = [ common self.nixosModules.default];
+ imports = [ common self.nixosModules.default ];
xeserv.services = {
aegis.enable = true;