Hunt framework 1.0.0 正式版,三年来最大更新

栏目: 软件资讯 · 发布时间: 8年前

内容简介:Hunt framework 通过三年的不断完善,HuntLabs 所有成员很高兴的对外宣布 hunt 1.0.0 正式版,经过了 0.4.x / 0.5.x / 0.7.x / 0.9.x / 0.10.x 的一些重要分支,我们改进了很多不方便使用的地方,同时带来了更好...

Hunt framework 通过三年的不断完善,HuntLabs 所有成员很高兴的对外宣布 hunt 1.0.0 正式版,经过了 0.4.x / 0.5.x / 0.7.x / 0.9.x / 0.10.x 的一些重要分支,我们改进了很多不方便使用的地方,同时带来了更好的性能。

主要更新特性: 

* 增强 Controller 以及提升 Action 可用性和便捷性 
* 重新梳理 config/application.conf 配置文件 
* 改进对静态文件的访问,实现真正的全栈框架 
* 改进路由正则表达式匹配规则 
* 参照 Java JPA 思想实现了ORM持久化框架 Entity 
* 参照 Spring boot 2.0 对 Entity 和 hunt 的整合增加 repository 模块,进一步简化数据库操作 
* 支持二级缓存的 UCache(后面考虑一下跟 红薯 商量出个D语言版本 j2cache) 
* 进一步加强改进 Collie 的整合 
* 参照了 Django(jinja2) 和 Symfony(twig)实现了 hunt 的模板引擎模块 
* 重构 Session 模块 
* 重构 Cookie 功能 
* 改进 Request 常规 API 
* 改进 Response 使用规则,更新 API 
* 改进 Middleware 规则,参照 Django 
* 使用 kiss.logger 替代标准库的日志模块 
* 核心网络模块使用 kiss.net.TcpStream 完成,有很大的性能改进,具体可以参看 kiss

总而言之我们把此次版本定义为 1.0 是一个重要的里程碑版本,相信 Hunt framework 的易用性和可靠性加上 Dlang 以身俱来的性能可以为更多开发者提供便利,我们觉得 hunt 更适合 C++ 和 php 开发者尝试使用,后面我们也会推出各种示例代码给大家,甚至开源一些常用的系统。

基础功能改进

配置文件优化

以前的配置项比较简洁,但是很多人说不够清晰,这次也重新整理了一次,改进的比较多举一个数据库配置的例子给大家:

老配置文件:

database.url=mysql://root:123456@localhost:3306/test?charset=utf8

新配置文件:

hunt.database.default.driver=mysqk
hunt.database.default.host=localhost
hunt.database.default.port=3306
hunt.database.default.database=test
hunt.database.default.username=root
hunt.database.default.password=123456
hunt.database.default.charset=utf8
hunt.database.default.prefix=

Controller

API改进:

控制器的Action支持多种类型返回,其中包括:string、数值类型、void、bool、Response、JsonResponse、RedirectResponse等。

应用示例:

// 模块定义
module app.controller.IndexController;

// 中间件定义
class IpFilterMiddleware : MiddlewareInterface
{
    override string name()
    {
        return IpFilterMiddleware.stringof;
    }

    override Response onProcess(Request req, Response res)
    {
        writeln(req.session());
        return null;
    }
}

// Controller定义
class IndexController : Controller
{
    mixin MakeController;
    this()
    {
        this.addMiddleware(new IpFilterMiddleware());     // 中间件注入
    }
    // 通过@Action标识动作
    @Action string index()
    {
        JSONValue model;
        model["title"] = "Hunt demo";
        model["now"] = Clock.currTime.toString();
        view.setTemplateExt(".dhtml");
        return view.render("home", model);                // 返回模板视图定义的内容
    }

    // 方法名以Action结尾时,可自动识别为动作
    Response showAction()
    {
        Response response = new Response("Hello world<br/>");
        response.setHeader(HttpHeaderCode.CONTENT_TYPE, "text/html;charset=utf-8")
        .cookie("name2", "value", 10000)                // Cookie设置
        .header("X-Header-One", "Header Value")            // 自定义头标识定义
        .withHeaders(["X-Header-Two":"Header Value", "X-Header-Tree": "Header Value"]);  // 批量添加头标识
        return response;
    }
}

更多信息请参考:https://github.com/huntlabs/hunt/wiki/Configuration

Request

API改进:

request.method;             // return GET or POST
request.path;                 // return /path/to/request
request.GET;                // return all get params
request.POST;               // return all post params
request.allFiles;            // return all files params
request.get!int("id");     // fetch uid as int from get params
request.post!int("id");    // fetch uid as int from post params
request.session();          // return session object
request.ip();               // client ip address

主要参考项目:LaravelSymfony 等 更多信息请参考:https://github.com/huntlabs/hunt/wiki/Request

Response

API改进:

Response showAction()
{
    Response response = new Response("Hello world<br/>");
    response.setHeader(HttpHeaderCode.CONTENT_TYPE, "text/html;charset=utf-8")
    .cookie("name2", "value", 10000)                // Cookie设置
    .header("X-Header-One", "Header Value")            // 自定义头标识定义
    .withHeaders(["X-Header-Two":"Header Value", "X-Header-Tree": "Header Value"]);  // 批量添加头标识

    return response;
}

新增Response类型

JsonResponse
RedirectResponse

主要参考项目:LaravelSymfony 等 更多信息请参考:https://github.com/huntlabs/hunt/wiki/Response

Routing

路由表规则改进

1)url路径支持正则匹配

GET    /user/{id<\d+>}   user.detail

2)不再支持相同路径定义

# 以下条目存在冲突
*        /home                        index.show
GET        /home                        index.index

3) 不需要再手动添加/静态路径 Router会根据情况自动添加以下路由,默认将wwwroot目录做为静态文件路径,配置项可修改。

hunt.http.path=wwwroot/

4)静态Index文件搜索 如果路由路径未完全匹配上Controller上的Action方法,则转去搜索静态文件,文件格式为路径+默认静态文件。默认静态文件如下:

index.html, index.htm, default.html, default.htm, home.html

应用举例

GET    /                 index.index
GET    /users            user.list
GET    /user/{id}        user.detail
GET    /user/{id<\d+>}   user.detail

主要参考项目:Play FrameworkDjango等 更多信息请参考:https://github.com/huntlabs/hunt/wiki/Routing

Session

API改进: 主要参考项目:Laravel

应用举例:

struct SessionSettings
{
    string storage = "memory";
    string prefix = "huntsession_";
    string args = "/tmp";
    uint expire = 3600;
}

class SessionTest
{
    private SessionStorage storage;
    private Session session;
    private    SessionSettings sessionSettings;

    this()
    {
        trace("testing Session");
        sessionSettings.expire = 60;
        storage = createSessionStorage(sessionSettings);
    }

    void start()
    {
        testSetName();
        testReplace();
    }

    void testSetName()
    {
        session = new Session(storage, false);
        this.assertEquals("hunt_session", this.session.getName());
        this.session.setName("session.test.com");
        this.session.start();
        this.assertEquals("session.test.com", this.session.getName());
    }

    void testReplace()
    {
        this.session.replace(["happiness": "be good", "symfony" : "awesome"]);
        this.assertEquals(["happiness" : "be good", "symfony" : "awesome"], this.session.all());
        this.session.replace(null);
        this.assertEquals(null, this.session.all());
    }
}

更多信息请参考:https://github.com/huntlabs/hunt/wiki/Session

View

模板引擎重构

View模板

<title>{{ user.name }}</title>
<title>{{ user.age }}</title>

Controller定义

@Action string index()
{
    struct User
    {
        string name;
        int age;
    }

    User user;
    info.name = "Hunt demo";
    user.age = 18;

    // 给模板变量赋值
    view.assign("user", user);

    return view.render("home");                // 返回模板视图定义的内容
}

更多信息请参考:https://github.com/huntlabs/hunt/wiki/View

Database

基于 Entity 整合了 repository 概念,简化ORM开发。

应用举例:

数据实体模型定义

module app.model.User;

import entity;

@Table("users")
class User : Entity
{
    @AutoIncrement
    @PrimaryKey
    int id;
    string name;
    string email;
}

数据集定义

module app.repository.UserRepository;

import app.model.User;
import entity;

class UsersRepository : EntityRepository!(User, int)
{
}

CRUD操作

auto repository = new UserRepository;

// 插入
User user = new User;
user.name = "Brian";
user.email = "zoujiaqing@gmail.com";

auto user = repository.save( user );
// 查找
auto user = repository.findById( id );
// 删除
repository.remove( id );

Cache

应用举例:

@Action string testcache()
{
    string key = request.get("key");
    string value = request.get("value");

    cache.put(key, value);

    return cache.get(key);
}

相关地址


【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Numerical Methods and Methods of Approximation in Science and En

Numerical Methods and Methods of Approximation in Science and En

Karan Surana / CRC Press / 2018-10-31

ABOUT THIS BOOK Numerical Methods and Methods of Approximation in Science and Engineering prepares students and other readers for advanced studies involving applied numerical and computational anal......一起来看看 《Numerical Methods and Methods of Approximation in Science and En》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具