package main

import (
"net/http" "com.jtthink.net/myhttpserver/core"
) type MyHandler struct { }
func(*MyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request){ writer.Write([]byte("hello,myhandler"))
} func main() { router:=core.DefaultRouter() router.Get("/", func(ctx *core.MyContext) {
ctx.WriteString("my string GET")
})
router.Post("/", func(ctx *core.MyContext) {
ctx.WriteString("my string POST")
}) http.ListenAndServe(":8099",router) }

main.go

package core

import (
"net/http"
)
type MyHandlerFunc func(ctx *MyContext)
type MyRouter struct {
Mapping map[string]map[string]MyHandlerFunc
Ctx *MyContext
} func DefaultRouter() *MyRouter {
return &MyRouter{make(map[string]map[string]MyHandlerFunc),&MyContext{}}
}
func(this *MyRouter) Get(path string,f MyHandlerFunc) {
if this.Mapping["GET"]==nil{
this.Mapping["GET"]=make(map[string]MyHandlerFunc)
}
this.Mapping["GET"][path]=f
}
func(this *MyRouter) Post(path string,f MyHandlerFunc) {
if this.Mapping["POST"]==nil{
this.Mapping["POST"]=make(map[string]MyHandlerFunc)
}
this.Mapping["POST"][path]=f
} func(this *MyRouter) ServeHTTP(writer http.ResponseWriter, request *http.Request){ this.Ctx.request=request
this.Ctx.ResponseWriter=writer //chrome 会默认请求图标地址
if f,OK:=this.Mapping[request.Method][request.URL.Path];OK{
f(this.Ctx)
} }

MyRouter.go

package core

import "net/http"

type MyContext struct { //上下文对象
request *http.Request
http.ResponseWriter
}
func(this *MyContext) WriteString(str string){
this.Write([]byte(str))
}

MyContext.go

httpserver实现简单的上下文的更多相关文章

  1. http-server:一个简单的零配置命令行的http服务器

    首先简介一下http-server: http-server是一个简单的零配置命令行http服务器,他对于生产使用来说足够强大,他是简单和可删节足以用于测试,足够简单易用,而且可用于本地开发 1.首先 ...

  2. JavaScript框架设计(三) push兼容性和选择器上下文

    JavaScript框架设计(三) push兼容性和选择器上下文 博主很久没有更博了. 在上一篇 JavaScript框架设计(二) 中实现了最基本的选择器,getId,getTag和getClass ...

  3. python上下文管理器ContextLib及with语句

    http://blog.csdn.net/pipisorry/article/details/50444736 with语句 with语句是从 Python 2.5 开始引入的一种与异常处理相关的功能 ...

  4. python中实现上下文管理器的两种方法

    上下文管理器: python中实现了__enter__和__exit__方法的对象就可以称之为上下文管理器 实现方法一举例: def File(object): def __init__(self, ...

  5. with上下文管理 python魔法方法

    with语法在Python里很常见, 主要的利好是使用代码更简洁. 常见的使用场景有: 1. 资源对象的获取与释放. 使用with可以简化try...finally ... 2. 在不修改函数代码的前 ...

  6. 造轮子系列(三): 一个简单快速的html虚拟语法树(AST)解析器

    前言 虚拟语法树(Abstract Syntax Tree, AST)是解释器/编译器进行语法分析的基础, 也是众多前端编译工具的基础工具, 比如webpack, postcss, less等. 对于 ...

  7. jdk下httpserver源码解析

    在写这篇博客之前我查了很久发现全网都没有一篇写httpserver源码解析的 所以今天就由我来为大家解析一下httpserver的源码.(这里我会去掉其中的https部分的源码,只讲http部分,对h ...

  8. Python 的上下文管理器是怎么设计的?

    花下猫语:最近,我在看 Python 3.10 版本的更新内容时,发现有一个关于上下文管理器的小更新,然后,突然发现上下文管理器的设计 PEP 竟然还没人翻译过!于是,我断断续续花了两周时间,终于把这 ...

  9. Django admin 权威指南(一)

    版本: Django 1.10 此部分由官方文档<6.5.1 The Django admin site>翻译而来. 6.5.1.1 概览 默认情况下,使用startproject的时候, ...

随机推荐

  1. 上传本地项目到码云(gitee)

    1.码云上创建一个项目比如zhirong 2.本地创建一个文件夹F:\workspace\zhirong-items,进入zhirong-items打开git bash 3.执行git init ,这 ...

  2. JSON高亮格式化页面显示

    高亮CSS定义: <style type="text/css"> pre {outline: 1px solid #ccc; padding: 5px; margin: ...

  3. mac版pycharm的字体和行间距设置

  4. Life of Pi

    ·when you look into his eyes,you are seeing your own emotionsreflected back at you,nothing else. ·Go ...

  5. Scrapy下载图片及自定义分类下载路径

    配置下载图片的流程如下 在items中定义两个属性,image_urls 和images .image_urls是用来存储需要下载的图片url链接,列表类型: 当文件下载完成后会把相关下载信息存入im ...

  6. zepto.js按需载入模板对象

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/Joyhen/article/details/34412103 Zepto.js 是支持移动WebKi ...

  7. i\'ll make a man out of you

    Let's get down to business To defeat the Huns Did they send me daughters When I asked for sons? You' ...

  8. H5C3--transform实现任何元素居中对齐

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. float示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Linux 下用Win共享

    让win能访问到 linux 的smb 共享: linux 安装 samba: apt-get install samba #然后 vim /etc/samba/smb.conf #找到securit ...