From ffa70e8f551863385391d288031cbefaffdc62ea Mon Sep 17 00:00:00 2001 From: CronyAkatsuki Date: Mon, 30 Dec 2024 21:04:04 +0100 Subject: [PATCH] Get basic webserver going. --- README.md | 8 +++++++- main.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5e97bdd..1e8543c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # Goffy -Basic iffy.cert.hr front, for now just a cli tool. +> Basic iffy.cert.hr front, for now just a cli tool. + +Improved to simpler webserver that runs on port 3333. + +To get result on url, just run 127.0.0.1:3333/result?url=ekupi.hr for example, and you will get result. + +Next to get some http templating setup. diff --git a/main.go b/main.go index 4740e9b..7db0ad8 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -14,11 +15,33 @@ type Result struct { } func main() { + mux := http.NewServeMux() + mux.HandleFunc("/", getRoot) + mux.HandleFunc("/result", getResult) + + err := http.ListenAndServe(":3333", mux) + if errors.Is(err, http.ErrServerClosed) { + fmt.Println("server closed") + } else if err != nil { + panic(err) + } +} + +func getResult(w http.ResponseWriter, r *http.Request) { + // Get the url from query + url := r.URL.Query().Get("url") + if url == "" { + fmt.Println("got /result request") + io.WriteString(w, "This is result page!") + return + } + // Url to send request to iffy_url := "https://iffy.cert.hr/validate" // Json to send - jsonData := []byte(`{"match": "ekupi.hr"}`) + jsonString := `{"match": "` + url + `"}` + jsonData := []byte(jsonString) // Creating a http request, and making a http request request, err := http.NewRequest("POST", iffy_url, bytes.NewBuffer(jsonData)) @@ -41,5 +64,12 @@ func main() { panic(err) } + fmt.Println("got /result request") fmt.Println(result.Message) + io.WriteString(w, result.Message) +} + +func getRoot(w http.ResponseWriter, r *http.Request) { + fmt.Println("got / request") + io.WriteString(w, "This is my website!") }