当前位置:网站首页>Go language to realize static server
Go language to realize static server
2022-07-03 11:50:00 【Brother Xing plays with the clouds】
Study Go Language Some feelings of , Not necessarily accurate . In case of war ,JAVA It usually acts as an aircraft carrier battle group . Once out , It's the frigate 、 cruiser 、 Carrier based aircraft 、 Awacs... 、 EW aircraft 、 Submarines and so on The majestic , Kill will pass . (JVM, Dozens of JAR package ,Tomcat middleware ,SSH frame , Various configuration files ... Born to be a heavyweight , Designed for large-scale operations ) and GO Language is more like F35 Fighter bomber single-handed , quietly , Drop the bomb and leave . Exclusive bomber , Air combat will also be a little . I'm not sure , Just call it big brother F22. (GO It's a compiled language , Don't rely on , No virtual machine required , You can call C Code and it's simple enough , But very comprehensive ) plan Go Language What to learn 1. build Http service 2. Connect to database 3. Local IO 4. Multithreading 5. The Internet 6. Call the local command 7. call C The language code First , Build a static The server I like to write programs using HTML adopt AJAX send out JSON Request to back-end processing . HttpServer.go package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var realPath *string
func staticResource(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
request_type := path[strings.LastIndex(path, "."):]
switch request_type {
case ".css":
w.Header().Set("content-type", "text/css")
case ".js":
w.Header().Set("content-type", "text/javascript")
default:
}
fin, err := os.Open(*realPath + path)
defer fin.Close()
if err != nil {
log.Fatal("static resource:", err)
}
fd, _ := ioutil.ReadAll(fin)
w.Write(fd)
}
func main() {
realPath = flag.String("path", "", "static resource path")
flag.Parse()
http.HandleFunc("/", staticResource)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
} See a more BT Methods ..
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("/tmp/static/")))
http.ListenAndServe(":8080", nil)
} take EasyUI Unzip the front-end framework to /tmp/static Under the table of contents
stay GOPATH perform go run HttpServer.go --path=/tmp/static View the web page , Everything is all right .
such Go Language In less than 50 Line code , Less than after compilation 7M The executable of , A simple Static servers .
边栏推荐
- 剑指offer专项32-96题做题笔记
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- 一些常用术语
- Repo ~ common commands
- Hongmeng fourth training
- This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
- Program process management tool -go Supervisor
- R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
- CSRF
- mysql使用update联表更新的方法
猜你喜欢
聊聊Flink框架中的状态管理机制
vulnhub之presidential
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
STL教程9-容器元素深拷贝和浅拷贝问题
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
鸿蒙第三次培训(项目实训)
vulnhub之momentum
Kubernetes 三打探针及探针方式
【学习笔记】dp 状态与转移
Vulnhub geminiinc
随机推荐
Kibana - installation and configuration of kibana
Vulnhub's presidential
牛牛的组队竞赛
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
phpcms 提示信息页面跳转showmessage
Uniapp implementation Click to load more
STL tutorial 8-map
R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
银泰百货点燃城市“夜经济”
STL教程9-容器元素深拷贝和浅拷贝问题
Qt+VTK+OCCT读取IGES/STEP模型
PHP server interacts with redis with a large number of close_ Wait analysis
Viewing binary bin files with notepad++ editor
previous permutation lintcode51
利用Zabbix动态监控磁盘I/O
鸿蒙第三次培训(项目实训)
C language AES encryption and decryption
同事写了一个责任链模式,bug无数...
phpcms 提示信息頁面跳轉showmessage
Unity3D学习笔记5——创建子Mesh