Esempio n. 1
0
func main() {
	errorlog, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		fmt.Printf("error opening file: %v", err)
		os.Exit(1)
	}
	defer errorlog.Close()
	logger = log.New(errorlog, "applog: ", log.Lshortfile|log.LstdFlags)

	settings, err := setting.Create()
	if err != nil {
		logger.Fatal(err)
	}
	err = settings.CreateDir()
	if err != nil {
		logger.Fatal(err)
	}
	mux := http.NewServeMux()
	pagehandler := handler.NewPageHandler(settings.DataDirPath, settings.TargetURL)
	curdhandler := handler.NewMockCURDHandler(settings.DataDirPath)
	viewDataHandler := handler.NewViewDataHandler(settings.DataDirPath)

	wsHandler := ws.NewHandler(logger)
	mockhandler := handler.NewMockhandler(settings.TargetURL, settings.DataDirPath, *wsHandler)
	mux.Handle("/mock/wsmockinfo", websocket.Handler(wsHandler.WSMockInfoServer))
	go wsHandler.Run()

	mux.Handle("/mock", &pagehandler)
	mux.Handle("/mock/api/mock/", &curdhandler)
	mux.Handle("/mock/api/mock/data/", &viewDataHandler)

	mux.HandleFunc("/mock/edit/", pagehandler.EditHandler)
	mux.HandleFunc("/mock/new/", pagehandler.New)
	mux.Handle("/", &mockhandler)
	logger.Printf("Server to listen on a port: %v \n", settings.Port)
	logger.Fatal(http.ListenAndServe(":"+strconv.Itoa(settings.Port), mux))
}
Esempio n. 2
0
func TestInitSetting_WhenUrlHasPortNumber(t *testing.T) {

	oldArgs := os.Args

	os.Args = []string{"cmd", "-u=google.com:9090"}
	expectedURL := "google.com:9090"
	expectedParnetDirName := "google.com9090"

	settings, err := setting.Create()
	if err != nil {
		t.Errorf("Error occured while trying to create settings: %v", err)
	}

	if settings.TargetURL != expectedURL {
		t.Errorf("Expected: %v but got: %v", expectedURL, settings.TargetURL)
	}

	segments := strings.Split(settings.DataDirPath, string(filepath.Separator))
	parentDir := segments[len(segments)-1]
	if parentDir != expectedParnetDirName {
		t.Errorf("Expected: %v but got: %v", expectedParnetDirName, parentDir)
	}
	defer func() { os.Args = oldArgs }()
}