Golang(6)Web Basic
3.1 How Go Work with Web
URL (Uniform Request Locator)
DNS (Domain Name System)
TCP ——> HTTP (80 port TCP)
Http Request
Http Request —> Request Line, Request Header, Request Body
GET /domains/example/ HTTP/1.1 //Request Line: Request Method
Host: www.iana.org //server host name
User-Agent
Accept //Client mine
Accept-Encoding //
Accept-Charset //Client Charset
Body
Http Response
HTTP/1.1 200 OK //response status Line
Server: nginx/1.0.8 //Web Server software name and version
Date: Tue, 30 Oct 2012 04:14:25 GMT //sending time
Content-Type: text/html //Server side data mine
Transfer-Encoding: chunked //
Connection: keep-alive
Content-Length: 90 //length of the body
<!Document html>…. Body
4XX client errors, 5XX server side errors, 2XX success, 3XX redirect, 1XX info
Keep-Alive
After HTTP/1.1, Keep-Alive will default be true. We can reuse the TCP connection. This Keep-Alive time can be set on the server side configuration.
3.2 Construct the Web Server based on Go
The Simplest Server based on http package
package main
import (
"fmt"
"log"
"net/http"
"strings"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析参数,默认是不会解析的
fmt.Println("form: ", r.Form) //这些信息是输出到服务器端的打印信息
fmt.Println("path: ", r.URL.Path)
fmt.Println("scheme: ", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello astaxie!") //这个写入到w的是输出到客户端的
}
func main() {
http.HandleFunc("/", sayhelloName) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Visit the http://localhost:9090 to see the result.
3.3 Further Work to Build Web
Some Concepts from Server Side
Request, Response, Connection, Handler
How http Package Works
for {
rw, e := l.Accept()
if e != nil {
}
c, err := srv.newConn(rw)
if err != nil {
continue
}
go c.serve()
}
handler is the second parameter of ListenAndServe, if it is nil, we will use handler = DefaultServeMux
3.4 Details of http Package
There are 2 key functions in http: Conn, ServeMux
Conn and Concurrence
c, err := srv.newConn(rw)
if err != nil {
continue
}
go c.serve()
Customer ServeMux
How the old codes work
type ServeMux struct {
mu sync.RWMutex //lock
m map[string]muxEntry
hosts bool // if put the host in the mapping rules
}
type muxEntry struct {
explicit bool //match exactly
h Handler
pattern string
}
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
When we call method HandlerFuc, it will convert the function to contains method ServHTTP
type HandlerFunc func(ResponseWriter, *Request)
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w,r)
}
Map the Rules and then Call the Handler
Customer Mapping
package main
import (
"fmt"
"net/http"
)
type MyMux struct {
}
func (p *MyMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
sayhelloName(w, r)
return
}
http.NotFound(w, r)
return
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello myroute!")
}
func main() {
mux := &MyMux{}
http.ListenAndServe(":9090", mux)
}
4. Forms
…snip...
References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/03.0.md
- 浏览: 2539983 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 467NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 464NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 278NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 254Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 564NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 255Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 356Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
它提供了丰富的功能,如中间件支持、路由控制以及强大的错误处理机制,使得在Golang中构建Web服务变得简单而高效。 首先,我们需要安装Echo库。在你的终端中,运行以下命令来获取Echo: ```bash go get -u github....
【标题】"gobasic:golang基础博客源码http" 【描述】"基本的golang基础博客源码,此项目将包含使用Go语言(Golang)编写的基本博客系统的源代码,目的是详细介绍GOlang Basics,帮助开发者理解并掌握Go语言的基础...
About This Book, Effectively deploy and integrate Go web services with applications in the real worldFamiliarize yourself with RESTful practices and apply them in GoA comprehensive tutorial with lots ...
- **BasicAuth**:基本认证中间件,用于简单的身份验证。 - **BodyLimit**:请求体大小限制中间件,防止服务遭受大体积的恶意上传。 - **CORS**:跨域资源共享中间件,用于处理跨域请求问题。 - **CSRF**:跨站请求...
在Golang中,HTTP基本认证是一种简单而广泛使用的身份验证机制,它允许服务器验证客户端(通常是Web浏览器)发送的请求。基本认证的工作原理是,当客户端首次尝试访问受保护的资源时,服务器会返回一个401 ...
无闻老师的Go Web讲义,Golang指导的好资料。Go语言是谷歌推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性。谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发Go,是因为过去10...
在VBA(Visual Basic for Applications)中,我们可以利用`WinHttp.WinHttpRequest.5.1`对象来实现cURL命令的转换。例如,上述cURL命令在VBA中的等效代码为: ```vba Sub curl_to_vba() Dim xhr As Object Set xhr...
Golang Gin 框架英文文档是关于 Golang 语言中使用 Gin 框架进行 Web 开发的详细文档。该文档涵盖了 Gin 框架的安装、使用、配置、路由、模板引擎、数据库交互、认证授权、日志记录、错误处理等多方面的知识点。 一...
Golang Web框架的API处理程序测试。 支持框架 Golang包http提供HTTP客户端和服务器实现。 支持向上 用法 下载此程序包。 $ go get github.com/appleboy/gofight/v2 要导入此程序包,请将以下行添加到您的代码...
文档中提到了传输层安全(Transport Layer Security,TLS)、基本认证(Basic Authentication)、OAuth和OAuth2、JSON Web Tokens(JWT)以及安全会话等安全相关的知识点。 8. 高级特性:手册提到了一些高级特性,...
关注 Nado Coating、Nomad Coder 网页抓取webscrapping_basic:Nado 编码讲座( ) webscrapping_project:我的编码最终项目webscrapping_nomad:Nomad编码器挑战IsitDown.py:检查输入的网址是否正确货币代码:抓取...
包括-数千种代码,OOP,并发性,并行性,Goroutines,互斥体和等待组,Go中测试,Go工具链,后端Web开发,一些项目包括使用bufio.Logner的日志文件解析器,扫描仪,垃圾邮件屏蔽程序,复古LED时钟,控制台动画,...
Golang Perl Visual Basic Object Pascal TIOBE 8月编程语言排行1-20 PHP概念 PHP原始为Personal Home Page的缩写,已经正式更名为 "PHP: Hypertext Preprocessor"。 PHP官方解释 PHP is a popular general-purpose ...
用于 , 网站和其他一些网站的Basic Go网络服务器。 运行示例 第一种方法:生成可执行文件: go build example/main.go && ./main go run example/main.go方法: go run example/main.go 特征 使用regexp进行路由:...
Go, also commonly referred ...The Go web site (https://golang.org/) defines Go this way: “Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Plik是golang中可扩展且友好的临时文件上传系统(wetransfer like)。 主要特点 强大的命令行客户端 易于使用的Web UI 多个数据后端:文件,OpenStack Swift,S3 多个元数据后端:Sqlite3,PostgreSQL OneShot:...
Go语言,也被称为Golang,是由Google开发的一种静态类型的、编译式的、并发型且具有垃圾回收功能的编程语言。它的设计目标是提高编程效率、系统级编程的性能以及网络和多核计算的并发处理能力。`go_basic`这个主题...
例如,使用`BasicAuth()`中间件进行基本身份验证。 6. **日志管理**: Gin提供了自定义日志格式、控制输出颜色和模型绑定的功能。可以自定义日志记录到文件,调整输出格式以满足特定需求。 7. **数据绑定和验证**...