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. 【Flutter实战】图片组件及四大案例

    老孟导读:大家好,这是[Flutter实战]系列文章的第三篇,这一篇讲解图片组件,Image有很多高级用法,希望对您有所帮助. 图片组件是Flutter基础组件之一,和文本组件一样必不可少.图片组件包 ...

  2. EOS基础全家桶(十三)智能合约基础

    简介 智能合约是现在区块链的一大特色,而不同的链使用的智能合约的虚拟机各不相同,编码语言也有很大差异.而今天我们开始学习EOS的智能合约,我也是从EOS初期一直开发合约至今,期间踩过无数坑,也在Sta ...

  3. 使用Kubeflow构建机器学习流水线

    在此前的文章中,我已经向你介绍了Kubeflow,这是一个为团队设置的机器学习平台,需要构建机器学习流水线. 在本文中,我们将了解如何采用现有的机器学习详细并将其变成Kubeflow的机器学习流水线, ...

  4. 数据库事务(1)----- JDBC事务与JTA事务

    数据库事务(一)- JDBC事务与JTA事务 本文主要对JDBC事务与JTA事务做一个简单介绍. 1. 数据库事务概念 一个数据库事务通常包含对数据库进行读或写的一个操作序列.它的存在包含有以下两个目 ...

  5. 【DMCP】2020-CVPR-DMCP Differentiable Markov Channel Pruning for Neural Networks-论文阅读

    DMCP 2020-CVPR-DMCP Differentiable Markov Channel Pruning for Neural Networks Shaopeng Guo(sensetime ...

  6. JavaWeb网上图书商城完整项目--day02-16.登录功能各层实现

    /*1.第一步将用户提交的参数封装成javabean对象 * *2.对提交的参数的进行合法性的校验 * *3.通过用户名和密码去查找得到user对象 *如果user对象为null,说明用户名和密码不正 ...

  7. django 分页器,序列化 ,MTV MVC

    序列化组件## from django.core import serializers # django自带的一个小型的序列化工具# def reg(request):# user_list = mo ...

  8. Codeforces Round #652 (Div. 2) 总结

    A:问正n边形的一条边和x轴平行的时候有没有一条边和y轴重合,直接判断n是否是4的倍数 #include <iostream> #include <cstdio> #inclu ...

  9. METS介绍

    METS介绍 首页  >  关于METS > METS介绍   医护英语水平考试(Medical English Test System, 以下简称:METS)是由教育部考试中心与中国国际 ...

  10. vscode 配置 c++ 环境

    vscode 配置 c++ 环境 参考的这篇bloghttps://blog.csdn.net/bat67/article/details/81268581 1.安装编译器.这里安装 codebloc ...