Skip to content

Commit

Permalink
GET, POST, and greet example.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Nov 5, 2023
1 parent 570b1d6 commit 9449c18
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 13 deletions.
34 changes: 33 additions & 1 deletion ankovm/webfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ankovm

import (
"fmt"
"io"
"net/http"
"os"
"strings"

"github.com/mattn/anko/env"
"github.com/mattn/anko/parser"
Expand Down Expand Up @@ -58,6 +60,7 @@ func httpHeaderFn(r *http.Request) func() map[string][]string {
func httpRemoteFn(r *http.Request) func() map[string]interface{} {
return func() map[string]interface{} {
dict := make(map[string]interface{})
body, err := io.ReadAll(r.Body)

dict["method"] = r.Method
dict["host"] = r.Host
Expand All @@ -67,9 +70,38 @@ func httpRemoteFn(r *http.Request) func() map[string]interface{} {
dict["encoding"] = r.TransferEncoding
dict["url"] = r.URL.String()
dict["form"] = r.Form
dict["post_data"] = r.PostForm
dict["cookies"] = r.Cookies()

if err == nil {
body := string(body)
dict["body"] = body
}

if r.Method == "POST" && dict["body"] != nil {
entries := strings.Split(string(body), "&")
post := make(map[string]string)

for i := 0; i < len(entries); i++ {
pair := strings.Split(entries[i], "=")
post[pair[0]] = pair[1]
}

dict["post_data"] = post
} else if r.Method == "GET" {
content := r.RequestURI
content = content[strings.Index(content, "?")+1:]

entries := strings.Split(content, "&")
get := make(map[string]string)

for i := 0; i < len(entries); i++ {
pair := strings.Split(entries[i], "=")
get[pair[0]] = pair[1]
}

dict["get_data"] = get
}

return dict
}
}
33 changes: 33 additions & 0 deletions examples/get_method/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<head>
<title>Forms Example</title>

<style>
form {
margin-top: 21%;
font-family: Tahoma;
}

input[type="text"] {
border: 1px solid #1c1d1f;
border-right: 0px;
padding: 4px;
}

input[type="submit"] {
background-color: #1c1d1f;
color: white;
border: 0px;
padding: 5px;
margin-left: -5px;
}
</style>
</head>
<body>
<form action="receiver.awp" method="get" align="center">
<h2>GET Method Example</h2>
<input type="text" name="name" placeholder="Enter your name" />
<input type="submit" />
</form>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/get_method/receiver.awp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<head>
<title>Form GET Example</title>

<style>
* {font-family: Tahoma}
h2 {margin-top: 21%}

a {
text-decoration: none;
color: blue;
}
</style>
</head>
<body>
<center>
<h2>Hello, <% echo(httpRemote()["get_data"]["name"]); %>!</h2>
<a href="/">&lt; Go Back</a>
</center>
</body>
</html>
28 changes: 16 additions & 12 deletions examples/greet/index.awp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<style>
h1 {
margin-top: 24%;
font-family: Tahoma;
}
</style>
<%

include("say.awp");
say("<h1 align=\"center\">Hello, from AnkoWeb!</h1>");

%>
<!DOCTYPE html>
<head>
<style>
h1 {
margin-top: 24%;
font-family: Tahoma;
}
</style>
</head>
<body>
<%
include("say.awp");
say("<h1 align=\"center\">Hello, from AnkoWeb!</h1>");
%>
</body>
</html>
33 changes: 33 additions & 0 deletions examples/post_method/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<head>
<title>Form POST Example</title>

<style>
form {
margin-top: 21%;
font-family: Tahoma;
}

input[type="text"] {
border: 1px solid #1c1d1f;
border-right: 0px;
padding: 4px;
}

input[type="submit"] {
background-color: #1c1d1f;
color: white;
border: 0px;
padding: 5px;
margin-left: -5px;
}
</style>
</head>
<body>
<form action="receiver.awp" method="post" align="center">
<h2>POST Method Example</h2>
<input type="text" name="name" placeholder="Enter your name" />
<input type="submit" />
</form>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/post_method/receiver.awp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<head>
<title>Form POST Example</title>

<style>
* {font-family: Tahoma}
h2 {margin-top: 21%}

a {
text-decoration: none;
color: blue;
}
</style>
</head>
<body>
<center>
<h2>Hello, <% echo(httpRemote()["post_data"]["name"]); %>!</h2>
<a href="/">&lt; Go Back</a>
</center>
</body>
</html>

0 comments on commit 9449c18

Please sign in to comment.