この小さな file が本当に動く?
はい。この source そのものが page として動きます。
<%@ LANGUAGE="GND.Go.Yaegi" %>
<%
import . "OpenASP"
func run() {
Response.Write("<h1>Hello from Go on GNDLine.</h1>")
}
%>
GNDLine beta4 日本語 package
この package には、GNDLine web runtime と Go を含む modern language engine 群がまとめて入っています。
はい。この source そのものが page として動きます。
<%@ LANGUAGE="GND.Go.Yaegi" %>
<%
import . "OpenASP"
func run() {
Response.Write("<h1>Hello from Go on GNDLine.</h1>")
}
%>
これも同じです。この source そのものが JSON endpoint として動きます。
<%@ LANGUAGE="GND.Go.Yaegi" %>
<%
import . "OpenASP"
func run() {
Response.WriteJSON(map[string]any{
"ok": true,
"message": "Hello from GND.Go.Yaegi via .api",
})
}
%>
同梱 sample では、host/page の主要 surface をひととおり触れます。
RequestResponseSessionApplicationServerRoutingAuthenticationMultipartJSON同梱 page sample は次の engine で動きます。
Go YaegiOpenVBSこの gallery には Page(.asp) sample と JSON API(.api) sample の両方があります。動き方は同じで、.api は endpoint らしい URL に見せるための suffix です。
この sample では、HTML が func run() の中に inline で現れます。
<%
func run() {
title := "inline sample"
%>
<h1><%= title %></h1>
<%
}
%>
ここでは file upload を試せます。page は file 名、size、可能なら text preview を表示します。
この page は Response.WriteJSON(...) で JSON を返します。POST request は JSON のみです。
<%
func run() {
Response.WriteJSON(map[string]any{
"ok": true,
})
}
%>
この page は GND.Go.Yaegi.vendor から package を import して JSON を返します。
<%
import "example.com/srcsample"
func run() {
Response.WriteJSON(map[string]any{
"message": srcsample.Message("demo"),
})
}
%>
この page は query string から session state を切り替えます。
<%
func run() {
query := Request.QueryString()
Session.Contents().SetItem("LoginState", first(query.Item("mode")))
}
%>
この page は session state を読み、現在の login 値を表示します。
<%
func run() {
loginState := Session.Contents().Item("LoginState")
}
%>
<p>LoginState=<%= loginState %></p>
この page は SQLite を開き、sample row を書いて JSON を返します。
<%
import "example.com/sqliteapp/store"
func run() {
db, _ := store.Open("sample.db")
Response.WriteJSON(map[string]any{
"ok": true,
})
}
%>
この page は BILib.Mail を adapt して JSON preview を返します。
<%
import . "BILib"
func run() {
mail, _ := AsMail(Server.CreateObject("BILib.Mail"))
Response.WriteJSON(map[string]any{
"ok": true,
})
}
%>
この global hook は app 起動時に application 全体の値を保存します。
<script language="GND.Go.Yaegi" runat="server">
func Application_OnStart() {
Application.Lock()
Application.Contents().SetItem("SiteName", "GNDLine Beta Package")
Application.Unlock()
}
</script>
この global hook は新しい session に default login state を与えます。
<script language="GND.Go.Yaegi" runat="server">
func Session_OnStart() {
Session.Contents().SetItem("LoginState", "guest")
}
</script>
global.asa に入れて、app 起動時に route を登録します。
<script language="GND.Go.Yaegi" runat="server">
import . "OpenASP"
func Application_OnStart() {
router, _ := AsRouter(Server.CreateObject(".Router"))
router.GET("/routed.asp", "/target.asp")
router.GET("/api/*", "/api/json.api")
router.GET("/users/{id}.asp", "/auth/home.asp")
Application.Use(router)
}
</script>
global.asa に入れて、page に入る前に route を保護します。
<script language="GND.Go.Yaegi" runat="server">
import . "OpenASP"
func Application_OnStart() {
auth, _ := AsAuthBySession(Server.CreateObject(".AuthBySession"))
auth.Item("/member/*", "MemberID", "/login.asp")
auth.Item("/admin/*", "AdminID", "/login-admin.asp")
Application.Use(auth)
}
</script>
この page は classic VBScript 風の CreateObject(...) を使います。
<%@ LANGUAGE="GND.OpenVBS" %>
<%
set dic = CreateObject("Scripting.Dictionary")
dic("foo") = "bar"
%>
<h1><%= dic("foo") %></h1>
この page は Go から OpenVBS function を呼びます。
<%@ LANGUAGE="GND.Go.Yaegi" %>
<script language="GND.OpenVBS" runat="server">
function GetMessage()
GetMessage = "this is OpenVBS in Go."
end function
</script>
<%
func run() {
message := GetMessage()
Response.Write(message)
}
%>
この page は OpenVBS から Go function を呼びます。
<%@ LANGUAGE="GND.OpenVBS" %>
<script language="GND.Go.Yaegi" runat="server">
func GetMessage() string {
return "this is Go in OpenVBS."
}
</script>
<%
v = GetMessage()
Response.Write v
%>
この page はわざと invalid Go で終わります。
<%
func run() {
pageStart("GNDLine 500 Example")
}
this is not valid go
%>
この package には、OpenVBS page code から使える object runtime 群と optional な BILDB plugin も同梱されています。
BILib.Mailmail field を組み立て、SMTP config bag で送信します。
<%@ LANGUAGE="GND.OpenVBS" %>
<%
mail = CreateObject("BILib.Mail")
mail.Subject = "hello"
%>
BILib.Dictionaryclassic script 風の item access を持つ dictionary object です。
<%@ LANGUAGE="GND.OpenVBS" %>
<%
dict = CreateObject("BILib.Dictionary")
dict("foo") = "bar"
%>
BILib.RegExpOpenVBS page code から regular-expression object を使えます。
<%@ LANGUAGE="GND.OpenVBS" %>
<%
re = CreateObject("BILib.RegExp")
re.Pattern = "foo+"
%>
BILib.HttpRequestOpenVBS page code から HTTP client object を使えます。
<%@ LANGUAGE="GND.OpenVBS" %>
<%
req = CreateObject("BILib.HttpRequest")
req.Open "GET", "https://example.com/", False
%>
BILib.StreamOpenVBS page code から byte/text stream object を使えます。
<%@ LANGUAGE="GND.OpenVBS" %>
<%
stream = CreateObject("BILib.Stream")
stream.Open
stream.WriteText "hello"
%>
BILDB.Connectionoptional な ODBC database plugin です。host 側に外部 ODBC runtime が必要です。
<%@ LANGUAGE="GND.OpenVBS" %>
<%
conn = CreateObject("BILDB.Connection")
conn.Open "Driver=..."
conn("SELECT * FROM TMember WHERE name=?", "jenny").foreach(function(r)
Response.Write r(0).name & ":" & r("id")
Response.Write r(1).name & ":" & r("name")
Response.Write r(2).name & ":" & r("time")
end function)
%>