Giter VIP home page Giter VIP logo

magic-rest's Introduction

Magic-rest

A http server based on netty inspired by java servlet and node express

Features

  1. extremely lightwight out-of-box http server
  2. big file transport support zero-copy
  3. support http keep-alive,cookies,gzip
  4. support static resources and browser cache
  5. support freemarker template to render html
  6. use netty's asynchronous and zero-copy
  7. simple to use and learn

Hello World

  1. 定义Person实体类,用于展示json解析功能
public class Person {
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Person(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	
}
  1. Hello World 示例
import java.util.HashMap;
import java.util.Map;

import com.homura.magic.server.bootstrap.Magic;
import com.homura.magic.server.core.MagicRequest;
import com.homura.magic.server.core.MagicResponse;
import com.homura.magic.server.core.handler.MagicHandler;
import com.homura.magic.server.core.handler.StaticResourceHandler;
import com.homura.magic.server.core.plugin.StaticResourcePlugin;
import com.homura.magic.server.router.MagicRouter;

import io.netty.handler.codec.http.cookie.DefaultCookie;

public class HelloWorld {

	public static void main(String[] args) throws InterruptedException {
		new Magic().
        //创建并添加路由
		router(new MagicRouter()
                //注册处理器handler,必须调用response.flush()输出响应流
				.get("/freemarker/hello", new MagicHandler() {
					@Override
					public void handle(MagicRequest request, MagicResponse response) throws Exception {
						Map<String, Object> attr=new HashMap<>();
						attr.put("name", "hello world");
						response.render("hello.ftl", attr).flush();
					}
				})
                //支持ant风格的url匹配模式,内部借用spring-core中org.springframework.util.AntPathMatcher实现
				.get("/hello*", new MagicHandler() {

			@Override
			public void handle(MagicRequest request, MagicResponse response) throws Exception {
                //内部使用fastjson作为json解析器				
				response.json(new Person("tom", 10)).addCookie(new DefaultCookie("a", "b"))
						.addCookie(new DefaultCookie("c", "d")).flush();
			}
		})
				)
        //注册静态资源处理插件
		.plugins(new StaticResourcePlugin("/statics/**", new StaticResourceHandler("classpath:static/assets"))
				)
        //注册freemarker模板插件,模板以.ftl后缀结尾
		.templateRoot("/template")
        //绑定至8080端口,并启动服务器
		.bind(8080);
	}
}
  1. that's all,just enjoy,it's a pretty gift for you to leran netty and httpcodec.

Architchture

architechture image
use the concept of servlet's request,response and FilterChain

To be done

  1. https support
  2. rest style support
  3. annotation support maybe

magic-rest's People

Contributors

techzealot avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.