diff --git a/client/client_test.go b/client/client_test.go index ab65c2a..5e949a5 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io/ioutil" "net/http" "os" "os/exec" @@ -335,3 +336,40 @@ func TestAdvancedQuery(t *testing.T) { // TODO: test the username,hostname atoms } + +func TestGithubRedirects(t *testing.T) { + // Set up + defer shared.BackupAndRestore(t)() + defer shared.RunTestServer(t)() + + // Check the redirects + client := &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + resp, err := client.Get("http://localhost:8080/download/hishtory-linux-amd64") + shared.Check(t, err) + if resp.StatusCode != 302 { + t.Fatalf("expected endpoint to return redirect") + } + locationHeader := resp.Header.Get("location") + if !strings.Contains(locationHeader, "https://github.com/ddworken/hishtory/releases/download/v") { + t.Fatalf("expected location header to point to github") + } + if !strings.HasSuffix(locationHeader, "/hishtory-linux-amd64") { + t.Fatalf("expected location header to point to binary") + } + + // And retrieve it and check we can do that + resp, err = http.Get("http://localhost:8080/download/hishtory-linux-amd64") + shared.Check(t, err) + if resp.StatusCode != 200 { + t.Fatalf("didn't return a 200 status code, status_code=%d", resp.StatusCode) + } + respBody, err := ioutil.ReadAll(resp.Body) + shared.Check(t, err) + if len(respBody) < 5_000_000 { + t.Fatalf("response is too short to be a binary, resp=%d", len(respBody)) + } +} diff --git a/server/server.go b/server/server.go index 0195b46..fa1fa5c 100644 --- a/server/server.go +++ b/server/server.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "time" "github.com/ddworken/hishtory/shared" _ "github.com/lib/pq" @@ -133,12 +134,47 @@ func OpenDB() (*gorm.DB, error) { } func init() { - if ReleaseVersion == "UNKNOWN" { + if ReleaseVersion == "UNKNOWN" && !isTestEnvironment() { panic("server.go was built without a ReleaseVersion!") } + go keepReleaseVersionUpToDate() InitDB() } +func keepReleaseVersionUpToDate() { + for { + updateReleaseVersion() + time.Sleep(10 * time.Minute) + } +} + +type releaseInfo struct { + Name string `json:"name"` +} + +func updateReleaseVersion() { + resp, err := http.Get("https://api.github.com/repos/ddworken/hishtory/releases/latest") + if err != nil { + fmt.Printf("failed to get latest release version: %v\n", err) + return + } + if resp.StatusCode != 200 { + fmt.Printf("failed to call github API, status_code=%d\n", resp.StatusCode) + return + } + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Printf("failed to read github API response body: %v\n", err) + return + } + var info releaseInfo + err = json.Unmarshal(respBody, &info) + if err != nil { + fmt.Printf("failed to parse github API response: %v", err) + } + ReleaseVersion = info.Name +} + func InitDB() { var err error GLOBAL_DB, err = OpenDB() diff --git a/shared/testutils.go b/shared/testutils.go index f33cc24..d070eae 100644 --- a/shared/testutils.go +++ b/shared/testutils.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path" + "strings" "testing" "time" ) @@ -75,6 +76,9 @@ func RunTestServer(t *testing.T) func() { if err != nil { t.Fatalf("failed to kill process: %v", err) } + if strings.Contains(stderr.String()+stdout.String(), "failed to") { + t.Fatalf("server failed to do something: stderr=%#v, stdout=%#v", stderr.String(), stdout.String()) + } fmt.Printf("stderr=%#v, stdout=%#v\n", stderr.String(), stdout.String()) } }