Commit e80b843d authored by Artemis's avatar Artemis 🐱

Moved data layer to separate module, converted existing code to use proper structure

parent 13d81a77
package main
import (
"github.com/go-redis/redis"
"github.com/spf13/viper"
)
func GetBackend() (error, *redis.Client) {
backend := redis.NewClient(&redis.Options{
Addr: viper.GetString("RedisAddress"),
Password: viper.GetString("RedisPassword"),
DB: viper.GetInt("RedisDatabase"),
})
res := backend.Ping()
if err := res.Err(); err != nil {
return err, nil
}
return nil, backend
}
......@@ -2,16 +2,16 @@ package main
import (
"fmt"
"github.com/go-redis/redis"
"github.com/husobee/vestigo"
"gitlab.com/Artemix/paste/storage"
"html/template"
"net/http"
"time"
)
type Handlers struct {
Backend *redis.Client
Templates *template.Template
BackendService *storage.BackendService
Templates *template.Template
}
type Content struct {
......@@ -93,8 +93,10 @@ func (h Handlers) HandleNewPaste(w http.ResponseWriter, req *http.Request) {
return
}
res := h.Backend.Set(key, pasteValue, keepaliveTime)
if err := res.Err(); err != nil {
err = h.BackendService.
Ephemeral().
Persist(key, pasteValue, &storage.PersistOptions{Expiration: keepaliveTime})
if err != nil {
w.WriteHeader(500)
fmt.Println(err)
_ = h.Templates.ExecuteTemplate(w, "homepage", &Content{Error: "Failed to store the value", Value: pasteValue})
......@@ -108,20 +110,20 @@ func (h Handlers) HandleNewPaste(w http.ResponseWriter, req *http.Request) {
func (h Handlers) HandleShowPaste(w http.ResponseWriter, req *http.Request) {
key := vestigo.Param(req, "key")
res := h.Backend.Get(key)
if err := res.Err(); err != nil {
if err == redis.Nil {
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
} else {
w.WriteHeader(500)
fmt.Println(err)
}
res, err := h.BackendService.Ephemeral().Retrieve(key)
if err != nil {
// Crash
w.WriteHeader(500)
fmt.Println(err)
return
} else if res == "" {
// 404
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
}
err := h.Templates.ExecuteTemplate(w, "show", &Content{
Value: res.Val(),
err = h.Templates.ExecuteTemplate(w, "show", &Content{
Value: res,
Raw: "/r/" + key,
})
if err != nil {
......@@ -132,22 +134,22 @@ func (h Handlers) HandleShowPaste(w http.ResponseWriter, req *http.Request) {
func (h Handlers) HandleShowRaw(w http.ResponseWriter, req *http.Request) {
key := vestigo.Param(req, "key")
res := h.Backend.Get(key)
if err := res.Err(); err != nil {
if err == redis.Nil {
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
} else {
w.WriteHeader(500)
fmt.Println(err)
}
res, err := h.BackendService.Ephemeral().Retrieve(key)
if err != nil {
// Crash
w.WriteHeader(500)
fmt.Println(err)
return
} else if res == "" {
// 404
w.WriteHeader(404)
_, _ = fmt.Fprintln(w, NotFoundErr)
}
w.Header().Set("Content-Type", "text/plain; charset=utf8")
_, _ = fmt.Fprint(w, res.Val())
_, _ = fmt.Fprint(w, res)
}
func (h Handlers) HandleStaticContent(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "./static/" + vestigo.Param(req, "_name"))
http.ServeFile(w, req, "./static/"+vestigo.Param(req, "_name"))
}
......@@ -2,23 +2,35 @@ package main
import (
"fmt"
"github.com/go-redis/redis"
"github.com/spf13/viper"
"gitlab.com/Artemix/paste/storage"
"log"
"net/http"
"time"
)
func main() {
_ = SetupConfig()
err := SetupConfig()
if err != nil {
log.Println(err)
}
err, backend := GetBackend()
redisBackend := &storage.RedisBackend{}
err = redisBackend.Init(&redis.Options{
Addr: viper.GetString("RedisAddress"),
Password: viper.GetString("RedisPassword"),
DB: viper.GetInt("RedisDatabase"),
})
if err != nil {
panic(err)
log.Fatal(err)
}
handlers := &Handlers{
Templates: InitTemplates(),
Backend: backend,
BackendService: &storage.BackendService{
RedisBackend: redisBackend,
},
}
host := viper.GetString("HttpHost")
......
package storage
import "time"
// region Public Backend interface
type Backend interface {
// Tries to store the paste at the given key, with the given expiration
// If error isn't nil, there was a fatal error on redis querying
Persist(key string, value string, options *PersistOptions) error
// Tries to retrieve a paste at the given key
// If string is not empty, the paste is found
// If string is empty but error is nil, the paste was not found but the request succeeded
// If string is empty and error isn't nil, there was a fatal error on redis querying
Retrieve(key string) (string, error)
}
type PersistOptions struct {
Expiration time.Duration
}
// endregion
// region Public BackendService interface
type BackendService struct {
RedisBackend Backend
}
func (b BackendService) Ephemeral() Backend {
return b.RedisBackend
}
func (b BackendService) Persistent() Backend {
panic("implement me")
}
// endregion
package storage
import (
"github.com/go-redis/redis"
)
type RedisBackend struct {
Client *redis.Client
}
func (r *RedisBackend) Persist(key string, value string, options *PersistOptions) error {
return r.Client.
Set(key, value, options.Expiration).
Err()
}
func (r *RedisBackend) Retrieve(key string) (string, error) {
res := r.Client.Get(key)
if err := res.Err(); err != nil {
if err == redis.Nil {
// Not found
return "", nil
} else {
// Crash
return "", err
}
}
// Found
return res.Val(), nil
}
func (r *RedisBackend) Init(opts *redis.Options) error {
r.Client = redis.NewClient(opts)
res := r.Client.Ping()
return res.Err()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment