func main() { var port = flag.String("port", "8080", "Declare a port to listen on.") flag.Parse() f, err := os.OpenFile("logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("error opening file: %v", err) } defer f.Close() // log.SetOutput(f) log.Printf("Listening on port %v...\n", *port) r := mux.NewRouter() mw.Decorate( r, LoggingMW, ) r.HandleFunc("/observer-mode/rest/featured", spec.FeaturedHandler) r.HandleFunc("/observer-mode/rest/consumer/version", spec.VersionHandler) r.HandleFunc("/observer-mode/rest/consumer/getGameMetaData/{platformId}/{gameId}/{yolo}/token", spec.GetGameMetaDataHandler) r.HandleFunc("/observer-mode/rest/consumer/getLastChunkInfo/{platformId}/{gameId}/{param}/token", spec.GetLastChunkInfoHandler) r.HandleFunc("/observer-mode/rest/consumer/getLastChunkInfo/{platformId}/{gameId}/null", spec.EndOfGameStatsHandler) r.HandleFunc("/observer-mode/rest/consumer/getGameDataChunk/{platformId}/{gameId}/{chunkId}/token", spec.GetGameDataChunkHandler) r.HandleFunc("/observer-mode/rest/consumer/getKeyFrame/{platformId}/{gameId}/{keyFrameId}/token", spec.GetKeyFrameHandler) http.Handle("/", r) if err := http.ListenAndServe(":"+*port, nil); err != nil { panic(err) } }
func TestDecorate(t *testing.T) { // new router r := http.NewServeMux() expectedContentType := "application/json" expectedStatus := http.StatusCreated r.HandleFunc("/api/data", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{"data":"json"}`)) }) // decorate router server := mw.Decorate( r, CreatedMiddleware(), JSONMiddleware(), ) http.Handle("/api/", server) go http.ListenAndServe(":8080", nil) c := http.Client{} req, err := http.NewRequest("GET", "http://localhost:8080/api/data", nil) if err != nil { t.Errorf("error creating request: %s\n", err) } resp, err := c.Do(req) if err != nil { t.Errorf("error making request: %s\n", err) } // checks if JSONMiddlware was called actualContentType := resp.Header.Get("Content-Type") if expectedContentType != actualContentType { t.Errorf("expected %s and got %s", expectedContentType, actualContentType) } // checks if CreatedMiddleware was called actualStatus := resp.StatusCode if expectedStatus != actualStatus { t.Errorf("expected %s and got %s", expectedContentType, actualContentType) } }
func main() { // new router r := mux.NewRouter() r.HandleFunc("/api/data", DataHandler).Methods("GET") // decorate router server := mw.Decorate( r, // add middleware from existing packages nosurf.NewPure, // or your own JSONMiddleware(), ) http.Handle("/api/", server) err := http.ListenAndServe(":8080", nil) if err != nil { panic(err) } }
func main() { /* * lolclient connect port * lolclient path * game connect port * tgp parameter * */ /* * 本工具参数 */ var lolclient_port uint16 var lolroot_path string var lolobfile string var lolgame_port uint16 var help bool = false // var version bool = false fmt.Println() fmt.Println("版 本: lol_launcher_mac " + LAUNCHER_VERSION + " build at " + BUILD_DATE + " By CFC4N ([email protected])") fmt.Println("声 明: 本软件仅供技术交流,游戏娱乐,请勿用于非法用途。\n") s := getopt.New() /* * 接收本工具参数 */ // s.StringVarLong(&lolroot_path, "path", 'X', "The root path of League of Legends games", "/Applications/League of Legends.app/") // s.Uint16VarLong(&lolclient_port, "client_port", 'Y',"The port LolClient connected.") s.StringVarLong(&lolobfile, "obfile", 'f', "ob录像文件所在路径,建议放在replays目录下。") s.Parse(os.Args) if help { s.PrintUsage(os.Stderr) return } if len(lolobfile) <= 0 { s.PrintUsage(os.Stderr) return } lolroot_path = getCurrentDirectory() lolCommands := command.NewLolCommand() //设置游戏安装目录,以及日志目录 lolCommands.LolSetConfigPath("/Applications/League of Legends.app/", lolroot_path) //获取游戏中,大厅程序以及游戏进程程序所在目录等配置 lolCommands.LolGetConfig() /* * * 参数判断 */ if lolclient_port <= 0 || lolclient_port >= 65535 { lolclient_port = DEFAULT_CLIENT_PORT } if lolgame_port <= 0 || lolgame_port >= 65535 { lolgame_port = DEFAULT_GAME_PORT } /* * */ //观看录像模式 log.Println("录像观看模式已启动...") filePath := lolroot_path + "/" + lolobfile log.Println("加载录像文件:", filePath) //加载分析OB文件 err := replay.Loadfile(filePath) if err != nil { panic(err) } listenHost := "127.0.0.1:" + strconv.Itoa(int(DEFAULT_REPLAY_PORT)) // fmt.Println(replay.GameInfo) // os.Exit(0) params := "spectator " + listenHost + " " + replay.GameInfo.Encryption_key + " " + strconv.Itoa(int(replay.GameInfo.Game_id)) + " " + replay.GameMetaData.GameKey.PlatformId // log.SetOutput(f) log.Println("录像回放服务已监听:", listenHost) r := mux.NewRouter() mw.Decorate( r, LoggingMW, ) r.HandleFunc("/observer-mode/rest/featured", replay.FeaturedHandler) r.HandleFunc("/observer-mode/rest/consumer/version", replay.VersionHandler) r.HandleFunc("/observer-mode/rest/consumer/getGameMetaData/{platformId}/{gameId}/{yolo}/token", replay.GetGameMetaDataHandler) r.HandleFunc("/observer-mode/rest/consumer/getLastChunkInfo/{platformId}/{gameId}/{param}/token", replay.GetLastChunkInfoHandler) r.HandleFunc("/observer-mode/rest/consumer/getLastChunkInfo/{platformId}/{gameId}/null", replay.EndOfGameStatsHandler) r.HandleFunc("/observer-mode/rest/consumer/getGameDataChunk/{platformId}/{gameId}/{chunkId}/token", replay.GetGameDataChunkHandler) r.HandleFunc("/observer-mode/rest/consumer/getKeyFrame/{platformId}/{gameId}/{keyFrameId}/token", replay.GetKeyFrameHandler) http.Handle("/", r) //启动游戏 // log.Println("录像播放参数:",params) go lolCommands.LolGameCommand(strconv.Itoa(int(lolgame_port)), params) if err := http.ListenAndServe(listenHost, nil); err != nil { panic(err) } //监听系统关闭消息 log.Println("软件退出。") os.Exit(0) }