Code Monkey home page Code Monkey logo

paste's Introduction

Paste

A simple pastebin service. Running at paste.fascinated.cc.

Environment Variables

Name Description Default
MONGO_URI The MongoDB URI mongodb://localhost:27017
PASTE_ID_LENGTH The length of the paste ID 12
MAX_PASTE_LENGTH The maximum length of a paste 5000000
ENABLE_METRICS Whether to enable Prometheus metrics false
TEXTBOX_PLACEHOLDER The placeholder text for the textbox Enter your text here...
HASTEBIN_COMPATIBILITY_URL The URL to use for Hastebin compatibility /documents
SITE_TITLE The title of the site Paste
ENABLE_LOGGING Whether to enable logging true

Javascript Utility

/**
 * Uploads a paste to paste.fascinated.cc
 *
 * @param content the content of the paste
 * @returns the paste key and the URL
 */
async function uploadPaste(content: string) {
  const response = await fetch("https://paste.fascinated.cc/api/upload", {
    method: "POST",
    body: content,
  });
  const json = await response.json();

  if (!response.ok) {
    throw new Error(json.message);
  }

  return {
    ...json,
    url: `https://paste.fascinated.cc/${json.key}`,
  };
}

console.log(await uploadPaste("Hello, World!"));

Go Utility

package main

import (
 "bytes"
 "encoding/json"
 "fmt"
 "io"
 "net/http"
)

type PasteResponse struct {
 Key string `json:"key"`
 URL string `json:"url"`
}

func uploadPaste(content string) (*PasteResponse, error) {
 url := "https://paste.fascinated.cc/api/upload"
 req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(content)))
 if err != nil {
  return nil, err
 }
 req.Header.Set("Content-Type", "text/plain")

 resp, err := (&http.Client{}).Do(req)
 if err != nil {
  return nil, err
 }
 defer resp.Body.Close()

 body, err := io.ReadAll(resp.Body)
 if err != nil {
  return nil, err
 }
 if resp.StatusCode != http.StatusOK {
  return nil, err
 }

 var pasteResponse PasteResponse
 if err := json.Unmarshal(body, &pasteResponse); err != nil {
  return nil, err
 }
 pasteResponse.URL = "https://paste.fascinated.cc/" + pasteResponse.Key
 return &pasteResponse, nil
}

func main() {
 content := "Hello, World!"
 if response, err := uploadPaste(content); err != nil {
  fmt.Println("Error:", err)
 } else {
  fmt.Println("URL:", response.URL)
 }
}

paste's People

Contributors

realfascinated avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.