This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added multiple goroutines to thread different types of work, updating…
…, querying api and getting tokens....
- Loading branch information
1 parent
c9bea09
commit 7cb493f
Showing
5 changed files
with
254 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package rest | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type Config struct { | ||
Sections []Section `json:"sections"` | ||
} | ||
|
||
//JSON Returns the Marshalled Version of the Response | ||
func (r *Config) JSON() ([]byte, error) { | ||
payload, err := json.Marshal(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return payload, nil | ||
} | ||
|
||
type Section struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
Components []Component `json:"components"` | ||
} | ||
|
||
type Component struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
Value interface{} `json:"value"` | ||
} | ||
|
||
type Status struct { | ||
Healthy bool `json:"status"` | ||
} | ||
|
||
//JSON Returns the Marshalled Version of the Response | ||
func (r *Status) JSON() ([]byte, error) { | ||
payload, err := json.Marshal(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return payload, nil | ||
} | ||
|
||
func StatusResponse(w http.ResponseWriter, r *http.Request) { | ||
resBody := Status{ | ||
Healthy: true, | ||
} | ||
|
||
json, _ := resBody.JSON() | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(json) | ||
} | ||
|
||
func ConfigResponseMain(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodPost { | ||
// Declare a new Person struct. | ||
var sect Section | ||
|
||
// Try to decode the request body into the struct. If there is an error, | ||
// respond to the client with the error message and a 400 status code. | ||
err := json.NewDecoder(r.Body).Decode(§) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
} | ||
} | ||
|
||
func ConfigResponse(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
resBody := Config{ | ||
Sections: []Section{ | ||
{ | ||
ID: 15, | ||
Name: "Main", | ||
Components: []Component{ | ||
{ | ||
ID: 1, | ||
Name: "Region", | ||
Type: "select", | ||
Value: []string{"GBR", "USA"}, | ||
}, | ||
{ | ||
ID: 1, | ||
Name: "Model", | ||
Type: "select", | ||
Value: []string{"3080", "3090"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: 15, | ||
Name: "Discord", | ||
Components: []Component{ | ||
{ | ||
ID: 1, | ||
Name: "Webhook URL", | ||
Type: "input", | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: 15, | ||
Name: "Twilio", | ||
Components: []Component{ | ||
{ | ||
ID: 1, | ||
Name: "Source", | ||
Type: "input", | ||
}, | ||
{ | ||
ID: 1, | ||
Name: "Destination", | ||
Type: "input", | ||
}, | ||
{ | ||
ID: 1, | ||
Name: "API Key", | ||
Type: "input", | ||
}, | ||
{ | ||
ID: 1, | ||
Name: "Account SID", | ||
Type: "input", | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: 15, | ||
Name: "Telegram", | ||
Components: []Component{ | ||
{ | ||
ID: 1, | ||
Name: "API Key", | ||
Type: "input", | ||
}, | ||
{ | ||
ID: 1, | ||
Name: "Chat ID", | ||
Type: "input", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
json, _ := resBody.JSON() | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(json) | ||
} | ||
|
||
if r.Method == http.MethodPost { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package update | ||
|
||
import ( | ||
"log" | ||
"math/rand" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/inconshreveable/go-update" | ||
) | ||
|
||
// FetchApply Fetches and applys any updates from GitHub releases to this program in place. | ||
func FetchApply(url string, wg *sync.WaitGroup) error { | ||
defer wg.Done() | ||
|
||
for { | ||
log.Println("Attempting to fetch updates from github") | ||
doUpdate(url) | ||
sleep(60000) | ||
} | ||
} | ||
|
||
func doUpdate(url string) error { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
err = update.Apply(resp.Body, update.Options{}) | ||
if err != nil { | ||
log.Println("Error applying updates from github.") | ||
} | ||
return err | ||
} | ||
|
||
func sleep(delay int64) { | ||
// Force a randomized jitter of up to 5 seconds to avoid looking like a bot. | ||
rand.Seed(time.Now().UnixNano()) | ||
n := rand.Intn(5) | ||
|
||
ns := time.Duration(n) * time.Second | ||
ds := time.Duration(delay/1000) * time.Second | ||
time.Sleep(time.Duration(ns + ds)) | ||
} |