You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
)
func articleList(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "article service: article list")
}
func articleRetrieve(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "article service: article retrieve")
}
func articleCreate(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "article service: article create")
}
func articleDelete(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "article service: article delete")
}
func articleUpdate(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "article service: article update")
}
func articleUpdatePartial(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "article service: article update partial")
}
func main() {
// mux 包定义路由
router := mux.NewRouter()
// restful api 定义
router.HandleFunc("/articles", articleList).Methods("GET")
router.HandleFunc("/articles/{id}", articleRetrieve).Methods("GET")
router.HandleFunc("/articles", articleCreate).Methods("POST")
router.HandleFunc("/articles/{id}", articleDelete).Methods("DELETE")
router.HandleFunc("/articles/{id}", articleUpdate).Methods("PUT")
router.HandleFunc("/articles/{id}", articleUpdatePartial).Methods("PATCH")
// http 监听
log.Fatalln(http.ListenAndServe(":8088", router))
}