HTTP Server Basics

Use net/http package and useful third-party packages by building simple servers.

Building a Simple Server

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net/http"
  6. )
  7.  
  8. func hello(w http.ResponseWriter, r *http.Request) {
  9. fmt.Fprintf(w, "Hello %s\n", r.URL.Query().Get("name"))
  10. }
  11.  
  12. func main() {
  13. http.HandleFunc("/hello", hello)
  14. http.ListenAndServe(":8000",nil)
  15. }

Run the above program and test it.

  1. curl -i http://localhost:8000/hello?name=eric

You can also use http.ListenAndServeTLS(), which will start a server using HTTPS and TLS.

Build a Simple Router

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net/http"
  6. )
  7.  
  8. type router struct {
  9. }
  10.  
  11. func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  12. switch req.URL.Path {
  13. case "/a":
  14. fmt.Fprintf(w, "Executing /a\n")
  15. case "/b":
  16. fmt.Fprintf(w, "Executing /b\n")
  17. case "/c":
  18. fmt.Fprintf(w, "Executing /c\n")
  19. default:
  20. http.Error(w, "404 Not Found", 404)
  21. }
  22. }
  23.  
  24. func main() {
  25. var r router
  26. http.ListenAndServe(":8000", &r)
  27. }

Test the above program by the following commands.

  1. curl http://localhost:8000/a
  2. curl http://localhost:8000/d

Building simple Middleware

A simple middleware, which is a sort of wrapper that will execute on all incoming requests regardless of the destination function.

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "time"
  8. )
  9.  
  10. type logger struct {
  11. Inner http.Handler
  12. }
  13.  
  14. func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  15. log.Printf("start %s\n", time.Now().String())
  16. l.Inner.ServeHTTP(w,r)
  17. log.Printf("finish %s",time.Now().String())
  18. }
  19.  
  20. func hello(w http.ResponseWriter, r *http.Request) {
  21. fmt.Fprint(w, "Hello\n")
  22. }
  23.  
  24. func main() {
  25. f := http.HandlerFunc(hello)
  26. l := logger{Inner: f}
  27. http.ListenAndServe(":8000", &l)
  28. }

Run the program and issue a request.

  1. curl http://localhost:8000

Go Pentester - HTTP Servers(1)的更多相关文章

  1. Go Pentester - HTTP Servers(2)

    Routing with the gorilla/mux Package A powerful HTTP router and URL matcher for building Go web serv ...

  2. Go Pentester - HTTP Servers(3)

    Building Middleware with Negroni Reasons use middleware, including logging requests, authenticating ...

  3. Coping with the TCP TIME-WAIT state on busy Linux servers

    Coping with the TCP TIME-WAIT state on busy Linux servers 文章源自于:https://vincent.bernat.im/en/blog/20 ...

  4. How To Restart timer service on all servers in farm

    [array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...

  5. eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat

    eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...

  6. coderforces #387 Servers(模拟)

    Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  7. Servers

    Servers¶ Server interface. class novaclient.v1_1.servers.Server(manager, info, loaded=False) Bases: ...

  8. 使用servers 启动项目时 ,一直处于启动中, 最后出现无法的问题。

    使用eclipse 中的servers 配置了一个server 来启动项目, 发现无法启动 排除法: 去掉项目配置,单独启动该server ,发现可以启动, 说明是项目出现问题 但是项目并没有报错, ...

  9. servers中添加server时,看不到运行环境的选择。

    servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.

随机推荐

  1. 简单梳理JavaScript垃圾回收机制

    JavaScript具有自动垃圾回收机制,即执行环境会负责管理代码执行过程中使用地内存. 这种垃圾回收机制的原理很简单:找出那些不再继续使用的变量,然后释放其占用的内存.为此,垃圾收集器会按照固定的时 ...

  2. MFC的Spin Control基础控件的使用

    1.向GUI界面添加一个MFC 提供的Spin数值调节控件 2.设置其"伙伴“,并设置数值调节的范围 3.如何让数值显示在文本框中?你可以有多种选择:可以让文本框控件绑定一个数值类型的变量: ...

  3. 08.DRF-反序列化

    三.反序列化使用 3.1 验证 使用序列化器进行反序列化时,需要对数据进行验证后,才能获取验证成功的数据或保存成模型类对象. 在获取反序列化的数据前,必须调用is_valid()方法进行验证,验证成功 ...

  4. java中的excel操作

    导入jxl.jar包: 下载个jxl.jar包,然后这个包放在什么位置都行,在你的项目中导入这个包就可以.   具体做法: 项目上右键,点击“属性”, 类别那里选择”库“,点击"添加jar文 ...

  5. 深入理解JVM(③)ZGC收集器

    前言 ZGC是一款在JDK11中新加入的具有实验性质的低延迟垃圾收集器,目前仅支持Linux/x86-64.ZGC收集器是一款基于Region内存布局的,(暂时)不设分代的,使用了读屏障.染色指针和内 ...

  6. Redis->主从复制->哨兵模式(高可用)

    一:安装redis $ yum -y install gcc $ yum -y install gcc-c++ $ wget http://download.redis.io/releases/red ...

  7. 主机Redis服务迁移到现有Docker Overlay环境

    记录最后一次对中型2C企业级项目的容器化改造 hello, 好久不见,之前文章记录了一个实战的2C分布式项目的改造过程,结果如下: 其中Redis并未完成容器化改造(目前是主机单点),本文记录将Red ...

  8. 入门大数据---Hbase容灾与备份

    一.前言 本文主要介绍 Hbase 常用的三种简单的容灾备份方案,即CopyTable.Export/Import.Snapshot.分别介绍如下: 二.CopyTable 2.1 简介 CopyTa ...

  9. Dll的多字节和Unicode

    Dll的多字节和Unicode 分类: MFC2013-10-17 13:00 28人阅读 评论(0) 收藏 举报 dll字符集字符集多字节Unicode 我们定义dll的时候会区分: 字符集:使用多 ...

  10. Oracle数据库中,误删除或者修改数据恢复方法

    在我们实际工作中,误删除或者修改Oracle数据库中的数据,怎么办呢?这里给大家分享一种解决办法.假如你误操作的时间不超过30分钟(数据库默认的回滚保持段里的数据时间,可以在pl/sql执行窗口按ct ...