23
Web Development in D Vladimir Panteleev DConf 2013 1

Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Web Development in DVladimir Panteleev

DConf 2013

1

Page 2: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

What is web development?

•Websites

•Web applications

•Web services (REST)

•Databases

2

Page 3: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Why D?

3

Page 4: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

02000400060008000

1000012000140001600018000

C# +HttpListener

JavaScript +Node.js

Java +restlets/jetty

Erlang +Cowboy

D + Vibe.d

Requests per second

-c 100 -c 1000 -c 5000

Benchmarks by Михаил Страшун (Dicebot) - http://j.mp/14SASPX4

Page 5: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

0

20000

40000

60000

80000

100000

120000

C# +HttpListener

Python +Tornado

JavaScript +Node.js

Java +restlets/jetty

PHP +Apache2

D + Vibe.d

Requests per second

5

Page 6: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

vibe.d•Asynchronous I/O, synchronous API

•Exception-based error handling

•Compile-time templates

•Load-balancing (WIP)

•Zero-downtime changes (WIP)

•REST, SSL, MongoDB, Redis

6

Page 7: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Synchronous

One thread (or process) per connection

Asynchronous

Multiple connections per thread (or process)

7

Page 8: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Synchronous

// ...auto result = fun();// use result

Asynchronous// ...fun(delegate(result) {

// use result});

8

Page 9: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

SynchronouslogInfo("Connecting to DB...");auto db = connectMongoDB("localhost")

.getDatabase("test");auto coll = db["test"];

logInfo("Inserting value...");coll.insert(["a" : 2]);

logInfo("Querying DB...");foreach (doc; coll.find(["name" : "hans"]))

logInfo(doc.toJson().toString());

Asynchronousconsole.log('Connecting to DB...');client.open(function(err, p_client) {

client.collection('test', function (err, coll) {if (err) throw err;

console.log('Inserting value...');coll.insert({a:2}, function(err, docs) {

if (err) throw err;

console.log('Querying DB...');var q = coll.find({"name" : "hans"});q.toArray(function(err, results) {

if (err) throw err;

for (var i=0; i<results.length; i++)console.log(JSON.stringify(results[i]));

});});

});});

9

Page 10: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

vibe.d – Intro

$ dub init appname$ cd appname$ dub

appname/package.jsonsource/

app.dpublic/

images/styles/

style.cssviews/

layout.dtindex.dt

10

Page 11: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

─── package.json ───

{"name": "appname","description": "My fabulous new app","copyright": "Copyright (C) ...","homepage": "http://appname.org","authors": [

"Hans Wurst"],"dependencies": {

"vibe-d": ">=0.7.12"}

}

11

Page 12: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

─── app.d ───import vibe.d;

void index(HTTPServerRequest req,HTTPServerResponse res)

{res.renderCompat!("index.dt",

HTTPServerRequest, "req")(req);}

shared static this(){

auto router = new URLRouter;router.get("/", &index);

auto settings = new HTTPServerSettings;settings.port = 8080;listenHTTP(settings, router);

}

─── index.dt ───

extends layout

block bodyh1 Example page - Homep Hello, World!

─── layout.dt ───!!! 5html

headtitle Example page

bodyblock body

12

Page 13: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Compile-time “Diet” templates!!! 5html

headtitle My page: #{pageTitle}

bodyh1= pageTitlep This is the content of this page. The| title "#{pageTitle}" is inserted dynamically.| We can also use loops and other D statements:

block placeholderp- foreach(i, ch; pageTitle)

| #{i+1}. character: #{ch}p(style='color: red;') This text is red.p.special.small This paragraph has the 'special'| and 'small' CSS classes

p#footer This paragraph has the id 'footer'.#somediv| I'm a text inside the div ##somediv

13

Page 14: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

REST (Representational State Transfer)

•Client-server

• Stateless

•Cacheable

• Layered system

•Uniform interface

HTTP request examples:

GET /items/GET /items/42PUT /items/42DELETE /items/42

14

Page 15: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

REST clientinterface IExampleAPI{

// Matches "GET /"string getIndex();

// Matches "GET /data"@property string data();

// Matches "PUT /data"@property void data(string info);

// Matches "POST /sum"// or "GET /sum?a=...&b=..."int postSum(int a, int b);

// Matches "GET /item/<category>/<item>"@path("item/:category/:item")int getItem(string _category, int _item);

}

auto api = new RestInterfaceClient!IExampleAPI("http://localhost/");

auto index = api.getIndex();api.data = "My data";assert(api.data == "My data");assert(api.postSum(2, 3) == 5);

15

Page 16: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

REST serverclass Example : IExampleAPI{override:

string getIndex() { return "Index!"; }

string _data;@property string data() { return _data; }@property void data(string v) { _data=v; }

int postSum(int a, int b) {return a + b;

}

int getItem(string _category,string _item) {// ...

}}

auto routes = new URLRouter;registerRestInterface!IExampleAPI(

routes, new Example(), "/");

16

Page 17: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

• SSL: via OpenSSL

• Database support: MongoDB, Redis

• Root (setuid)

• dub registry:

• socket.io (JS real-time communication library)

• sass-vibe (CSS extension)

• vibedist (load balancing, virtual hosts, zero-downtime changes)

• mysql-native (native MySQL driver for vibe.d)

• zeal (MVC framework on top of vibe.d)

• and more: http://registry.vibed.org/

http://vibed.org/17

Page 18: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

HibernateD ORMclass Customer {int id;string name;

// @Embedded is inferred -// Address is @EmbeddableAddress address;

// @ManyToOne inferredLazy!AccountType accountType;

// @OneToMany inferredUser[] users;

this() {address = new Address();

}}

class User {long id;string name;Customer customer;

@ManyToManyLazyCollection!Role roles;

}

class Role {int id;string name;

@ManyToManyLazyCollection!User users;

}

@Embeddableclass Address {string zip;string city;string streetAddress;

}

class AccountType {int id;string name;

}

http://sourceforge.net/p/hibernated/18

Page 19: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

HibernateD ORM// create sample data

Role r10 = new Role();r10.name = "role10";Role r11 = new Role();r11.name = "role11";Customer c10 = new Customer();c10.name = "Customer 10";User u10 = new User();u10.name = "Alex";u10.customer = c10;u10.roles = [r10, r11];sess.save(r10);sess.save(r11);sess.save(c10);sess.save(u10);

// load and check data

User u11 = sess.createQuery("FROM User WHERE name=:Name").setParameter("Name", "Alex").uniqueResult!User();

assert(u11.roles.length == 2);assert(u11.roles[0].name.startsWith("role"));assert(u11.roles[1].name.startsWith("role"));assert(u11.customer.name == "Customer 10");assert(u11.customer.users.length == 1);assert(u11.customer.users[0] == u10);assert(u11.roles[0].users.length == 1);assert(u11.roles[0].users[0] == u10);

http://sourceforge.net/p/hibernated/19

Page 20: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Other projects• Web development:

• Adam Ruppe’s arsd – http://bit.ly/12suI2N

• DFeed / ae – https://github.com/CyberShadow/ae

• Package management:

• Orbit – https://github.com/jacob-carlborg/orbit

• Database access:

• ddbc – http://dsource.org/projects/ddbc

• Template engine:

• Mustache for D – http://dsource.org/projects/ddbc

20

Page 21: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

D and JavaScript

• JavaScript in D

• DMDScript - http://www.digitalmars.com/dscript/

• Higgs

•D to JavaScript• Adam Ruppe’s DMD hack: http://bit.ly/zIZOMl

• ABA’s LDC+Emscripten+Asm.js experiments: http://j.mp/YUCtDv

•Haxe - http://www.dsource.org/projects/haxed/

21

Page 22: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Future

More Vibe!https://github.com/rejectedsoftware/vibe.d

http://registry.vibed.org/

core.event / std.event ?http://wiki.dlang.org/Event_system

22

Page 23: Web Development in D · vibe.d –Intro $ dub init appname $ cd appname $ dub appname/ package.json source/ app.d public/ images/ styles/ style.css views/ layout.dt index.dt 10 ───

Thank you

Acknowledgements:

Sönke Ludwig – Vibe.d and feedbackМихаил Страшун (Dicebot) – benchmarks

Questions?23