Get basic webserver going.
This commit is contained in:
parent
cc55c7b1b6
commit
ffa70e8f55
@ -1,3 +1,9 @@
|
|||||||
# Goffy
|
# 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.
|
||||||
|
32
main.go
32
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -14,11 +15,33 @@ type Result struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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
|
// Url to send request to
|
||||||
iffy_url := "https://iffy.cert.hr/validate"
|
iffy_url := "https://iffy.cert.hr/validate"
|
||||||
|
|
||||||
// Json to send
|
// Json to send
|
||||||
jsonData := []byte(`{"match": "ekupi.hr"}`)
|
jsonString := `{"match": "` + url + `"}`
|
||||||
|
jsonData := []byte(jsonString)
|
||||||
|
|
||||||
// Creating a http request, and making a http request
|
// Creating a http request, and making a http request
|
||||||
request, err := http.NewRequest("POST", iffy_url, bytes.NewBuffer(jsonData))
|
request, err := http.NewRequest("POST", iffy_url, bytes.NewBuffer(jsonData))
|
||||||
@ -41,5 +64,12 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("got /result request")
|
||||||
fmt.Println(result.Message)
|
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!")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user