app.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "time"
  11. )
  12. type Application struct {
  13. Name, AccessToken, Sid, BaseURL, AuthString string
  14. Languages map[string]string
  15. Delay time.Duration
  16. }
  17. func JsonDecoder(data io.ReadCloser, target interface{}) {
  18. decoder := json.NewDecoder(data)
  19. err := decoder.Decode(target)
  20. if err != nil {
  21. log.Printf("error reading json: %v", err)
  22. }
  23. }
  24. func (app *Application) LoadLanguages() {
  25. data, err := os.Open("./html/languages.json")
  26. defer data.Close()
  27. if err != nil {
  28. log.Printf("error reading languages: %v", err)
  29. return
  30. }
  31. app.Languages = make(map[string]string)
  32. JsonDecoder(data, &app.Languages)
  33. }
  34. func (app Application) CheckLanguage(language string) bool {
  35. _, ok := app.Languages[language]
  36. if !ok {
  37. return false
  38. }
  39. return true
  40. }
  41. func (app *Application) Login() {
  42. credentials, err := ioutil.ReadFile("./secrets.json")
  43. if err != nil {
  44. log.Printf("Error reading credentials: %v", err)
  45. }
  46. loginURL := app.BaseURL + "auth/login"
  47. req, err := http.NewRequest("POST", loginURL, bytes.NewBuffer(credentials))
  48. req.Header.Set("Content-Type", "application/json")
  49. client := &http.Client{}
  50. resp, err := client.Do(req)
  51. if err != nil {
  52. log.Printf("error logging in: %v", err)
  53. }
  54. defer resp.Body.Close()
  55. JsonDecoder(resp.Body, &app)
  56. app.AuthString = "?authToken=" + app.AccessToken
  57. log.Println(app.AuthString, resp.Status)
  58. }