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 quicktemplate 模版快速入门

Golang 有很多的模版引擎,自带的 `html/template` 也很好,大多数情况都能满足需求,只是有些逻辑、条件判断不好在模版里实现, `quicktemplate` 是个很好的选择。...

Golang 实现 10 进制转 N 进制

给定一个不没有重复字符的字符串,如 `[0-9,a-z]`,把一个 10 进制数字转为,该字符集的字符串。应用场合如汽车牌、顺序计数。...

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

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