Skip to content
Permalink
Browse files

Initial Commit

  • Loading branch information...
Depado committed Sep 30, 2015
0 parents commit f8e5d57fb60547128ad5cc205f3837e42ac86cec
@@ -0,0 +1,2 @@
gopaste
my.db

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

@@ -0,0 +1 @@
port: 9090
@@ -0,0 +1,28 @@
package configuration

import (
"io/ioutil"

"gopkg.in/yaml.v2"
)

// Configuration is a struct representing a single configuration.
type Configuration struct {
Port string
}

// Config represents the actual configuration.
var Config = new(Configuration)

// Load parses the yml file passed as argument and fills the Config.
func Load(file string) error {
conf, err := ioutil.ReadFile(file)
if err != nil {
return err
}
err = yaml.Unmarshal(conf, &Config)
if err != nil {
return err
}
return nil
}
114 main.go
@@ -0,0 +1,114 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"text/template"

"github.com/Depado/gopaste/configuration"
"github.com/GeertJohan/go.rice"
"github.com/goji/param"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"github.com/nu7hatch/gouuid"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)

var templateBox = rice.MustFindBox("templates")
var staticBox = rice.MustFindBox("assets")
var db gorm.DB

// Paste is a paste
type Paste struct {
Title string
Content string
Markdown bool
}

// PasteEntry is the db model
type PasteEntry struct {
gorm.Model
Paste
Key string
}

func loadTemplate(path string) (*template.Template, error) {
templateString, err := templateBox.String(path)
if err != nil {
return nil, err
}
t, err := template.New(path).Parse(templateString)
if err != nil {
return nil, err
}
return t, nil
}

func index(c web.C, w http.ResponseWriter, r *http.Request) {
var err error

if r.Method == "POST" {
err = r.ParseForm()
if err != nil {
http.Error(w, "No good!", 400)
return
}
p := Paste{}
err = param.Parse(r.PostForm, &p)
if err != nil {
http.Error(w, "You're doing it wrong. In some way at least.", 500)
return
}
pe := PasteEntry{}
pe.Paste = p
u, err := uuid.NewV4()
if err != nil {
http.Error(w, "Something went wrong.", 500)
}
pe.Key = u.String()
db.Create(&pe)
http.Redirect(w, r, fmt.Sprintf("/paste/%s", pe.Key), http.StatusFound)
return
}

t, err := loadTemplate("index.html")
if err != nil {
log.Fatal(err)
}
t.Execute(w, c.Env)
}

func getPaste(c web.C, w http.ResponseWriter, r *http.Request) {
pe := PasteEntry{}
db.Where(&PasteEntry{Key: c.URLParams["key"]}).First(&pe)
if pe.Key == "" {
http.Error(w, "Not found", 404)
}
fmt.Fprint(w, pe.Content)
}

func main() {
var err error

err = configuration.Load("conf.yml")
if err != nil {
log.Fatal(err)
}

db, err = gorm.Open("sqlite3", "my.db")
if err != nil {
log.Fatal(err)
}
db.AutoMigrate(&PasteEntry{})

goji.Get("/paste/:key", getPaste)
goji.Get("/static/*", http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox())))
goji.Get("/", index)
goji.Post("/", index)

flag.Set("bind", ":"+configuration.Config.Port)
goji.Serve()
}
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>GoPaste</title>
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="/static/css/materialize.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style media="screen">
body {
background: url(/static/img/background.png) repeat;
display: flex;
min-height: 100vh;
flex-direction: column;
}
main {
flex: 1 0 auto;
}
</style>
</head>

<body>
<main>
<div class="container">
<div class="row">
<div class="col s12">
<div class="card">
<div class="card-content">
<span class="card-title black-text">Create a new Paste</span>
<form action="/" method="post">
<!-- <div class="row">
<div class="input-field col s12">
<input id="title" name="Title" type="text" class="validate">
<label for="title">Title</label>
</div>
</div> -->
<div class="row">
<div class="input-field col s12">
<textarea id="content" name="Content" class="materialize-textarea"></textarea>
<label for="content">Content</label>
</div>
</div>
<!-- <div class="row">
<div class="input-field col s12">
<input type="checkbox" name="Markdown" id="markdown" />
<label for="markdown">Markdown</label>
</div>
</div> -->
<div class="row">
<div class="input-field col s12 center-align">
<button class="btn waves-effect waves-light" type="submit">Submit
<i class="material-icons right">send</i>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main>

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="/static/js/materialize.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.modal-trigger').leanModal();
});
</script>
</body>
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>GoPaste</title>
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="/static/css/materialize.min.css">
</head>

<body>
{{.content}}
</body>

0 comments on commit f8e5d57

Please sign in to comment.
You can’t perform that action at this time.