Skip to content

Commit

Permalink
Merge pull request #231 from spacebin-org/develop
Browse files Browse the repository at this point in the history
v0.1.5a: update routes, regex, and copyright year
  • Loading branch information
jackdorland authored Apr 19, 2021
2 parents fd7da44 + 19d61d8 commit d6d3ac9
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 169 deletions.
2 changes: 1 addition & 1 deletion cmd/spirit/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion internal/app/router.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
8 changes: 4 additions & 4 deletions internal/app/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@ package app
import (
"github.com/gofiber/fiber/v2"
"github.com/spacebin-org/spirit/internal/pkg/config"
"github.com/spacebin-org/spirit/internal/pkg/document"
"github.com/spacebin-org/spirit/internal/pkg/domain"
)

// Start initializes the server
Expand All @@ -39,9 +39,9 @@ func Start() *fiber.App {
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)

// Return statuscode with error message
return c.Status(code).JSON(&document.Response{
return c.Status(code).JSON(&domain.Response{
Error: err.Error(),
Payload: document.Payload{},
Payload: domain.Payload{},
Status: code,
})
},
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/database/database.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/database/models/document.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/document/controller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
65 changes: 0 additions & 65 deletions internal/pkg/document/create.go

This file was deleted.

66 changes: 0 additions & 66 deletions internal/pkg/document/read.go

This file was deleted.

110 changes: 89 additions & 21 deletions internal/pkg/document/routes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,30 +16,98 @@

package document

import "github.com/gofiber/fiber/v2"

// Payload is a document object
type Payload struct {
ContentHash string `json:"content_hash,omitempty"` // A base64 representation form of the document's content.
ID *string `json:"id,omitempty"` // The document ID.
Content *string `json:"content,omitempty"` // The document content.
Extension *string `json:"extension,omitempty"` // The extension of the document.
CreatedAt *int64 `json:"created_at,omitempty"` // The Unix timestamp of when the document was inserted.
UpdatedAt *int64 `json:"updated_at,omitempty"` // The Unix timestamp of when the document was last modified.
Exists *bool `json:"exists,omitempty"` // Whether the document does or does not exist.
}
import (
"crypto/md5"
"encoding/hex"

// Response is a Spacebin API response
type Response struct {
Error string `json:"error"` // .Error() should already be called
Payload Payload `json:"payload"`
Status int `json:"status"`
}
"github.com/gofiber/fiber/v2"
"github.com/spacebin-org/spirit/internal/pkg/config"
"github.com/spacebin-org/spirit/internal/pkg/domain"
)

// Register loads all document-related endpoints
func Register(app *fiber.App) {
api := app.Group("/v1/documents")

registerCreate(api)
registerRead(api)
api.Post("/", func(c *fiber.Ctx) error {
b := new(CreateRequest)

// Validate and parse body
if err := c.BodyParser(b); err != nil {
return fiber.NewError(400, err.Error())
}

if err := b.Validate(); err != nil {
return fiber.NewError(400, err.Error())
}

// Create and retrieve document
id, err := NewDocument(b.Content, b.Extension)

if err != nil {
return fiber.NewError(500, err.Error())
}

document, err := GetDocument(id)

if err != nil {
return fiber.NewError(500, err.Error())
}

hash := md5.Sum([]byte(document.Content))

c.Status(201).JSON(&domain.Response{
Status: c.Response().StatusCode(),
Payload: domain.Payload{
ID: &document.ID,
ContentHash: hex.EncodeToString(hash[:]),
},
Error: "",
})

return nil
})

api.Get("/:id", func(c *fiber.Ctx) error {
if c.Params("id") != "" && len(c.Params("id")) == config.Config.Documents.IDLength {
document, err := GetDocument(c.Params("id"))

if err != nil {
return fiber.NewError(404, err.Error())
}

c.Status(200).JSON(&domain.Response{
Status: c.Response().StatusCode(),
Payload: domain.Payload{
ID: &document.ID,
Content: &document.Content,
Extension: &document.Extension,
CreatedAt: &document.CreatedAt,
UpdatedAt: &document.UpdatedAt,
},
Error: "",
})
} else {
return fiber.NewError(400)
}

return nil
})

api.Get("/:id/raw", func(c *fiber.Ctx) (err error) {
if c.Params("id") != "" && len(c.Params("id")) == config.Config.Documents.IDLength {
document, err := GetDocument(c.Params("id"))

if err != nil {
return fiber.NewError(404, err.Error())
}

c.Status(200).SendString(document.Content)
} else {
return fiber.NewError(400)
}

return nil
})

}
11 changes: 5 additions & 6 deletions internal/pkg/document/validate.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,14 +35,13 @@ func (c CreateRequest) Validate() error {
* This regex matches the file extension for various languages.
* Languages including:
* Python, JavaScript(React), TypeScript(React), Go, Kotlin,
* C++, SQL, C-Sharp, C (including `.h`), Scala, Haskell,
* Shell, PowerShell, PHP, Assembly, Julia, Objective-C (.m),
* Perl, Crystal, JSON, YAML, TOML, and Plain Text
* python, javascript, jsx, typescript, tsx, go, kotlin, cpp, sql, csharp,
* c, scala, haskell, shell-session, bash, powershell, php, asm6502, julia,
* objc, perl, crystal, json, yaml, toml, none, rust, ruby.
* For any unsupported formats Plain Text should be used.
*/
regex := regexp.MustCompile("^py$|^[tj](sx|s)$|^go$|^kt$|^java$|^c$|^c(pp|[rs])$|^sql$|^swift$|^dart$|^r$|^r[sb]$|^h$|^scala$|^hs$|^sh$|^p(s1|hp)$|^asm$|^jl$|^m$|^txt$|^pl$|^(x|ya|to)ml$")
regex := regexp.MustCompile("^python$|^javascript$|^jsx$|^typescript$|^tsx$|^go$|^kotlin$|^cpp$|^sql$|^csharp$|^c$|^scala$|^haskell$|^shell-session$|^bash$|^powershell$|^php$|^asm6502$|^julia$|^objc$|^perl$|^crystal$|^json$|^yaml$|^toml$|^none$|^rust$|^ruby$")

return validation.ValidateStruct(&c,
validation.Field(
Expand Down
35 changes: 35 additions & 0 deletions internal/pkg/domain/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package domain

// Payload is a document object
type Payload struct {
ContentHash string `json:"content_hash,omitempty"` // A base64 representation form of the document's content.
ID *string `json:"id,omitempty"` // The document ID.
Content *string `json:"content,omitempty"` // The document content.
Extension *string `json:"extension,omitempty"` // The extension of the document.
CreatedAt *int64 `json:"created_at,omitempty"` // The Unix timestamp of when the document was inserted.
UpdatedAt *int64 `json:"updated_at,omitempty"` // The Unix timestamp of when the document was last modified.
Exists *bool `json:"exists,omitempty"` // Whether the document does or does not exist.
}

// Response is a Spacebin API response
type Response struct {
Error string `json:"error"` // .Error() should already be called
Payload Payload `json:"payload"`
Status int `json:"status"`
}
2 changes: 1 addition & 1 deletion magefile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// +build mage

/*
* Copyright 2020 Luke Whrit, Jack Dorland; The Spacebin Authors
* Copyright 2020-2021 Luke Whrit, Jack Dorland
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit d6d3ac9

Please sign in to comment.