How do I make a Web site that compiles code online?

Source: Internet
Author: User
Just like some code-racing sites, uploading code directly can see the results on a Web page, and the official Go language website has similar features. How is it implemented? Are there any ready-made solutions?

Reply content:

If you need a ready-made solution, this is good:

https:// github.com/mattgodbolt/ Gcc-explorer

Effect preview:

Compiler Explorer


If you are implementing a programming language for traditional use, the content involved is very miscellaneous, including:

    • Web server +web The component that the server interacts with the backend program (this can be used off-the-shelf, I use nginx+fcgi)
    • Back-end Programs

The backend core is a similar to the call system to EXECL-like API to execute the corresponding compilation tool, with this API, you have to carefully handle the pipeline, export output to display back to the page.

But because this kind of program and OJ, is the public service on the network and actually also lets the user upload the code compiles the execution, therefore the question mainly concentrates on the security processing, and the security question processing highly relies on the operating system.

In Linux, for example, security includes but not only the following:

    1. Build on environment, sandbox
    2. Run identity, including GID, UID
    3. Resource limitations, including sockets, file operations, core dump size, number of files, number of sub-processes, and more
    4. Syscall Restrictions
    5. Execution time limit, including run time and CPU time
    6. File system pressure

Windows is another heap of problems to solve, much more complicated than Linux (maybe I'm not very good with Windows).


I know a person, I realized a similar Haskell, and go playground almost online running web page, I asked him how quickly so many things are finished, he said, "the whole language is to use JS implementation, so there is a browser can run, do not need backend", This is also a way of thinking.

The first two days of the course design is just the root of the students together to design a simple online game platform, and usually also often to the site to do the game, simply say it to achieve it.

After committing the code to the Web site, save it as code, and in PHP or JSP there is something called the system command, such as the EXEC function in PHP. At this point you need a script, compile the resulting code, run it, get the running information and save it to the database, as in PHP you can write an implementation statement:
exec ("./onj $destFile $file", $output, $verdict);
From Onj Project-processfile.php-line
This is called the function of the sentence, in principle can be written in any language. Onj is written in Python, compiling, running, and judging the code. It needs to get a standard input to a game title, redirect the standard output to a file, and then compare the answers to the answer to whether the user-submitted code can produce the correct results.
Such a system is much better on the Internet, recommended a few:
1. ONJ / http Sourceforge.net/project s/onj/ PHP, the code is very neat, I like it, Chinese support is not perfect.
2.NEUOJ https:// github.com/yangzhe1991/ Neuoj The question is written in Python.
3.HUSTOJ / http Code.google.com/p/husto j/ Domestic seems to use more, I did not use.

It's too ugly to write.
The site of this tournament is more familiar to people who are engaged in ACM, if anyone who knows can ask. The problem with working directly with the web process is that the web is lightweight, and that the number of concurrent numbers is quite normal.
While compiling and running is more heavyweight, not too much concurrency, and must be controlled.
So there should be a task queue mechanism, successively orderly.
As for safety, you can open a different topic. You should call the compile command directly and check the output.
As for security, you should be able to do this with a user with a low level of privilege.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.