From 35fd7495aac7e9831398ccc4fbd5a4aa4c84c677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Mon, 29 May 2023 10:20:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=B2=E6=9F=93readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 20 +------------------- routers/docs.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 routers/docs.go diff --git a/main.go b/main.go index 1407b39..6c4da6b 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "io/ioutil" "log" "net/http" "runtime" @@ -11,7 +10,6 @@ import ( "main/utils" "github.com/gorilla/mux" - "github.com/russross/blackfriday" ) func main() { @@ -37,23 +35,7 @@ func main() { }) // 設定路由 - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - input, err := ioutil.ReadFile("./README.md") - if err != nil { - log.Println(err) - return - } - output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(0, "", ""), blackfriday.EXTENSION_TABLES|blackfriday.EXTENSION_FENCED_CODE|blackfriday.EXTENSION_AUTOLINK) - css := `` - html := "API Document" + css + "" + string(output) + "" - w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.Write([]byte(html)) - }) + r.HandleFunc("/", routers.GetDocs).Methods("GET") r.HandleFunc("/api/users", routers.UsersGet).Methods("GET") r.HandleFunc("/api/users", routers.UsersPost).Methods("POST") diff --git a/routers/docs.go b/routers/docs.go new file mode 100644 index 0000000..58b25bb --- /dev/null +++ b/routers/docs.go @@ -0,0 +1,46 @@ +package routers + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + + "github.com/russross/blackfriday" +) + +func GetDocs(w http.ResponseWriter, r *http.Request) { + input, err := ioutil.ReadFile("./README.md") + if err != nil { + log.Println(err) + return + } + output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(0, "", ""), blackfriday.EXTENSION_TABLES|blackfriday.EXTENSION_FENCED_CODE|blackfriday.EXTENSION_AUTOLINK) + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Write([]byte(fmt.Sprintf(` + + + + API Document + + + + + + + + + + + + + %s + + + `, string(output)))) +}