{"id":13689282,"url":"https://github.com/h5law/paste-server","last_synced_at":"2025-03-17T12:32:22.615Z","repository":{"id":231698870,"uuid":"525127414","full_name":"h5law/paste-server","owner":"h5law","description":"Go pastebin HTTP server","archived":false,"fork":false,"pushed_at":"2024-04-19T11:06:12.000Z","size":158,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2024-11-12T13:38:49.575Z","etag":null,"topics":["go","golang","pastebin","pastebin-api","pastebin-service"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/h5law.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-15T20:21:10.000Z","updated_at":"2024-04-05T10:19:51.000Z","dependencies_parsed_at":"2024-04-05T12:30:03.802Z","dependency_job_id":"19bbaf0a-d112-48ec-8ad3-1e95ebbbe814","html_url":"https://github.com/h5law/paste-server","commit_stats":null,"previous_names":["h5law/paste-server"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h5law%2Fpaste-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h5law%2Fpaste-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h5law%2Fpaste-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h5law%2Fpaste-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/h5law","download_url":"https://codeload.github.com/h5law/paste-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244033836,"owners_count":20387012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","pastebin","pastebin-api","pastebin-service"],"created_at":"2024-08-02T15:01:41.154Z","updated_at":"2025-03-17T12:32:22.593Z","avatar_url":"https://github.com/h5law.png","language":"Go","readme":"# paste-server\n\nThis is a simple Go HTTP server and router that interacts with a MongoDB\ndatabase to handle requests that allow for the creation, update, deletion and\nreading of \"pastes\".\n\nPastes are essentially files stored with some metadata in a MongoDB document\nwith the following structure:\n\n```\n{\n    _id:        ObjectId(\"...\"),\n    UUID:       String,\n    Content:    []String,\n    FileType:   String,\n    ExpiresAt:  DateTime,\n    AccessKey:  String,\n}\n```\n\n## Install\n\nBefore you can run an instance of the paste-server locally you must first\nset up and configure (only one command) a MongoDB instance see [here](#MongoDB).\n\n\nFirst clone this repo and enter into the directory\n\n```\ngit clone https://github.com/h5law/paste-server\ncd paste-server\n```\n\nThen create a [config](#Config) file (by default paste-server will look for it\nat `$HOME/.paste.yaml`) containing the MongoDB connection URI for the database\nyou have set up\n```\necho \"uri: \u003cyour connection URI here\u003e\" \u003e\u003e ~/.paste.yaml\n```\n\nThen install all dependencies\n```\ngo mod tidy\n```\n\nFinally build the binary\n```\ngo build -o paste-server\n```\n\nNow you can run `./paste-server` to see detailed usage info or to just simply\nrun the server\n```\n./paste-server start\n```\n\nTo set up the server to run as a daemon with systemd see [here](#Daemon)\n\n## Config\n\nThe config file is by default `$HOME/.paste.yaml` and is the same file used\nboth for a paste-server instance and the [paste-cli](https://github.com/h5law/paste-cli)\ntool. It can be pointed to any YAML file using the `-c/--config` flag.\n\nThe config file MUST contain the `uri` variable - the MongoDB connection string\nbut can also contain `app_env` a string of either `development` or `test` which\nwill make the instance use the `LetsEncryptStagingCA` if present otherwise it\nwill use the `LetsEncryptProductionCA` if `app_env` is not set or set to\nanything other than `test` or `development` when using the `-t/--tls` flag.\n\n```\nuri: \u003cMongoDB connection uri string\u003e\napp_env: \u003cdevelopment/test/(production -- optional not needed)\u003e\nurl: \u003cURL for paste-cli to use if not using the hosted instance at https://pastes.ch\u003e\n```\n\n## Daemon\n\nThe `paste-server.service` file contains a systemd service script used to\ndaemonise the paste-server instance. It requires the binary to be built and\nmoved to `/usr/local/bin/paste-server` and requries the config file containing\nthe MongoDB connection URI string to be saved at `/etc/paste.yaml` unless\nchanged.\n\nBy default the service file will start on port 80 as root. You will need to\nmake sure that your firewall has this port open for TCP connections (or for\nany port that you use). With firewalld you would run:\n```\nsudo firewall-cmd --add-port=\u003cPORT\u003e/tcp --permanent\nsudo firewall-cmd --reload\nsudo firewall-cmd --list-all\n```\n\nMake sure that the executable and config files have the right permissions:\n```\nsudo chown root:root /usr/local/bin/paste-server /etc/paste.yaml\nsudo chmod 755 /usr/local/bin/paste-server\nsudo restorecon -rv /usr/local/bin/paste-server\nsudo chmod 644 /etc/paste.yaml\n```\n\nMake sure to run `sudo restorecon -rv /etc/systemd/system/paste-server.service`\nafter moving the file so that systemd will recognise it - then run\n```\nsudo systemctl daemon-reload\nsudo systemctl start paste-server\nsudo systemctl enable paste-server\n```\n\n## Frontend\n\nFor those users who don't want to use the [paste cli tool](https://github.com/h5law/paste-cli)\nyou can build a Preact SPA and have the paste-server serve this prebuilt file\non the routes `/` and `/{uuid}` by specifying the static file directory for the\nbuilt assets.\n\nFirstly clone the paste-site [repo](https://github.com/h5law/paste-site):\n```\ngit clone https://github.com/h5law/paste-site\n```\n\n**This requires NodeJS version 16.16.0 or higher**. Then go into the directory\ninstall all its dependencies and build the static assets:\n```\ncd paste-site\nnpm i\nnpm run build\n```\n\nNow it is important to remember to use the `--spa-dir` flag when running the\npaste-server or no frontend will be provided on `/` and `/{uuid}` will give a\nsimple plaintext file. For example:\n```\npaste-server start -v --spa-dir=\"/var/www/paste-site/build\" --config=\"/etc/pastes.yaml\"\n```\n\nAnd if running as a daemon add the `--spa-dir` flag to the Exec line:\n```\nExecStart=/usr/local/bin/paste-server start -v -p 80 -l /var/log/paste-server --spa-dir \u003cPATH TO DIR\u003e -c /etc/paste.yaml\n\n```\n\n## TLS\n\nWhen starting the server in TLS mode make sure that you use the `--domain/-d`\nand `--email/-e` flags to set the domain of your site and the email to use for\nthe certificate or the server will not start properly.\n\nCurrently HTTP GET requests are forwarded to HTTPS but POST, PUT and DELETE\nrequests are not.\n\nI used the definition of the [HTTPS function](https://github.com/caddyserver/certmagic/blob/76f61c2947a20d86ca37669dbdc0ed7a96fc6c5f/certmagic.go#L68)\nfrom caddyserver's certmagic github documentation to implement the TLS server\nwith the ability to gracefully shutdown the server using the same context as\nwith the http server which was a great help!\n\n## MongoDB\n\nWhen setting up the database there are a few things you must do, ensure you\nare not using the \"pastes\" database or \"files\" collection namespaces already as\nthis is what the server will be using. Secondly create an index as follows:\n\n```\nuse pastes\ndb.files.createIndex( { \"expiresAt\": 1 }, { expireAfterSeconds: 0 } )\n```\n\nThis will automatically remove pastes when their expiration date is reached.\nBy default the server will use a period of 14 days but this can be altered.\n\n## Methods\n\nThe server supports CRUD operations on the database. Upon creating a paste the\nresponse will contain a field named `accessKey` - this is required for all\n`PUT` and `DELETE` operations. Without the access key the paste cannot be\nupdated or deleted prematurely.\n\n`PUT`, `GET` and `DELETE` http methods for a paste will use the URL:\n`/{uuid}` which will find the correct paste based on its unique identifier. But\n`POST` uses the URL: `/`. All details of the paste are passed in the request\nbody. Accepted fields are:\n\n```\n{\n    \"content\":      []String,\n    \"filetype\":     String,\n    \"expiresIn\":    Int,\n    \"accessKey\":    String,\n}\n```\n\n## URLS + Requests\n\nThe paste-server instance will expose the following urls:\n - `/api/new`\n - `/api/{uuid}`\n - `/{uuid}`\n - `/{uuid}/raw`\n - `/`\n\nThe `/api` routes are used by the [paste-cli](https://github.com/h5law/paste-cli)\ntool to preform CRUD operations and can be used to send HTTP requests to\ninteract with the instance without the paste-cli tool.\n\n- `POST /api/new`\n  - Requires JSON body containing at least `content` field of an array of\nstrings, a file split at new-lines\n  - Optionally can include `filetype`, and `expiresIn` fields which default to\n`plaintext` and `14` respectively\n  - Returns a JSON object containing the `accessKey`, `expiresAt`, and `uuid`\nfields\n- `GET /api/{uuid}`\n  - Returns JSON object containing the `content`, `filetype` and `expiresAt`\nfields\n- `UPDATE /api/{uuid}`\n  - Requires JSON body containing any changes to `content`, `filetype`, or a\nnew `expiresIn` value as well as the `accessKey` field\n  - By default the paste will be updated to expire in another 14 days after the\nupdate so use `expriesIn` with any changes made to ensure a longer or shorter\nlife\n- `DELETE /api/{uuid}`\n  - Requires the JSON body containing only the `accessKey` field\n  - Returns a message confirming the pastes deletion\n\nThe `/{uuid}` route (when no directory for a built frontend SPA has been\nprovided with the `--spa-dir` flag of the start command) will make use of a\nsimple plaintext site that neatly prints the contents of `GET /api/{uuid}` and\nall its fields. `/{uuid}/raw` does the same but only shows the content field -\nthis works whether `--spa-dir` is given or not.\n\nWith the `--spa-dir` flag set `/` will serve the Preact\n[SPA](https://github.com/h5law/paste-site) built in the directory given by the\n`--spa-dir` flag and will allow for new pastes to be created through the\nhomepage of the site. It also handles the `/{uuid}` route and allows for\nupdates and deletes to be made through the front end site. To see more about\nthe Preact SPA check out [this repo](https://github.com/h5law/paste-site)\n","funding_links":[],"categories":["golang"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh5law%2Fpaste-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fh5law%2Fpaste-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh5law%2Fpaste-server/lists"}