Ejemplo n.º 1
0
func process(output *Output, err error) {
	if err != nil {
		log.Info("%v", err)
	} else {
		log.Info("%#v", output)
	}
}
Ejemplo n.º 2
0
func runIt(runner *code.Runner) {
	input := code.MakeInput("cpp", "main.cpp", CODE2, STDIN_EXAMPLE)
	output, err := runner.Run(input)
	if err != nil {
		log.Info("%v", err)
	} else {
		log.Info("%#v", output)
	}
}
Ejemplo n.º 3
0
func GistFetch(id string) *EvalContext {
	client := github.NewClient(nil)
	gist, file, err := fetchFile(client, id, "", "eval.json")
	if err != nil {
		log.Fatal(err)
	}
	log.Info("%+v", file)

	v := &EvalContext{}
	err = json.Unmarshal([]byte(file.Content), v)
	log.Info("%+v\n", v)
	log.Info("%+v\n %+v\n %+v\n", v.Generator, v.Solution, v.Test)
	updateInput(client, gist, v.Generator, v.Solution, v.Test)
	return &EvalContext{v.Generator, v.Solution, v.Test}
}
Ejemplo n.º 4
0
func Parse(reader io.Reader) ([]*importer.SpotifySong, error) {
	decoder := json.NewDecoder(reader)
	var root map[string]interface{}
	err := decoder.Decode(&root)
	if err != nil {
		return nil, err
	}
	songs := make([]*importer.SpotifySong, 0)
	var objects map[string]interface{}
	objects = root["objects"].(map[string]interface{})
	for _, o := range objects {
		odata := (o).(map[string]interface{})
		artistField := odata["albumArtist"]
		if artistField == nil {
			// probably not a song, skip it
			continue
		}
		artist := artistField.(string)
		album := odata["album"].(string)
		name := odata["name"].(string)
		songs = append(songs, &importer.SpotifySong{Name: name, Artist: artist, Album: album})
	}
	log.Info("Parsed Conservatorio Json len %d", len(songs))
	return songs, nil
}
Ejemplo n.º 5
0
func initialize() {
	flag.Parse()
	assignString(PORT, *PORT, os.Getenv(prefixed("PORT")), "3014")
	assignString(ROOT, *ROOT, os.Getenv(prefixed("ROOT")), defaultRootDir())
	assignString(RUNNER, *RUNNER, os.Getenv(prefixed("RUNNER")), "./runner")
	log.Info("Using PORT=%s, ROOT=%s, RUNNER=%s", *PORT, *ROOT, *RUNNER)
}
Ejemplo n.º 6
0
func ExecuteCommandAsAdmin(cmd, username, pwd, domain string) {
	out, err := exec.Command(cmd).Output()
	if err != nil {
		log.Error(err)
	}
	log.Info(out)
}
Ejemplo n.º 7
0
func updateInput(client *github.Client, gist *github.Gist, inputs ...*Input) {
	for _, input := range inputs {
		log.Info("Input: %+v\n", input)
		if input == nil {
			continue
		}
		for i, file := range input.Files {
			log.Info("%+v", file)
			if file.Content == "" {
				if gistId, content := file.Id, gist.Files[github.GistFilename(file.Name)].Content; gistId == "" && content != nil {
					input.Files[i].Content = *content
					continue
				}
				if _, fetchedFile, err := fetchFile(client, file.Id, file.Sha, file.Name); err == nil {
					log.Info("Fetching %s from sha %s", file.Name, file.Id)
					input.Files[i].Content = fetchedFile.Content
				}
			}
		}
		//log.Info("Input: %+v\n", input)
	}
}
Ejemplo n.º 8
0
func Logger() echo.MiddlewareFunc {
	return func(h echo.HandlerFunc) echo.HandlerFunc {
		return func(c *echo.Context) error {
			req := c.Request()
			res := c.Response()

			remoteAddr := req.RemoteAddr
			if ip := req.Header.Get(echo.XRealIP); ip != "" {
				remoteAddr = ip
			} else if ip = req.Header.Get(echo.XForwardedFor); ip != "" {
				remoteAddr = ip
			} else {
				remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
			}

			start := time.Now()
			if err := h(c); err != nil {
				c.Error(err)
			}
			stop := time.Now()
			method := req.Method
			path := req.URL.Path
			if path == "" {
				path = "/"
			}
			size := res.Size()

			n := res.Status()
			code := color.Green(n)
			switch {
			case n >= 500:
				code = color.Red(n)
			case n >= 400:
				code = color.Yellow(n)
			case n >= 300:
				code = color.Cyan(n)
			}

			log.Info("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
			return nil
		}
	}
}
Ejemplo n.º 9
0
func realMain() int {

	// reads and parses application config params
	config, err := config.ReadFromFile("./config.json")
	if err != nil {
		fmt.Println("Config file reading error. Details: ", err)
		return 1
	}

	// creates Logger (can be replaced with other popular logger)
	log := log.New("GOB ")
	log.Debug("Application launched")

	// creates Backend object
	back, err := backend.New(&config.Backend, log)
	if err != nil {
		log.Error("Backend initialisation error. Details: ", err)
		return 2
	}

	// creates Frontend objects
	front, err := frontend.New(&config.Frontend, log, back)
	if err != nil {
		log.Error("Frontend initialisation error. Details: ", err)
		return 3
	}

	// starts background processes
	if err := back.Start(); err != nil {
		log.Error("Backend processes launching error. Details: ", err)
		return 4
	}

	// starts HTTP listener and handlers
	if err := front.Start(); err != nil {
		log.Error("Frontend listeners/processes error. Details: ", err)
		return 5
	}

	log.Info("Application succesfully terminated")
	return 0
}
Ejemplo n.º 10
0
func Evaluate(inputGen, inputCode1, inputCode2 *Input, runner *Runner) *Result {
	gen := []chan *Output{
		make(chan *Output),
		make(chan *Output),
	}
	results := make(chan struct {
		InputStr *string
		Output   *Output
	}, 2)
	go func() {
		output, err := runner.Run(inputGen)
		process(output, err)
		go func() { gen[0] <- output }()
		go func() { gen[1] <- output }()
	}()
	inputs := []*Input{inputCode1, inputCode2}
	for i := 0; i < 2; i++ {
		go func(i int) {
			genOutput := <-gen[i]
			input := inputs[i]
			log.Info("Using index: %d", i)
			log.Info("Code: %q", input.Files[0].Content)
			log.Info("Want: %q", genOutput.Stdout)
			log.Info("Before: %#v", input)
			UpdateStdin(input, StdinFile(genOutput.Stdout))
			log.Info("After: %#v", input)
			output, err := runner.Run(input)
			process(output, err)
			results <- struct {
				InputStr *string
				Output   *Output
			}{&genOutput.Stdout, output}
		}(i)
	}
	out1, out2 := <-results, <-results

	if diff(out1.Output.Stdout, out2.Output.Stdout) {
		log.Info("Different on input %q: %q %q", *out1.InputStr, out1.Output.Stdout, out2.Output.Stdout)
		return &Result{false}
	} else {
		log.Info("Identical on input %q", *out1.InputStr)
		return &Result{true}
	}
}
Ejemplo n.º 11
0
func (r *Runner) RunLocal(input *Input) (*Output, error) {
	log.Info("Starting local run...")
	if IsNotSupported(input.Language) {
		return &Output{}, errors.New(fmt.Sprintf("Language %s not supported", input.Language))
	}
	inputBytes, err := json.Marshal(input)
	if err != nil {
		return nil, err
	}
	inputStream := bytes.NewReader([]byte(inputBytes))
	outputStream := &bytes.Buffer{}

	runlib.Run(inputStream, outputStream)

	output := &Output{}
	err = json.Unmarshal(outputStream.Bytes(), &output)
	if err != nil {
		return nil, err
	}
	return output, nil

}
Ejemplo n.º 12
0
func configDb() {
	var dbconnect string

	dbconnect = dbuser
	if dbpassword != "" {
		dbconnect += ":" + dbpassword
	}
	dbconnect += "@"

	if dbhost != "" {
		dbconnect += "tcp(" + dbhost
		if dbport != "" {
			dbconnect += ":" + dbport
		}
		dbconnect += ")"
	}
	dbconnect += "/" + dbname + "?charset=utf8&parseTime=True"

	log.Info("Attempting to connect to db with:%s", dbconnect)

	dbm, err := gorm.Open("mysql", dbconnect)
	if err != nil {
		panic("Unable to connect to the database")
	}

	dbm.DB().Ping()
	dbm.DB().SetMaxIdleConns(10)
	dbm.DB().SetMaxOpenConns(100)
	dbm.LogMode(true)
	dbm.SingularTable(true)
	dbm.Set("gorm:table_options", "ENGINE=InnoDB")
	// and fix db on startup
	dbm.AutoMigrate(&models.User{})

	db = &dbm
}
Ejemplo n.º 13
0
func GistEvaluate(id string, runner *Runner) *Result {
	evalContext := GistFetch(id)
	result := Evaluate(evalContext.Generator, evalContext.Solution, evalContext.Test, runner)
	log.Info("Result: %v", result.Correct)
	return result
}
Ejemplo n.º 14
0
func runEcho() {
	log.Info("Running Echo Server on:%s:%s", ip, port)
	e.Run(ip + ":" + port)
}
Ejemplo n.º 15
0
func runHeartbeat() {
	log.Info("Running Heartbeat on:%s:%s/heartbeat", ip, port)
	e.Get("/heartbeat", heartbeat.Handler)
}