Skip to content

Commit

Permalink
feat: add ping page
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jun 14, 2022
1 parent 687caa5 commit 6c48db2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
16 changes: 10 additions & 6 deletions server/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,39 @@ import (

const (
INTERNAL_PAGE_HOME string = "/"
INTERNAL_PAGE_PING = "/_/ping/"
)

const (
TYPE_NOT_FOUND int = 0
TYPE_HOME = 1
TYPE_PING = 2
)

func isInternalUrls(url string) bool {
if strings.Contains(url, "ubuntu") || strings.Contains(url, "debian") {
return false
}
return url == INTERNAL_PAGE_HOME
return url == INTERNAL_PAGE_HOME || url == INTERNAL_PAGE_PING
}

func getInternalResType(url string) int {
if url == INTERNAL_PAGE_HOME {
return TYPE_HOME
}

if url == INTERNAL_PAGE_PING {
return TYPE_PING
}

return TYPE_NOT_FOUND
}

func renderInternalUrls(url string, rw *http.ResponseWriter) {
types := getInternalResType(url)
if types == TYPE_NOT_FOUND {
return
}

if types == TYPE_HOME {
} else if types == TYPE_HOME {
cacheSize, _ := system.DirSize("./.aptcache")
files, _ := ioutil.ReadDir("./.aptcache/header/v1")
available, _ := system.DiskAvailable()
Expand All @@ -54,7 +58,7 @@ func renderInternalUrls(url string, rw *http.ResponseWriter) {
system.HumanFileSize(memoryUsage),
goroutine,
))
return
} else if types == TYPE_PING {
io.WriteString(*rw, "pong")
}

}
7 changes: 1 addition & 6 deletions server/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ func (ap *AptProxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
var rule *linux.Rule
if isInternalUrls(r.URL.Path) {
rule = nil

renderInternalUrls(r.URL.Path, &rw)

if getInternalResType(r.URL.Path) == TYPE_HOME {
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
}

rw.Header().Set("Content-Type", "text/html; charset=utf-8")
} else {
rule, match := linux.MatchingRule(r.URL.Path, ap.Rules)
if match {
Expand Down

0 comments on commit 6c48db2

Please sign in to comment.