about summary refs log tree commit diff
path: root/users/wpcarro/tools/monzo_ynab/tokens.go
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-11-21T05·24-0800
committerclbot <clbot@tvl.fyi>2022-11-21T05·30+0000
commitd36aaeb9679a2bc6dfadc53928fe08f081203c0f (patch)
tree100495021a91abaa84bb5a4cdecc36df56f4a637 /users/wpcarro/tools/monzo_ynab/tokens.go
parent982022826da18904092be5e282f538662802228b (diff)
fix(wpcarro/ynab): Remove .skip-subtree r/5293
**TL;DR:**
- Delete half-baked packaging attempts (`job.nix`, `token.nix`).
- Ensure golang code compiles.
  - Some "packages" were being treated like "programs" presumably for
    debugging/testing purposes back when I was working on this. Make those
    behave like libraries.
  - Remove stale imports.
  - Fix syntax errors.
  - Miscellaneous other chores.
- Drop `shell.nix` and `use_nix` directive.

Change-Id: I63c275680bac55a3cad3b9cb48d51cdc431fbe48
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7318
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
Reviewed-by: wpcarro <wpcarro@gmail.com>
Diffstat (limited to 'users/wpcarro/tools/monzo_ynab/tokens.go')
-rw-r--r--users/wpcarro/tools/monzo_ynab/tokens.go36
1 files changed, 16 insertions, 20 deletions
diff --git a/users/wpcarro/tools/monzo_ynab/tokens.go b/users/wpcarro/tools/monzo_ynab/tokens.go
index 4be967ccb803..01b57d3daafd 100644
--- a/users/wpcarro/tools/monzo_ynab/tokens.go
+++ b/users/wpcarro/tools/monzo_ynab/tokens.go
@@ -1,7 +1,7 @@
 // Creating a Tokens server to manage my access and refresh tokens. Keeping this
 // as a separate server allows me to develop and use the access tokens without
 // going through client authorization.
-package main
+package tokens
 
 ////////////////////////////////////////////////////////////////////////////////
 // Dependencies
@@ -46,7 +46,7 @@ type setTokensRequest struct {
 
 // This is our application state.
 type state struct {
-	accessToken  string `json:"access_token"`
+	AccessToken  string `json:"access_token"`
 	refreshToken string `json:"refresh_token"`
 }
 
@@ -90,7 +90,7 @@ func logTokens(access string, refresh string) {
 }
 
 func (state *state) String() string {
-	return fmt.Sprintf("state{\n\taccessToken: \"%s\",\n\trefreshToken: \"%s\"\n}\n", state.accessToken, state.refreshToken)
+	return fmt.Sprintf("state{\n\tAccessToken: \"%s\",\n\trefreshToken: \"%s\"\n}\n", state.AccessToken, state.refreshToken)
 }
 
 // Schedule a token refresh for `expiresIn` seconds using the provided
@@ -104,10 +104,10 @@ func scheduleTokenRefresh(expiresIn int, refreshToken string) {
 	log.Printf("Scheduling token refresh for %v\n", timestamp)
 	time.Sleep(duration)
 	log.Println("Refreshing tokens now...")
-	accessToken, refreshToken := refreshTokens(refreshToken)
+	AccessToken, refreshToken := refreshTokens(refreshToken)
 	log.Println("Successfully refreshed tokens.")
-	logTokens(accessToken, refreshToken)
-	setState(accessToken, refreshToken)
+	logTokens(AccessToken, refreshToken)
+	setState(AccessToken, refreshToken)
 }
 
 // Exchange existing credentials for a new access token and `refreshToken`. Also
@@ -169,8 +169,8 @@ func handleInterrupts() {
 	go func() {
 		sig := <-sigs
 		log.Printf("Received signal to shutdown. %v\n", sig)
-		state := getState()
-		persistTokens(state.accessToken, state.refreshToken)
+		state := GetState()
+		persistTokens(state.AccessToken, state.refreshToken)
 		done <- true
 	}()
 
@@ -179,25 +179,21 @@ func handleInterrupts() {
 	os.Exit(0)
 }
 
-// Set `accessToken` and `refreshToken` on application state.
-func setState(accessToken string, refreshToken string) {
-	msg := writeMsg{state{accessToken, refreshToken}, make(chan bool)}
+// Set `AccessToken` and `refreshToken` on application state.
+func setState(AccessToken string, refreshToken string) {
+	msg := writeMsg{state{AccessToken, refreshToken}, make(chan bool)}
 	chans.writes <- msg
 	<-msg.sender
 }
 
 // Return our application state.
-func getState() state {
+func GetState() state {
 	msg := readMsg{make(chan state)}
 	chans.reads <- msg
 	return <-msg.sender
 }
 
-////////////////////////////////////////////////////////////////////////////////
-// Main
-////////////////////////////////////////////////////////////////////////////////
-
-func main() {
+func StartServer() {
 	// Manage application state.
 	go func() {
 		state := &state{}
@@ -215,7 +211,7 @@ func main() {
 				// As an attempt to maintain consistency between application
 				// state and persisted state, everytime we write to the
 				// application state, we will write to the store.
-				persistTokens(state.accessToken, state.refreshToken)
+				persistTokens(state.AccessToken, state.refreshToken)
 				msg.sender <- true
 			}
 		}
@@ -251,7 +247,7 @@ func main() {
 	log.Fatal(http.ListenAndServe(":4242",
 		http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 			if req.URL.Path == "/refresh-tokens" && req.Method == "POST" {
-				state := getState()
+				state := GetState()
 				go scheduleTokenRefresh(0, state.refreshToken)
 				fmt.Fprintf(w, "Done.")
 			} else if req.URL.Path == "/set-tokens" && req.Method == "POST" {
@@ -273,7 +269,7 @@ func main() {
 			} else if req.URL.Path == "/state" && req.Method == "GET" {
 				// TODO(wpcarro): Ensure that this returns serialized state.
 				w.Header().Set("Content-type", "application/json")
-				state := getState()
+				state := GetState()
 				payload, _ := json.Marshal(state)
 				io.WriteString(w, string(payload))
 			} else {