db2

command
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

README

0.png

You can execute the example by running $ dyd -serve in this directory and pointing your browser to http://localhost:6226.

To only convert the example into a go install-able repository issue $ dyd.

In either case here are the files after running dyd:

  • examples/db2/index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>db2 example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style><? write("%s", ctx.W3CSS()) ?></style>
  </head>
  <body style="background-color: #5864ff;">
    <div class="w3-container w3-text-white">
      <p><?
	// A web server logging visits in a persistent database and showing the
	// last visited time, if any.

	// This is the same as the 'db' example, but demonstrates mixing HTML
	// and Go files.

	write("%s", message(ctx))
    ?>
      </p>
    </div>
  </body>
</html>

  • examples/db2/dyd.go
// Code generated by dyd, DO NOT EDIT.

package main

import (
	"golang.org/x/net/html"
	"modernc.org/dyd/dyd"
)

// dydStart starts the webapp. dydStart always returns a non-nil error.
func dydStart(beforeListen ...func(a dyd.App) error) error {
	app, err := dyd.NewApp()
	if err != nil {
		return err
	}

	app.Bind("/index.html", Serve_index_1html)
	app.AddMeta("/index.html", []html.Attribute{
		html.Attribute{Namespace: "", Key: "charset", Val: "utf-8"},
	})
	app.AddMeta("/index.html", []html.Attribute{
		html.Attribute{Namespace: "", Key: "name", Val: "viewport"},
		html.Attribute{Namespace: "", Key: "content", Val: "width=device-width, initial-scale=1"},
	})
	app.AddTitle("/index.html", "db2 example")
	for _, f := range beforeListen {
		if err = f(app); err != nil {
			return err
		}
	}
	return app.ListenAndServe(":6226")
}

  • examples/db2/index.html.go
// Code generated by dyd, DO NOT EDIT.

package main

import (
	"modernc.org/dyd/dyd"
)

// Serve_index_1html produces /index.html.
func Serve_index_1html(ctx *dyd.Context, write func(s string, args ...any) error) {
	write(`<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>db2 example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>`)
	write("%s", ctx.W3CSS())
	write(`</style>
  </head>
  <body style="background-color: #5864ff;">
    <div class="w3-container w3-text-white">
      <p>`)

	// A web server logging visits in a persistent database and showing the
	// last visited time, if any.

	// This is the same as the 'db' example, but demonstrates mixing HTML
	// and Go files.

	write("%s", message(ctx))

	write(`
      </p>
    </div>
  </body>
</html>
`)
}

  • examples/db2/main.go
// Code generated by dyd, DO NOT EDIT.

package main

import "log"

func main() {
	log.Fatal(dydStart())
}

  • examples/db2/message.go
package main

import (
	"bytes"
	"context"
	"fmt"
	"time"

	"modernc.org/dyd/dyd"
)

func message(ctx *dyd.Context) (r []byte) {
	b := bytes.NewBuffer(nil)

	conn, err := ctx.DBConn(context.Background())
	if err != nil {
		fmt.Fprintf(b, "cannot connect to database: %v", err)
		return
	}

	if _, err := conn.ExecContext(context.Background(), "create table if not exists log(visited timestamp);"); err != nil {
		fmt.Fprintf(b, "cannot create table 'log': %v", err)
		return
	}

	row := conn.QueryRowContext(context.Background(), "select count(*) from log;")
	var cnt int64
	if err := row.Scan(&cnt); err != nil {
		fmt.Fprintf(b, "scanning row: %v", err)
		return
	}

	fmt.Fprintf(b, "visit count: %d", cnt+1)

	if cnt > 0 {
		row := conn.QueryRowContext(context.Background(), "select visited from log order by visited desc limit 1;")
		var last time.Time
		if err := row.Scan(&last); err != nil {
			fmt.Fprintf(b, "scanning row: %v", err)
			return
		}

		fmt.Fprintf(b, ", last visit at %s", last.Format(time.RFC1123Z))
	}

	conn.ExecContext(context.Background(), "insert into log(visited) values(?)", time.Now())
	return b.Bytes()
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL