ris自称是Go语言中所有Web框架最快的,它的特点如下:

1.聚焦高性能

2.健壮的静态路由支持和通配符子域名支持。

3.视图系统支持超过5以上模板

4.支持定制事件的高可扩展性Websocket API

5.带有GC, 内存 & redis 提供支持的会话

6.方便的中间件和插件

7.完整 REST API

8.能定制 HTTP 错误

9.Typescript编译器 + 基于浏览器的编辑器

10.内容 negotiation & streaming

11.传送层安全性

12.源码改变后自动加载

13.OAuth, OAuth2 支持27+ API providers

14.JSON Web Tokens

kataras/iris简介

github地址

https://github.com/kataras/iris

Star: 7938

文档地址

https://docs.iris-go.com/

描述

关于kataras/iris的描述十分霸气:

The fastest web framework for Go in (THIS) Earth. HTTP/2 Ready to GO. MVC when you need it.

还是那句话,暂时不去计较,只是学习。

获取

go get -u github.com/kataras/iris

快速开始

新建main.go

新建views文件夹,在views中新建hello.html

main.go

package main

import "github.com/kataras/iris"

func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html")) app.Get("/", func(ctx iris.Context) {
ctx.ViewData("message", "Hello world!")
ctx.View("hello.html")
}) app.Run(iris.Addr(":8080"))
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

hello.html

<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>{{.message}}</h1>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行,在浏览器输入http://localhost:8080/,得到运行结果。

basic认证

之前写过关于basic认证的文章:

Go实战–通过basic认证的http(basic authentication)

package main

import (
"time" "github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/middleware/basicauth"
) func newApp() *iris.Application {
app := iris.New() authConfig := basicauth.Config{
Users: map[string]string{"wangshubo": "wangshubo", "superWang": "superWang"},
Realm: "Authorization Required",
Expires: time.Duration(30) * time.Minute,
} authentication := basicauth.New(authConfig) app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") }) needAuth := app.Party("/admin", authentication)
{
//http://localhost:8080/admin
needAuth.Get("/", h)
// http://localhost:8080/admin/profile
needAuth.Get("/profile", h) // http://localhost:8080/admin/settings
needAuth.Get("/settings", h)
} return app
} func main() {
app := newApp()
app.Run(iris.Addr(":8080"))
} func h(ctx context.Context) {
username, password, _ := ctx.Request().BasicAuth()
ctx.Writef("%s %s:%s", ctx.Path(), username, password)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

Markdown

Go实战–golang中使用markdown(russross/blackfriday)

package main

import (
"time"
<span class="hljs-string">"github.com/kataras/iris"</span>
<span class="hljs-string">"github.com/kataras/iris/context"</span> <span class="hljs-string">"github.com/kataras/iris/cache"</span>

)

var markdownContents = []byte(`## Hello Markdown

This is a sample of Markdown contents

Features

All features of Sundown are supported, including:

  • Compatibility. The Markdown v1.0.3 test suite passes with

    the –tidy option. Without –tidy, the differences are

    mostly in whitespace and entity escaping, where blackfriday is

    more consistent and cleaner.

  • Common extensions, including table support, fenced code

    blocks, autolinks, strikethroughs, non-strict emphasis, etc.

  • Safety. Blackfriday is paranoid when parsing, making it safe

    to feed untrusted user input without fear of bad things

    happening. The test suite stress tests this and there are no

    known inputs that make it crash. If you find one, please let me

    know and send me the input that does it.

    NOTE: “safety” in this context means runtime safety only. In order to

    protect yourself against JavaScript injection in untrusted content, see

    this example.

  • Fast processing. It is fast enough to render on-demand in

    most web applications without having to cache the output.

  • Routine safety. You can run multiple parsers in different

    goroutines without ill effect. There is no dependence on global

    shared state.

  • Minimal dependencies. Blackfriday only depends on standard

    library packages in Go. The source code is pretty

    self-contained, so it is easy to add to any project, including

    Google App Engine projects.

  • Standards compliant. Output successfully validates using the

    W3C validation tool for HTML 4.01 and XHTML 1.0 Transitional.

    this is a link `)

func main() {

app := iris.New()

app.Get(<span class="hljs-string">"/"</span>, cache.Handler<span class="hljs-number">(10</span>*time.Second), writeMarkdown)
app.Run(iris.Addr(<span class="hljs-string">":8080"</span>))

}

func writeMarkdown(ctx context.Context) {

println(“Handler executed. Content refreshed.”)

ctx.Markdown(markdownContents)

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

YAML

Go实战–go语言中使用YAML配置文件(与json、xml、ini对比)

iris.yml

DisablePathCorrection: false
EnablePathEscape: false
FireMethodNotAllowed: true
DisableBodyConsumptionOnUnmarshal: true
TimeFormat: Mon, 01 Jan 2006 15:04:05 GMT
Charset: UTF-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

main.go

package main

import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
) func main() {
app := iris.New()
app.Get("/", func(ctx context.Context) {
ctx.HTML("<b>Hello!</b>")
}) app.Run(iris.Addr(":8080"), iris.WithConfiguration(iris.YAML("./iris.yml")))
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Post Json

Go实战–net/http中JSON的使用(The way to go)

package main

import (
"fmt" "github.com/kataras/iris"
"github.com/kataras/iris/context"
) type Company struct {
Name string
City string
Other string
} func MyHandler(ctx context.Context) {
c := &Company{}
if err := ctx.ReadJSON(c); err != nil {
panic(err.Error())
} else {
fmt.Printf("Company: %#v", c)
ctx.Writef("Company: %#v", c)
}
} func main() {
app := iris.New()
app.Post("/bind_json", MyHandler)
app.Run(iris.Addr(":8080"))
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

curl命令行执行:

curl -d '{"Name":"vSuperWang", "City":"beijing", "Other":"shit"}' -H "Content-Type: application/json" -X POST http://localhost:8080/bind_json
  • 1

Go实战--也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)的更多相关文章

  1. Go语言web框架 gin

    Go语言web框架 GIN gin是go语言环境下的一个web框架, 它类似于Martini, 官方声称它比Martini有更好的性能, 比Martini快40倍, Ohhhh….看着不错的样子, 所 ...

  2. 最好的6个Go语言Web框架

    原文:Top 6 web frameworks for Go as of 2017 作者:Edward Marinescu 译者:roy 译者注:本文介绍截至目前(2017年)最好的6个Go语言Web ...

  3. Go语言Web框架gwk介绍4

    Go语言Web框架gwk介绍 (四)   事件 gwk支持事件系统,但并没有硬编码有哪些事件,而是采用了比较松散的定义方式. 订阅事件有两种方式: 调用On函数或者OnFunc函数 func On(m ...

  4. Go语言Web框架gwk介绍 3

    Go语言Web框架gwk介绍 (三)   上一篇忘了ChanResult ChanResult 可以用来模拟BigPipe,定义如下 type ChanResult struct { Wait syn ...

  5. Go语言Web框架gwk介绍2

    Go语言Web框架gwk介绍 (二) HttpResult 凡是实现了HttpResult接口的对象,都可以作为gwk返回Web客户端的内容.HttpResult接口定义非常简单,只有一个方法: ty ...

  6. Go语言Web框架gwk介绍 1

    Go语言Web框架gwk介绍 (一)   今天看到Golang排名到前30名了,看来关注的人越来越多了,接下来几天详细介绍Golang一个web开发框架GWK. 现在博客园支持markdown格式发布 ...

  7. GO语言web框架Gin之完全指南

    GO语言web框架Gin之完全指南 作为一款企业级生产力的web框架,gin的优势是显而易见的,高性能,轻量级,易用的api,以及众多的使用者,都为这个框架注入了可靠的因素.截止目前为止,github ...

  8. 对Golang有兴趣的朋友,推荐一款go语言Web框架-dotweb

    Go语言,2009年推出,对我个人,2015年下半年,才下定决心正式开始引入使用Go,自此,让我获得了一种全新的开发体验. 在不断的项目过程中,一个开发人员总喜欢堆积一些代码段,由于Go的开源特性,逐 ...

  9. Go语言Web框架gwk介绍 (一)

    今天看到Golang排名到前30名了,看来关注的人越来越多了,接下来几天详细介绍Golang一个web开发框架GWK. 现在博客园支持markdown格式发布文章么?后台的编辑器不太好用嘛. GWK ...

随机推荐

  1. 【LeetCode】成对交换节点

    e.g. 给定链表 1->2->3->4,返回 2->1->4->3 的头节点. 我写了个常见的从头节点遍历,少量的奇数个或偶数个数据都能成功重新排列.但链表过长时 ...

  2. Utils--前台调用后台接口工具类

    Utils--前台调用后台接口工具类 package com.taotao.manage.httpclient; import java.io.IOException; import java.net ...

  3. ReactiveCocoa入门教程--第二部分

    翻译自:http://www.raywenderlich.com/62796/reactivecocoa-tutorial-pt2 ReactiveCocoa 是一个框架,它允许你在你的iOS程序中使 ...

  4. 数组Array.sort()排序的方法

    数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...

  5. 【转】[总结]vue开发常见知识点及问题资料整理(持续更新)

    1.(webpack)vue-cli构建的项目如何设置每个页面的title 2.vue项目中使用axios上传图片等文件 3.qs.stringify() 和JSON.stringify()的区别以及 ...

  6. 线程池 execute 和 submit 的区别

    代码示例: public class ThreadPool_Test { public static void main(String[] args) throws InterruptedExcept ...

  7. ActiveMQ queue 分页

    分页:即获取部分数据,queue按页从message cursor读取消息,然后分发给consumer. 页大小: public abstract class BaseDestination impl ...

  8. Tomcat修改用户名密码教程

    Tomcat安装教程见http://www.cnblogs.com/lsdb/p/6497964.html 启动tomcat后访问http://127.0.0.1/:8080,出现界面如下其右上角有三 ...

  9. [转]10+倍性能提升全过程--优酷账号绑定淘宝账号的TPS从500到5400的优化历程

    摘要: # 10+倍性能提升全过程--优酷账号绑定淘宝账号的TPS从500到5400的优化历程 ## 背景说明 > 2016年的双11在淘宝上买买买的时候,天猫和优酷土豆一起做了联合促销,在天猫 ...

  10. localStorage 设置本地缓存

    var timestamp = parseInt(Date.parse(new Date()));var btn = document.getElementById("close" ...