渲染readme
This commit is contained in:
20
main.go
20
main.go
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -11,7 +10,6 @@ import (
|
|||||||
"main/utils"
|
"main/utils"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/russross/blackfriday"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -37,23 +35,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 設定路由
|
// 設定路由
|
||||||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/", routers.GetDocs).Methods("GET")
|
||||||
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 := `<style type="text/css">
|
|
||||||
body {
|
|
||||||
max-width: 960px;
|
|
||||||
margin: 0 auto;
|
|
||||||
}
|
|
||||||
</style>`
|
|
||||||
html := "<!DOCTYPE html><html><head><title>API Document</title>" + css + "</head><body>" + string(output) + "</body></html>"
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
w.Write([]byte(html))
|
|
||||||
})
|
|
||||||
|
|
||||||
r.HandleFunc("/api/users", routers.UsersGet).Methods("GET")
|
r.HandleFunc("/api/users", routers.UsersGet).Methods("GET")
|
||||||
r.HandleFunc("/api/users", routers.UsersPost).Methods("POST")
|
r.HandleFunc("/api/users", routers.UsersPost).Methods("POST")
|
||||||
|
46
routers/docs.go
Normal file
46
routers/docs.go
Normal file
@@ -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(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>API Document</title>
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/2.10.0/github-markdown.min.css">
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
|
||||||
|
<script>hljs.initHighlightingOnLoad();</script>
|
||||||
|
|
||||||
|
<!-- 使 highlight 支持 go 語言的代碼解析 -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/go.min.js"></script>
|
||||||
|
<script>hljs.initHighlightingOnLoad();</script>
|
||||||
|
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
max-width: 960px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body class="markdown-body">
|
||||||
|
%s
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`, string(output))))
|
||||||
|
}
|
Reference in New Issue
Block a user