Skip to content

Commit

Permalink
Finished writing support for grades
Browse files Browse the repository at this point in the history
  • Loading branch information
demostanis committed Oct 14, 2020
1 parent f4aea29 commit 02d8219
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
29 changes: 24 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func main() {
func index(c echo.Context) error {
torOnlyEnabled := c.QueryParam("toronly") == "on"
torEnabled := torOnlyEnabled || c.QueryParam("tor") == "on"
gradesEnabled := *new([]string)
for _, thisGrade := range grade.Grades() {
if c.QueryParam(thisGrade["Id"].(string)) == "on" {
gradesEnabled = append(gradesEnabled, thisGrade["Id"].(string))
}
}
if len(gradesEnabled) < 1 {
gradesEnabled = grade.Defaults()
}
blacklist := *new([]string)

if b := c.QueryParam("blacklist"); len(b) > 0 {
Expand All @@ -68,14 +77,23 @@ func index(c echo.Context) error {
for key, instance := range *fetchedInstances {
if instance.Error == nil && instance.Version != nil {
stop := false
for _, blacklisted := range blacklist {
if len(strings.TrimSpace(blacklisted)) < 1 {
continue
}
if r, err := regexp.Compile(blacklisted); err == nil && r.MatchString(key) {

for i, thisGrade := range gradesEnabled {
if instance.Html.Grade != grade.Symbol(thisGrade) && len(gradesEnabled) -1 == i {
stop = true
}
}

if !stop {
for _, blacklisted := range blacklist {
if len(strings.TrimSpace(blacklisted)) < 1 {
continue
}
if r, err := regexp.Compile(blacklisted); err == nil && r.MatchString(key) {
stop = true
}
}
}
if !stop {
if torEnabled && instance.NetworkType == "tor" {
keys = append(keys, key)
Expand Down Expand Up @@ -106,6 +124,7 @@ func index(c echo.Context) error {
},
"GradeComment": grade.Comment(randInstance.Html.Grade),
"Grades": grade.Grades(),
"GradesSelected": gradesEnabled,
})
} else {
return c.Render(http.StatusTooEarly, "index.html", map[string]bool{
Expand Down
14 changes: 14 additions & 0 deletions pkg/grade/grade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ func Grades() []map[string]interface{} {
}
}

func Symbol(id string) string {
var result string
for _, thisGrade := range Grades() {
if thisGrade["Id"] == id {
result = thisGrade["Symbol"].(string)
}
}
return result
}

func Defaults() []string {
return []string{"grade-v", "grade-c"}
}

func Comment(grade string) string {
switch grade {
case "V":
Expand Down
5 changes: 3 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ <h3>Configuration panel</h3>
*://another.unwanted.instance.cn/{{end}}</textarea><br /><br />

<label for="grades">Grades: </label>
{{range .Grades}}
<input type="checkbox" name="{{.Id}}">{{.Symbol}}
{{$selected := .GradesSelected}}
{{range $grade := .Grades}}
<input type="checkbox" name="{{.Id}}"{{range $thisGrade := $selected}}{{if eq $thisGrade $grade.Id}}checked{{end}}{{end}}>{{.Symbol}}
{{end}}

<input type="submit" value="Save">
Expand Down

0 comments on commit 02d8219

Please sign in to comment.