HTTP Server Basics

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

Building a Simple Server

package main

import (
"fmt"
"net/http"
) func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s\n", r.URL.Query().Get("name"))
} func main() {
http.HandleFunc("/hello", hello)
http.ListenAndServe(":8000",nil)
}

Run the above program and test it.

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

package main

import (
"fmt"
"net/http"
) type router struct {
} func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/a":
fmt.Fprintf(w, "Executing /a\n")
case "/b":
fmt.Fprintf(w, "Executing /b\n")
case "/c":
fmt.Fprintf(w, "Executing /c\n")
default:
http.Error(w, "404 Not Found", 404)
}
} func main() {
var r router
http.ListenAndServe(":8000", &r)
}

Test the above program by the following commands.

curl http://localhost:8000/a
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.

package main

import (
"fmt"
"log"
"net/http"
"time"
) type logger struct {
Inner http.Handler
} func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("start %s\n", time.Now().String())
l.Inner.ServeHTTP(w,r)
log.Printf("finish %s",time.Now().String())
} func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello\n")
} func main() {
f := http.HandlerFunc(hello)
l := logger{Inner: f}
http.ListenAndServe(":8000", &l)
}

Run the program and issue a request.

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. 全链路监控系统开源Pinpoint入门视频教程(最新版本1.8)

    pinpoint支持的模块 源码:https://github.com/naver/pinpoint技术概述:https://skyao.gitbooks.io/learning-pinpoint/c ...

  2. Spring事务深入剖析--spring事务失效的原因

    之前我们讲的分布式事务的调用都是在一个service中的事务方法,去调用另外一个service中的业务方法, 如果在一个sevice中存在两个分布式事务方法,在一个seivice中两个事务方法相互嵌套 ...

  3. shell编程之系统环境变量2

    本课程是<Tony老师聊shell——变量>课程的延续,主要介绍Linux shell编程基础中的运算符.包括declare命令.数值运算方法和变量测试.首先在declare命令中介绍了数 ...

  4. 4. union-find算法

    算法的主题思想: 1.优秀的算法因为能够解决实际问题而变得更为重要: 2.高效算法的代码也可以很简单: 3.理解某个实现的性能特点是一个挑战: 4.在解决同一个问题的多种算法之间进行选择时,科学方法是 ...

  5. shell基本正则表达式

    基本正则表达式 星号* 匹配它前面的字符串或正则表达式任意次(包括0次).比如,“1122*” 将匹配11+1个或多个2,其可能匹配的字符串将是112.1122.112222.11223343等 句点 ...

  6. Dubbo想要个网关怎么办?试试整合Spring Cloud Gateway

    一.背景 在微服务架构中 API网关 非常重要,网关作为全局流量入口并不单单是一个反向路由,更多的是把各个边缘服务(Web层)的各种共性需求抽取出来放在一个公共的"服务"(网关)中 ...

  7. HTML&CSS面试高频考点(三)

    11. CSS隐藏元素的方式 /*占据空间,无法点击*/ visibility: hidden; position: relative; top: -999em; /* 不占据空间,无法点击 */ p ...

  8. 几个常见CSS错误和解决办法

    1.IE6下,当float存在时,margin双倍的问题 解决方法:加display:inline; 例: #content {    float: left;     width: 500px;   ...

  9. CentOS下安装python3环境及pypy环境

    安装前基础环境 1. win7虚拟机CentOS7.6系统 2. 网络环境通过NAT方式 3. 已经配置到yum仓库并系统自带有python2.7 安装前准备 1. python3.6.5源码包:ht ...

  10. 运行python出现 SyntaxError: Non-ASCII character '\xe6' in file /Users/finup/Documents/python_project/test.py 解决办法

    使用pycharm运行程序时出现以下错误 这个错误主要是由于python2的编码默认是ASCII,你的文件里有中文就必须要用utf-8编码,只要在文件需要在文件开头标注 #coding=utf-8如下 ...