mux

Go Web 编程之 静态文件

↘锁芯ラ 提交于 2020-01-14 07:52:07
概述 在 Web 开发中,需要处理很多静态资源文件,如 css/js 和图片文件等。本文将介绍在 Go 语言中如何处理文件请求。 接下来,我们将介绍两种处理文件请求的方式:原始方式和 http.FileServer 方法。 原始方式 原始方式比较简单粗暴,直接读取文件,然后返回给客户端。 func main() { mux := http.NewServeMux() mux.HandleFunc("/static/", fileHandler) server := &http.Server { Addr: ":8080", Handler: mux, } if err := server.ListenAndServe(); err != nil { log.Fatal(err) } } 上面我们创建了一个文件处理器,将它挂载到路径 /static/ 上。一般地,静态文件的路径有一个共同的前缀,以便与其它路径区分。如这里的 /static/ ,还有一些常用的,例如 /public/ 等。 代码的其它部分与 程序模板 没什么不同,这里就不赘述了。 另外需要注意的是,这里的注册路径 /static/ 最后的 / 不能省略。我们在前面的文章 程序结构 中介绍过,如果请求的路径没有精确匹配的处理,会逐步去掉路径最后部分再次查找。 静态文件的请求路径一般为 /static/hello.html

Go Web 编程之 程序结构

浪子不回头ぞ 提交于 2020-01-10 10:16:11
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 概述 一个典型的 Go Web 程序结构如下,摘自《Go Web 编程》: 客户端发送请求; 服务器中的多路复用器收到请求; 多路复用器根据请求的 URL 找到注册的处理器,将请求交由处理器处理; 处理器执行程序逻辑,必要时与数据库进行交互,得到处理结果; 处理器调用模板引擎将指定的模板和上一步得到的结果渲染成客户端可识别的数据格式(通常是 HTML); 最后将数据通过响应返回给客户端; 客户端拿到数据,执行对应的操作,如渲染出来呈现给用户。 本文介绍如何创建多路复用器,如何注册处理器,最后再简单介绍一下 URL 匹配。我们以上一篇文章中的"Hello World"程序作为基础。 package main import ( "fmt" "log" "net/http" ) func hello(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World") } func main() { http.HandleFunc("/", hello) if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) } } 多路复用器 默认多路复用器 net

Get TS packets into buffer from libavformat

北城以北 提交于 2020-01-06 05:49:05
问题 I would like to capture video, compress it and mux it as an mpeg2 ts. All of this is quite simple with the ffmpeg libraries, but what I would like to do, instead of writing it to a file, is capture the ts packets in a buffer and using this stream in other ways. Is there a simple way to do this? To write the encoded frames to file right now, I use av_interleaved_write_frame(oc, &pkt). OC is an output context with a filename and format defined. pkt is an AVPacket containing the compressed frame

How to call a route by its name from inside a handler?

那年仲夏 提交于 2020-01-02 09:20:07
问题 How do I properly refer to route names from inside handlers? Should mux.NewRouter() be assigned globally instead of standing inside a function? func AnotherHandler(writer http.ResponseWriter, req *http.Request) { url, _ := r.Get("home") // I suppose this 'r' should refer to the router http.Redirect(writer, req, url, 302) } func main() { r := mux.NewRouter() r.HandleFunc("/", HomeHandler).Name("home") r.HandleFunc("/nothome/", AnotherHandler).Name("another") http.Handle("/", r) http

Adding NAL headers to raw H264 stream

痴心易碎 提交于 2019-12-25 10:55:26
问题 In order to add NAL headers to a raw H264 stream, what do I need to do? Ultimately, I want to take the video file and mux it with an audio file. 2 related questions: 1.) I've looked at this post: H.264 conversion with FFmpeg (from a RTP stream) However, when I look at the first two bytes of each frame, I'm seeing 0 and 0 for any frame I record, which doesn't say anything about the NAL contents. Am I looking at the right place? Is there another way I can create and write in NAL headers to a

Feedback on Mux fail to run in Verilog

放肆的年华 提交于 2019-12-25 04:12:11
问题 I am doing the calculation where the output is one of the next input. However the mux output just providing X and causes all the rest calculation to go wrong. How to overcome this issue? Is it due to the clock? Here is my code: module all2(input sel,clk, input signed [15:0]x,d, output signed [15:0]w,e,y); localparam u = 16'd2; wire [15:0]w1; reg [15:0]y1,e1,u1,wk; assign w = wk; assign e = e1; assign y = y1; assign w1 = (sel)?wk:16'd0; always @(posedge clk or negedge clk) y1 <= x * w1; always

GStreamer: Add dummy audio track to the received rtp stream

﹥>﹥吖頭↗ 提交于 2019-12-24 00:51:58
问题 I'm initiating RTP stream from my Raspberry camera using: raspivid -n -vf -fl -t 0 -w 640 -h 480 -b 1200000 -fps 20 -pf baseline -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay pt=96 config-interval=10 ! udpsink host=192.168.2.3 port=5000 on the client site, I'm converting it to HLS and upload it on a web server: gst-launch-1.0 udpsrc port=5000 ! application/x-rtp,payload=96 ! rtph264depay ! mpegtsmux ! hlssink max-files=5 target-duration=5 location=C:/xampp/htdocs/live/segment%%05d

How to create a route with optional url var using gorilla mux?

夙愿已清 提交于 2019-12-23 09:58:33
问题 I want to have an optional URL variable in route. I can't seem to find a way using mux package. Here's my current route: func main() { r := mux.NewRouter() r.HandleFunc("/view/{id:[0-9]+}", MakeHandler(ViewHandler)) http.Handle("/", r) http.ListenAndServe(":8080", nil) } It works when the url is localhost:8080/view/1 . I want it to accept even if there's no id so that if I enter localhost:8080/view it'll still work. Thoughts? 回答1: You could define a new HandleFunc for the root /view path: r

mux raw h 264 to an mp4 file, some odd errors

删除回忆录丶 提交于 2019-12-23 02:01:33
问题 What I'm doing is on an IOS app with Xcode 7.3. I got h264 data from an ip camera using UDP,the data can be decoded and displayed rightly (decoded by ffmpeg). Now I want to mux the raw H264 data to an mp4 file(some users may want to record what they are watching on his cell-phone), using ffmpeg. Nothing wrong happened when the code is running, and the result file can be played normally with QuickTime on my computer. But when played on iphone with iphone's default video player, it can't be

Fetching a URL From the init() func in Go on AppEngine

。_饼干妹妹 提交于 2019-12-19 10:19:21
问题 Background : I'm running Go on GAE and using Mux for my router. In order to fetch a URL GAE requires that I use its built in urlFetch capability. I want to make this URL fetch happen during my modules init() but as far as I can tell I can only use urlFetch when invoked via a handler. func init() { r := mux.NewRouter() r.HandleFunc("/", homeHandler) r.HandleFunc("/about", anotherHandler) http.Handle("/", r) } GAE suggests the following code for making a urlFetch: c := appengine.NewContext(r)