1 Star 0 Fork 0

solid / sutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 2.55 KB
一键复制 编辑 原始数据 按行查看 历史
Solid 提交于 2024-03-19 16:53 . 添加spsutil进程模块
package main
import (
"crypto/md5"
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"strings"
_ "net/http/pprof"
"github.com/quic-go/quic-go/http3"
)
type binds []string
func (b binds) String() string {
return strings.Join(b, ",")
}
func (b *binds) Set(v string) error {
*b = strings.Split(v, ",")
return nil
}
// Size is needed by the /demo/upload handler to determine the size of the uploaded file
type Size interface {
Size() int64
}
// See https://en.wikipedia.org/wiki/Lehmer_random_number_generator
func generatePRData(l int) []byte {
res := make([]byte, l)
seed := uint64(1)
for i := 0; i < l; i++ {
seed = seed * 48271 % 2147483647
res[i] = byte(seed)
}
return res
}
func setupHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/demo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
mux.HandleFunc("/demo/tiles", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "<html><head><style>img{width:40px;height:40px;}</style></head><body>")
for i := 0; i < 200; i++ {
fmt.Fprintf(w, `<img src="/demo/tile?cachebust=%d">`, i)
}
io.WriteString(w, "</body></html>")
})
mux.HandleFunc("/demo/echo", func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
fmt.Printf("error reading body while handling /echo: %s\n", err.Error())
}
w.Write(body)
})
// accept file uploads and return the MD5 of the uploaded file
// maximum accepted file size is 1 GB
mux.HandleFunc("/demo/upload", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
err := r.ParseMultipartForm(1 << 30) // 1 GB
if err == nil {
var file multipart.File
file, _, err = r.FormFile("uploadfile")
if err == nil {
var size int64
if sizeInterface, ok := file.(Size); ok {
size = sizeInterface.Size()
b := make([]byte, size)
file.Read(b)
md5 := md5.Sum(b)
fmt.Fprintf(w, "%x", md5)
return
}
err = errors.New("couldn't get uploaded file size")
}
}
log.Printf("Error receiving upload: %#v", err)
}
io.WriteString(w, `<html><body><form action="/demo/upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadfile"><br>
<input type="submit">
</form></body></html>`)
})
return mux
}
func main() {
handler := setupHandler()
var certFile = "server.crt"
var keyFile = "server.key"
fmt.Println("listening on", ":6060")
err := http3.ListenAndServe(":6060", certFile, keyFile, handler)
if err != nil {
fmt.Println(err)
}
}
Go
1
https://gitee.com/solidone/sutils.git
git@gitee.com:solidone/sutils.git
solidone
sutils
sutils
f1241d18e4eb

搜索帮助