-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remind users to get new kjudge version (gitea-104)
Refactor admin/home a bit fix typo and error handler add lastUpdate to avoid getting too frequently implement a message for admin to get the newest kjudge version Co-authored-by: Natsu Kagami <[email protected]> Co-authored-by: Thang Pham <[email protected]>
- Loading branch information
1 parent
07c819c
commit 1057b55
Showing
4 changed files
with
93 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// +build !production | ||
|
||
package admin | ||
|
||
// NewVersionMessageGet checks if there is a new version of kjudge | ||
func NewVersionMessageGet() (string, error) { | ||
return "", nil | ||
} |
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,50 @@ | ||
// +build production | ||
|
||
package admin | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"git.nkagami.me/natsukagami/kjudge" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type jsonRelease struct { | ||
TagName string `json:"tag_name"` | ||
} | ||
|
||
type version struct { | ||
TagName string | ||
LastUpdate time.Time | ||
} | ||
|
||
var currentVersion version | ||
|
||
// NewVersionMessageGet checks if there is a new version of kjudge | ||
func NewVersionMessageGet() (string, error) { | ||
if currentVersion.TagName == "" || time.Now().After(currentVersion.LastUpdate.Add(time.Hour)) { | ||
response, err := http.Get("https://git.nkagami.me/api/v1/repos/natsukagami/kjudge/releases?page=1&per_page=1") | ||
if err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
defer response.Body.Close() | ||
var x []jsonRelease | ||
decode := json.NewDecoder(response.Body) | ||
if err := decode.Decode(&x); err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
if len(x) == 0 { | ||
return "", errors.New("Found no relase") | ||
} | ||
currentVersion.TagName = x[0].TagName[1:] | ||
currentVersion.LastUpdate = time.Now() | ||
} | ||
if kjudge.Version != currentVersion.TagName { | ||
return fmt.Sprintf("Please update kjudge to the newest version (%s)", currentVersion.TagName), nil | ||
} else { | ||
return "", nil | ||
} | ||
} |