contoh query:
Last active
November 9, 2018 17:26
-
-
Save birowo/6bb492da885c6e889e0b270a6f961591 to your computer and use it in GitHub Desktop.
golang: seperti https://gist.github.com/birowo/22f8007e932c1b5fbc11107741926848 tetapi pakai chi library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"math" | |
"net/http" | |
"net/url" | |
"strconv" | |
"strings" | |
"github.com/go-chi/chi" | |
) | |
type Hitung struct { | |
Kali float64 `json:"kali,omitempty"` | |
Bagi float64 `json:"bagi,omitempty"` | |
Plus float64 `json:"plus,omitempty"` | |
Minus float64 `json:"minus,omitempty"` | |
} | |
type Lingkaran struct { | |
Keliling float64 `json:"keliling,omitempty"` | |
Luas float64 `json:"luas,omitempty"` | |
} | |
type Bola struct { | |
Luas float64 `json:"luas,omitempty"` | |
Isi float64 `json:"isi,omitempty"` | |
} | |
var hostPage = []byte(` | |
<html> | |
<head></head> | |
<body> | |
queries:<br> | |
<textarea id="qs"></textarea><br> | |
<button onclick="getJSON('/?query='+query(qs.value), result)">submit</button><br> | |
<br> | |
response:<br> | |
<textarea id="rs"></textarea> | |
<script> | |
function getJSON(url, cb){ | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200){ | |
//alert(this.responseText) | |
if(this.getResponseHeader('Content-Type')=='application/json') cb(JSON.parse(this.responseText)) | |
else alert('error: response must json') | |
} | |
} | |
xhr.open('GET', url); | |
xhr.send(); | |
} | |
function query(qs){ | |
return qs.replace(/[\r\n]+/g, '|').replace(/^\|+|\s+|\|+$/g, '') | |
} | |
function result(json){ | |
rs.value = JSON.stringify(json, null, '. ') | |
} | |
</script> | |
</body> | |
</html> | |
`) | |
func main() { | |
r1 := chi.NewRouter() | |
r1.Get("/hitung/angka1/{angka1}/angka2/{angka2}", func(w http.ResponseWriter, r *http.Request) { | |
angka1, _ := strconv.ParseFloat(chi.URLParam(r, "angka1"), 64) | |
angka2, _ := strconv.ParseFloat(chi.URLParam(r, "angka2"), 64) | |
prop := strings.Split(r.URL.Query().Get("prop"), ",") | |
hitung := &Hitung{} | |
for _, field := range prop { | |
switch field { | |
case "plus": | |
hitung.Plus = angka1 + angka2 | |
case "minus": | |
hitung.Minus = angka1 - angka2 | |
case "kali": | |
hitung.Kali = angka1 * angka2 | |
case "bagi": | |
hitung.Bagi = angka1 / angka2 | |
} | |
} | |
//println("hitung") | |
json.NewEncoder(w).Encode(hitung) | |
}) | |
r1.Get("/lingkaran/radius/{radius}", func(w http.ResponseWriter, r *http.Request) { | |
radius, _ := strconv.ParseFloat(chi.URLParam(r, "radius"), 64) | |
prop := strings.Split(r.URL.Query().Get("prop"), ",") | |
lingkaran := &Lingkaran{} | |
for _, field := range prop { | |
switch field { | |
case "keliling": | |
lingkaran.Keliling = 2 * math.Pi * radius | |
case "luas": | |
lingkaran.Luas = math.Pi * radius * radius | |
} | |
} | |
//println("lingkaran") | |
json.NewEncoder(w).Encode(lingkaran) | |
}) | |
r1.Get("/bola/radius/{radius}", func(w http.ResponseWriter, r *http.Request) { | |
radius, _ := strconv.ParseFloat(chi.URLParam(r, "radius"), 64) | |
prop := strings.Split(r.URL.Query().Get("prop"), ",") | |
bola := &Bola{} | |
for _, field := range prop { | |
switch field { | |
case "luas": | |
bola.Luas = 4 * math.Pi * radius * radius | |
case "isi": | |
bola.Isi = (4 / 3) * math.Pi * radius * radius * radius | |
} | |
} | |
//println("bola") | |
json.NewEncoder(w).Encode(bola) | |
}) | |
http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
query := r.URL.Query() | |
if len(query) == 0 { | |
w.Header().Set("Content-Type", "text/html") | |
w.Write(hostPage) | |
return | |
} | |
for k, v := range query { | |
switch k { | |
case "query": | |
urls := strings.Split(v[0], "|") | |
i, n := 0, len(urls) | |
w.Header().Set("Content-Type", "application/json") | |
w.Write([]byte("{")) | |
for i < n { | |
r.URL, _ = url.Parse(urls[i]) | |
//println(r.URL.Path) | |
w.Write([]byte(`"`)) | |
w.Write([]byte(r.URL.Path)) | |
w.Write([]byte(`":`)) | |
r1.ServeHTTP(w, r) | |
i++ | |
if i < n { | |
w.Write([]byte(",")) | |
} | |
} | |
w.Write([]byte("}")) | |
w.WriteHeader(http.StatusOK) | |
return | |
default: | |
http.Error(w, "resource not found", http.StatusNotFound) | |
} | |
} | |
})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment