Skip to content

Commit

Permalink
Optimize the web app code.
Browse files Browse the repository at this point in the history
  • Loading branch information
chayev committed Mar 21, 2023
1 parent 4f17393 commit 27ca893
Showing 1 changed file with 44 additions and 55 deletions.
99 changes: 44 additions & 55 deletions webapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func main() {

log.Println("Listening on :8080...")
http.HandleFunc("/", handler)
http.HandleFunc("/android", handler)
http.HandleFunc("/results", viewResults)
http.HandleFunc("/android", android)
http.HandleFunc("/android-results", viewResultsAndroid)
http.HandleFunc("/android-results", viewResults)
log.Fatal(http.ListenAndServe(":8080", nil))
}

Expand All @@ -34,34 +34,60 @@ func handler(w http.ResponseWriter, r *http.Request) {

content := &PageOutput{CurrentTime: time.Now()}

t, _ := template.ParseFiles("tpl/home.html", "tpl/partials/header.html", "tpl/partials/footer.html", "tpl/partials/navToAndroid.html")
t.ExecuteTemplate(w, "home.html", &content)
// Determine if the request is for Android validation
isAndroid := r.URL.Path == "/android"

var templateName string
if isAndroid {
templateName = "android.html"
} else {
templateName = "home.html"
}

t, _ := template.ParseFiles("tpl/"+templateName, "tpl/partials/header.html", "tpl/partials/footer.html", "tpl/partials/navToAndroid.html", "tpl/partials/navToiOS.html")
t.ExecuteTemplate(w, templateName, &content)
}

func viewResults(w http.ResponseWriter, r *http.Request) {

var url string
var prefix string
var bundle string
var isAndroid bool

for _, n := range r.URL.Query()["url"] {
url = n
// Determine if the request is for iOS or Android validation
if r.URL.Path == "/results" {
isAndroid = false
} else if r.URL.Path == "/android-results" {
isAndroid = true
} else {
// Invalid request path
http.Error(w, "Invalid request path", http.StatusBadRequest)
return
}

for _, n := range r.URL.Query()["prefix"] {
prefix = n
// Parse form data from request body
err := r.ParseForm()
if err != nil {
http.Error(w, "Error parsing form data", http.StatusBadRequest)
return
}

for _, n := range r.URL.Query()["bundle"] {
bundle = n
}
// Get URL, prefix, and bundle parameters from form data
url = r.Form.Get("url")
prefix = r.Form.Get("prefix")
bundle = r.Form.Get("bundle")

var output []string

if url == "" {
output = append(output, "Enter URL to validate.")
} else {
output = yurllib.CheckAASADomain(url, prefix, bundle, true)
if isAndroid {
output = yurllib.CheckAssetLinkDomain(url, prefix, bundle)
} else {
output = yurllib.CheckAASADomain(url, prefix, bundle, true)
}
}

content := &PageOutput{URL: url, Prefix: prefix, Bundle: bundle}
Expand All @@ -72,52 +98,15 @@ func viewResults(w http.ResponseWriter, r *http.Request) {

content.CurrentTime = time.Now()

t, _ := template.ParseFiles("tpl/results.html", "tpl/partials/header.html", "tpl/partials/footer.html", "tpl/partials/navToAndroid.html")
t.ExecuteTemplate(w, "results.html", &content)
}

func android(w http.ResponseWriter, r *http.Request) {
var templateName string

content := &PageOutput{CurrentTime: time.Now()}

t, _ := template.ParseFiles("tpl/android.html", "tpl/partials/header.html", "tpl/partials/footer.html", "tpl/partials/navToiOS.html")
t.ExecuteTemplate(w, "android.html", &content)
}

func viewResultsAndroid(w http.ResponseWriter, r *http.Request) {

var url string
var package_name string
var fingerprint string

for _, n := range r.URL.Query()["url"] {
url = n
}

for _, n := range r.URL.Query()["prefix"] {
package_name = n
}

for _, n := range r.URL.Query()["bundle"] {
fingerprint = n
}

var output []string

if url == "" {
output = append(output, "Enter URL to validate.")
if isAndroid {
templateName = "android-results.html"
} else {
output = yurllib.CheckAssetLinkDomain(url, package_name, fingerprint)
templateName = "results.html"
}

content := &PageOutput{URL: url, Prefix: package_name, Bundle: fingerprint}

for _, item := range output {
content.Content += item
}

content.CurrentTime = time.Now()
t, _ := template.ParseFiles("tpl/"+templateName, "tpl/partials/header.html", "tpl/partials/footer.html", "tpl/partials/navToAndroid.html", "tpl/partials/navToiOS.html")

t, _ := template.ParseFiles("tpl/android-results.html", "tpl/partials/header.html", "tpl/partials/footer.html", "tpl/partials/navToiOS.html")
t.ExecuteTemplate(w, "android-results.html", &content)
t.ExecuteTemplate(w, templateName, &content)
}

0 comments on commit 27ca893

Please sign in to comment.