GolangNote

Golang笔记

golang goji 同时实现接口和静态文件服务

Permalink

golang goji 同时实现接口和静态文件服务

Go: main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import (
	"fmt"
	"net/http"

	"github.com/zenazn/goji"
	"github.com/zenazn/goji/web"
)

func apiExampleHandler(context web.C, resp http.ResponseWriter, req *http.Request) {
	fmt.Fprint(resp, "You've hit the API!")
}

func main() {
	goji.Handle("/api", apiExampleHandler)

	// Static file handler should generally be the last handler registered. Otherwise, it'll match every path.
	// Be sure to use an absolute path.
	staticFilesLocation := "/Users/Retired/Go/src/github.com/Retired/26320144/public"
	goji.Handle("/*", http.FileServer(http.Dir(staticFilesLocation)))

	goji.Serve()
}

本文网址: https://golangnote.com/topic/133.html 转摘请注明来源

Related articles

golang 实现的基于web的文件管理-filebrowser

FileBrowser 在指定目录中提供了一个文件管理界面,可用于上传,删除,预览,重命名和编辑文件。它允许创建多个用户,每个用户都可以有自己的目录。它可以用作独立的应用程序。...

Golang Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Write a Comment to "golang goji 同时实现接口和静态文件服务"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms