62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
const (
|
|
APPNAME = "Question"
|
|
)
|
|
|
|
func main() {
|
|
cf := flag.String("config", "config.yaml", "file of config")
|
|
flag.Parse()
|
|
viper.SetConfigFile(*cf)
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
log.WithFields(log.Fields{
|
|
"func": "main",
|
|
}).Errorf("%s", err.Error())
|
|
return
|
|
}
|
|
//返回数据格式是json
|
|
http.HandleFunc("/", receiveClientRequest)
|
|
|
|
InitMysql()
|
|
// handler.SendForm()
|
|
router := gin.New()
|
|
setupRouters(router)
|
|
// s := &http.Server{
|
|
// Addr: viper.GetString("api.host"),
|
|
// Handler: router,
|
|
// ReadTimeout: 10 * time.Second,
|
|
// WriteTimeout: 10 * time.Second,
|
|
// MaxHeaderBytes: 1 << 20,
|
|
// }
|
|
// fmt.Println(viper.GetString("api.host"))
|
|
// log.WithFields(log.Fields{
|
|
// "func": "main",
|
|
// }).Infof("%s on %s", APPNAME, viper.GetString("api.host"))
|
|
|
|
// if err := s.ListenAndServe(); err != nil {
|
|
// log.WithFields(log.Fields{
|
|
// "func": "main",
|
|
// }).Errorf("%s", err.Error())
|
|
// }
|
|
router.Run()
|
|
}
|
|
func receiveClientRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
|
|
w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型
|
|
w.Header().Set("content-type", "application/json") //返回数据格式是json
|
|
|
|
r.ParseForm()
|
|
fmt.Println("收到客户端请求: ", r.Form)
|
|
}
|