func InitServer() { config := conf.InitConfig(tools.DEFAULT_CONFIG_FILE) //http.HandleFunc(config.Server.HandlerList[0].Action, config.Server.HandlerList[0].MyFunc) http.HandleFunc(config.Server.HandlerList[0].Action, index) err := http.ListenAndServe(":"+config.Server.Port, nil) if err != nil { tools.Println(err) } }
func InitConfig(filePath string) (conf *Config) { if filePath == "" { filePath = tools.DEFAULT_CONFIG_FILE } if config == nil { file, err := tools.LoadFile(filePath) tools.Println(err) reader := bufio.NewReaderSize(file, tools.DEFAULT_BUFFER_SIZE) buf := make([]byte, 0, tools.DEFAULT_BUFFER_SIZE) bufs := bytes.NewBuffer(buf) for { buf, err := reader.ReadBytes('\n') tools.Println(buf) bufs.Write(buf) if err != nil { break } } tools.Println(bufs.Len()) if bufs.Len() == 0 { //nothing config = &Config{ Server: serverConfig{Addr: "127.0.0.1", Port: "3333", HandlerList: []handlerItem{handlerItem{Action: "/", MyFunc: "index"}}}, Redis: redisConfig{Addr: "127.0.0.1", Port: "6379"}, } tools.Println(config) writer := bufio.NewWriterSize(file, tools.DEFAULT_BUFFER_SIZE) buf, err = json.Marshal(&config) tools.Println(err) n, err := writer.Write(buf) tools.Println(n, err) writer.Flush() } else { err = json.Unmarshal(bufs.Bytes(), &config) tools.Println(err) } } return config }
func index(w http.ResponseWriter, r *http.Request) { tools.Println(w, r) }