GNDLine beta4 日本語 package

GNDLine - Go the GroundLine

この package には、GNDLine web runtime と Go を含む modern language engine 群がまとめて入っています。

この小さな file が本当に動く?

はい。この source そのものが page として動きます。

<%@ LANGUAGE="GND.Go.Yaegi" %>
<%
import . "OpenASP"

func run() {
    Response.Write("<h1>Hello from Go on GNDLine.</h1>")
}
%>

JSON API も同じくらい簡単

これも同じです。この 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 をひととおり触れます。

  • Request
  • Response
  • Session
  • Application
  • Server
  • Routing
  • Authentication
  • Multipart
  • JSON

この package に入っている language engine

同梱 page sample は次の engine で動きます。

  • Go Yaegi
  • OpenVBS

Page と API を選んで試す

この gallery には Page(.asp) sample と JSON API(.api) sample の両方があります。動き方は同じで、.api は endpoint らしい URL に見せるための suffix です。

Inline ASP page

この sample では、HTML が func run() の中に inline で現れます。

<%
func run() {
    title := "inline sample"
%>
<h1><%= title %></h1>
<%
}
%>

Multipart upload page

ここでは file upload を試せます。page は file 名、size、可能なら text preview を表示します。

JSON API

この page は Response.WriteJSON(...) で JSON を返します。POST request は JSON のみです。

<%
func run() {
    Response.WriteJSON(map[string]any{
        "ok": true,
    })
}
%>

Minimal vendor package API

この page は GND.Go.Yaegi.vendor から package を import して JSON を返します。

<%
import "example.com/srcsample"

func run() {
    Response.WriteJSON(map[string]any{
        "message": srcsample.Message("demo"),
    })
}
            %>

Session toggle

この page は query string から session state を切り替えます。

<%
func run() {
    query := Request.QueryString()
    Session.Contents().SetItem("LoginState", first(query.Item("mode")))
}
%>

Auth home

この page は session state を読み、現在の login 値を表示します。

<%
func run() {
    loginState := Session.Contents().Item("LoginState")
}
%>
<p>LoginState=<%= loginState %></p>

SQLite source package API

この page は SQLite を開き、sample row を書いて JSON を返します。

<%
import "example.com/sqliteapp/store"

func run() {
    db, _ := store.Open("sample.db")
    Response.WriteJSON(map[string]any{
        "ok": true,
    })
}
%>

Mail API

この page は BILib.Mail を adapt して JSON preview を返します。

<%
import . "BILib"

func run() {
    mail, _ := AsMail(Server.CreateObject("BILib.Mail"))
    Response.WriteJSON(map[string]any{
        "ok": true,
    })
}
%>

Application_OnStart

この 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>

Session_OnStart

この global hook は新しい session に default login state を与えます。

<script language="GND.Go.Yaegi" runat="server">
func Session_OnStart() {
    Session.Contents().SetItem("LoginState", "guest")
}
</script>

.Router

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>

.AuthBySession

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>

OpenVBS page

この page は classic VBScript 風の CreateObject(...) を使います。

<%@ LANGUAGE="GND.OpenVBS" %>
<%
set dic = CreateObject("Scripting.Dictionary")
dic("foo") = "bar"
%>
<h1><%= dic("foo") %></h1>

Go hosts OpenVBS

この 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)
}
%>

OpenVBS hosts Go

この 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
%>

Intentional 500 page

この page はわざと invalid Go で終わります。

<%
func run() {
    pageStart("GNDLine 500 Example")
}

this is not valid go
%>

Object gallery

この package には、OpenVBS page code から使える object runtime 群と optional な BILDB plugin も同梱されています。

BILib.Mail

mail field を組み立て、SMTP config bag で送信します。

<%@ LANGUAGE="GND.OpenVBS" %>
<%
mail = CreateObject("BILib.Mail")
mail.Subject = "hello"
%>

BILib.Dictionary

classic script 風の item access を持つ dictionary object です。

<%@ LANGUAGE="GND.OpenVBS" %>
<%
dict = CreateObject("BILib.Dictionary")
dict("foo") = "bar"
%>

BILib.RegExp

OpenVBS page code から regular-expression object を使えます。

<%@ LANGUAGE="GND.OpenVBS" %>
<%
re = CreateObject("BILib.RegExp")
re.Pattern = "foo+"
%>

BILib.HttpRequest

OpenVBS page code から HTTP client object を使えます。

<%@ LANGUAGE="GND.OpenVBS" %>
<%
req = CreateObject("BILib.HttpRequest")
req.Open "GET", "https://example.com/", False
%>

BILib.Stream

OpenVBS page code から byte/text stream object を使えます。

<%@ LANGUAGE="GND.OpenVBS" %>
<%
stream = CreateObject("BILib.Stream")
stream.Open
stream.WriteText "hello"
%>

BILDB.Connection

optional な 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)
%>