This repository has been archived on 2024-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
ambition-legacy/frontend/server/server.go

51 lines
1 KiB
Go
Raw Permalink Normal View History

package server
import (
"bytes"
"encoding/json"
2023-08-05 12:45:59 +01:00
"fmt"
"io/ioutil"
"net/http"
2023-08-05 12:45:59 +01:00
"os"
// Stylish stuff
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
// Needed for server url
_ "github.com/joho/godotenv/autoload"
)
2023-08-05 12:45:59 +01:00
// Define a bold green style
var bold = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("2"))
var server_address = os.Getenv("BACKEND_URL")
func GetUserJwtToken(name, password string) (string, error) {
if server_address == "" {
2023-08-05 12:45:59 +01:00
log.Info(fmt.Sprintf("Make sure you've set the %s environment variable!", bold.Render("BACKEND_URL")))
log.Fatal("No server URL provided!")
}
2023-08-05 12:45:59 +01:00
post_body, _ := json.Marshal(map[string]string{
2023-08-05 12:45:59 +01:00
"name": name,
"password": password,
})
resp_body := bytes.NewBuffer(post_body)
2023-08-05 12:45:59 +01:00
resp, err := http.Post(server_address+"/user", "application/json", resp_body)
if err != nil {
return "", err
}
defer resp.Body.Close()
2023-08-05 12:45:59 +01:00
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
2023-08-05 12:45:59 +01:00
return string(body), nil
2023-08-05 12:45:59 +01:00
}