tsk-5: Fetch token (#9)

Closes #5

Reviewed-on: #9
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-11-28 16:54:11 +00:00
committed by phoenix
parent c47941acaf
commit cef7f8a150
9 changed files with 171 additions and 13 deletions
+51
View File
@@ -0,0 +1,51 @@
package app
import (
"fmt"
"os"
"path"
"github.com/joho/godotenv"
)
const App_Name = "catapult"
type App struct {
ApiUrl string
AuthUrl string
ServiceUsername string
ServicePassphrase string
}
func Load() (*App, error) {
err := godotenv.Load()
if err != nil {
cwd, _ := os.Getwd()
envPath := path.Join(cwd, "../..", ".env")
if err = godotenv.Load(envPath); err != nil {
prevPath := path.Join(envPath, "../..", ".env")
if err = godotenv.Load(prevPath); err != nil {
return nil, fmt.Errorf("Error loading .env file: %w", err)
}
}
}
apiUrl := os.Getenv("API_URL")
authUrl := os.Getenv("AUTH_URL")
serviceUsername := os.Getenv("SERVICE_USERNAME")
servicePassphrase := os.Getenv("SERVICE_PASSPHRASE")
if len(apiUrl) == 0 {
return nil, fmt.Errorf("Api Url not provided")
} else if len(authUrl) == 0 {
return nil, fmt.Errorf("Auth url not provided")
} else if len(serviceUsername) == 0 {
return nil, fmt.Errorf("Service username not provided")
} else if len(servicePassphrase) == 0 {
return nil, fmt.Errorf("Service passphrase not provided")
} else {
return &App{
ApiUrl: apiUrl, AuthUrl: authUrl, ServiceUsername: serviceUsername, ServicePassphrase: servicePassphrase,
}, nil
}
}
+55
View File
@@ -0,0 +1,55 @@
package auth
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"git.kundeng.us/phoenix/textsender-models/tx0/token"
"git.kundeng.us/phoenix/catapult/internal/app"
)
type Auth struct {
Application *app.App
}
type service struct {
Username string `json:"username"`
Passphrase string `json:"passphrase"`
}
type response struct {
Message string `json:"message"`
Data []*token.Login `json:"data"`
}
func (a *Auth) GetToken() (*token.Login, error) {
serv := service{
Username: a.Application.ServiceUsername,
Passphrase: a.Application.ServicePassphrase,
}
jsonData, err := json.Marshal(serv)
if err != nil {
return nil, err
}
resp, err := http.Post(
fmt.Sprintf("%s/api/v1/service/login", a.Application.AuthUrl),
"application/json",
bytes.NewBuffer(jsonData),
)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var r response
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
return r.Data[0], nil
}
+19 -11
View File
@@ -2,32 +2,31 @@ package config
import (
"context"
"flag"
"fmt"
"log"
"sync"
"time"
"git.kundeng.us/phoenix/textsender-models/tx0/token"
"git.kundeng.us/phoenix/catapult/internal/app"
"git.kundeng.us/phoenix/catapult/internal/auth"
)
const App_Name = "catapult"
func CheckVersionFlag() *bool {
versionFlag := flag.Bool("version", false, "Print version information")
flag.Parse()
return versionFlag
}
type Service struct {
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
App *app.App
token *token.Login
}
func NewService() *Service {
func NewService(application *app.App) *Service {
ctx, cancel := context.WithCancel(context.Background())
return &Service{
ctx: ctx,
cancel: cancel,
App: application,
}
}
@@ -55,6 +54,15 @@ func (s *Service) worker1() {
case <-ticker.C:
// Do some work
log.Println("Worker 1: Processing...")
if s.token == nil {
catapultAuth := auth.Auth{Application: s.App}
if token, err := catapultAuth.GetToken(); err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Access token:", token.AccessToken)
s.token = token
}
}
}
}
}
+12
View File
@@ -0,0 +1,12 @@
package config
import (
"flag"
)
func CheckVersionFlag() *bool {
versionFlag := flag.Bool("version", false, "Print version information")
flag.Parse()
return versionFlag
}