Compare commits
	
		
			6 Commits
		
	
	
		
			v0.2.0
			...
			e3ff8065f1
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e3ff8065f1 | |||
| 9449b37ab1 | |||
| 1cb169318c | |||
| 94e1920098 | |||
| 41e82fb21e | |||
| d8817cc67f | 
@@ -78,10 +78,11 @@ func ActionUpload(c *cli.Context) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	mw.Close()
 | 
						mw.Close()
 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
 | 
						req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
 | 
				
			||||||
	req.Header.Add("Content-Type", mw.FormDataContentType())
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						req.Header.Add("Content-Type", mw.FormDataContentType())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	resp, err := client.Do(req)
 | 
						resp, err := client.Do(req)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -57,6 +57,7 @@ func ActionServe(c *cli.Context) error {
 | 
				
			|||||||
	// Setup loggers
 | 
						// Setup loggers
 | 
				
			||||||
	rootLogger := getRootLogger(cfg.LogLevel)
 | 
						rootLogger := getRootLogger(cfg.LogLevel)
 | 
				
			||||||
	serverLogger := rootLogger.Named("SERV")
 | 
						serverLogger := rootLogger.Named("SERV")
 | 
				
			||||||
 | 
						accessLogger := rootLogger.Named("ACCS")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Setup contexts for clean shutdown
 | 
						// Setup contexts for clean shutdown
 | 
				
			||||||
	rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt)
 | 
						rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt)
 | 
				
			||||||
@@ -70,6 +71,7 @@ func ActionServe(c *cli.Context) error {
 | 
				
			|||||||
		srv := gpaste.NewHTTPServer(cfg)
 | 
							srv := gpaste.NewHTTPServer(cfg)
 | 
				
			||||||
		srv.Addr = cfg.ListenAddr
 | 
							srv.Addr = cfg.ListenAddr
 | 
				
			||||||
		srv.Logger = serverLogger
 | 
							srv.Logger = serverLogger
 | 
				
			||||||
 | 
							srv.AccessLogger = accessLogger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Wait for cancel
 | 
							// Wait for cancel
 | 
				
			||||||
		go func() {
 | 
							go func() {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										39
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								config.go
									
									
									
									
									
								
							@@ -3,6 +3,8 @@ package gpaste
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/pelletier/go-toml"
 | 
						"github.com/pelletier/go-toml"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -15,15 +17,48 @@ type ServerConfig struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ServerStoreConfig struct {
 | 
					type ServerStoreConfig struct {
 | 
				
			||||||
	Type string `toml:"Type"`
 | 
						Type string                    `toml:"Type"`
 | 
				
			||||||
 | 
						FS   *ServerStoreFSStoreConfig `toml:"FS"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ServerStoreFSStoreConfig struct {
 | 
				
			||||||
 | 
						Dir string `toml:"Dir"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ServerConfigFromReader(r io.Reader) (*ServerConfig, error) {
 | 
					func ServerConfigFromReader(r io.Reader) (*ServerConfig, error) {
 | 
				
			||||||
	decoder := toml.NewDecoder(r)
 | 
						decoder := toml.NewDecoder(r)
 | 
				
			||||||
	var c ServerConfig
 | 
						c := ServerConfig{
 | 
				
			||||||
 | 
							Store: &ServerStoreConfig{
 | 
				
			||||||
 | 
								FS: &ServerStoreFSStoreConfig{},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if err := decoder.Decode(&c); err != nil {
 | 
						if err := decoder.Decode(&c); err != nil {
 | 
				
			||||||
		return nil, fmt.Errorf("error decoding server config: %w", err)
 | 
							return nil, fmt.Errorf("error decoding server config: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						c.updateFromEnv()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &c, nil
 | 
						return &c, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (sc *ServerConfig) updateFromEnv() {
 | 
				
			||||||
 | 
						if value, ok := os.LookupEnv("GPASTE_LOGLEVEL"); ok {
 | 
				
			||||||
 | 
							sc.LogLevel = strings.ToUpper(value)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if value, ok := os.LookupEnv("GPASTE_URL"); ok {
 | 
				
			||||||
 | 
							sc.URL = value
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if value, ok := os.LookupEnv("GPASTE_LISTENADDR"); ok {
 | 
				
			||||||
 | 
							sc.ListenAddr = value
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if value, ok := os.LookupEnv("GPASTE_STORE_TYPE"); ok {
 | 
				
			||||||
 | 
							sc.Store.Type = value
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if value, ok := os.LookupEnv("GPASTE_STORE_FS_DIR"); ok {
 | 
				
			||||||
 | 
							sc.Store.FS.Dir = value
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										93
									
								
								config_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								config_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,93 @@
 | 
				
			|||||||
 | 
					package gpaste_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"git.t-juice.club/torjus/gpaste"
 | 
				
			||||||
 | 
						"github.com/google/go-cmp/cmp"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestServerConfig(t *testing.T) {
 | 
				
			||||||
 | 
						t.Run("FromReader", func(t *testing.T) {
 | 
				
			||||||
 | 
							clearEnv()
 | 
				
			||||||
 | 
							simpleConfig := `
 | 
				
			||||||
 | 
					LogLevel = "INFO"
 | 
				
			||||||
 | 
					URL = "http://paste.example.org"
 | 
				
			||||||
 | 
					ListenAddr = ":8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[Store]
 | 
				
			||||||
 | 
					Type = "fs"
 | 
				
			||||||
 | 
					[Store.FS]
 | 
				
			||||||
 | 
					Dir = "/tmp"
 | 
				
			||||||
 | 
					`
 | 
				
			||||||
 | 
							expected := &gpaste.ServerConfig{
 | 
				
			||||||
 | 
								LogLevel:   "INFO",
 | 
				
			||||||
 | 
								URL:        "http://paste.example.org",
 | 
				
			||||||
 | 
								ListenAddr: ":8080",
 | 
				
			||||||
 | 
								Store: &gpaste.ServerStoreConfig{
 | 
				
			||||||
 | 
									Type: "fs",
 | 
				
			||||||
 | 
									FS: &gpaste.ServerStoreFSStoreConfig{
 | 
				
			||||||
 | 
										Dir: "/tmp",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							sr := strings.NewReader(simpleConfig)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							c, err := gpaste.ServerConfigFromReader(sr)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error parsing config: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if !cmp.Equal(c, expected) {
 | 
				
			||||||
 | 
								t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						t.Run("FromEnv", func(t *testing.T) {
 | 
				
			||||||
 | 
							clearEnv()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var envMap map[string]string = map[string]string{
 | 
				
			||||||
 | 
								"GPASTE_LOGLEVEL":     "DEBUG",
 | 
				
			||||||
 | 
								"GPASTE_URL":          "http://gpaste.example.org",
 | 
				
			||||||
 | 
								"GPASTE_STORE_TYPE":   "fs",
 | 
				
			||||||
 | 
								"GPASTE_LISTENADDR":   ":8000",
 | 
				
			||||||
 | 
								"GPASTE_STORE_FS_DIR": "/tmp",
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							expected := &gpaste.ServerConfig{
 | 
				
			||||||
 | 
								LogLevel:   "DEBUG",
 | 
				
			||||||
 | 
								URL:        "http://gpaste.example.org",
 | 
				
			||||||
 | 
								ListenAddr: ":8000",
 | 
				
			||||||
 | 
								Store: &gpaste.ServerStoreConfig{
 | 
				
			||||||
 | 
									Type: "fs",
 | 
				
			||||||
 | 
									FS: &gpaste.ServerStoreFSStoreConfig{
 | 
				
			||||||
 | 
										Dir: "/tmp",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for k, v := range envMap {
 | 
				
			||||||
 | 
								os.Setenv(k, v)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							sr := strings.NewReader("")
 | 
				
			||||||
 | 
							c, err := gpaste.ServerConfigFromReader(sr)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error parsing empty config")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if !cmp.Equal(c, expected) {
 | 
				
			||||||
 | 
								t.Errorf("Result does not match: %s", cmp.Diff(c, expected))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func clearEnv() {
 | 
				
			||||||
 | 
						for _, env := range os.Environ() {
 | 
				
			||||||
 | 
							result := strings.Split(env, "=")
 | 
				
			||||||
 | 
							value := result[0]
 | 
				
			||||||
 | 
							if strings.Contains(value, "GPASTE_") {
 | 
				
			||||||
 | 
								os.Unsetenv(value)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										10
									
								
								filestore.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								filestore.go
									
									
									
									
									
								
							@@ -6,12 +6,12 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type File struct {
 | 
					type File struct {
 | 
				
			||||||
	ID               string
 | 
						ID               string    `json:"id"`
 | 
				
			||||||
	OriginalFilename string
 | 
						OriginalFilename string    `json:"original_filename"`
 | 
				
			||||||
	Body             io.ReadCloser
 | 
						MaxViews         uint      `json:"max_views"`
 | 
				
			||||||
 | 
						ExpiresOn        time.Time `json:"expires_on"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	MaxViews  uint
 | 
						Body io.ReadCloser
 | 
				
			||||||
	ExpiresOn time.Time
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type FileStore interface {
 | 
					type FileStore interface {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										115
									
								
								filestore_fs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								filestore_fs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,115 @@
 | 
				
			|||||||
 | 
					package gpaste
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type FSFileStore struct {
 | 
				
			||||||
 | 
						dir      string
 | 
				
			||||||
 | 
						metadata map[string]*File
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewFSFileStore(dir string) (*FSFileStore, error) {
 | 
				
			||||||
 | 
						s := &FSFileStore{
 | 
				
			||||||
 | 
							dir:      dir,
 | 
				
			||||||
 | 
							metadata: make(map[string]*File),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := s.readMetadata()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return s, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (s *FSFileStore) Store(f *File) error {
 | 
				
			||||||
 | 
						defer f.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						metadata := &File{
 | 
				
			||||||
 | 
							ID:               f.ID,
 | 
				
			||||||
 | 
							OriginalFilename: f.OriginalFilename,
 | 
				
			||||||
 | 
							MaxViews:         f.MaxViews,
 | 
				
			||||||
 | 
							ExpiresOn:        f.ExpiresOn,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						path := filepath.Join(s.dir, f.ID)
 | 
				
			||||||
 | 
						dst, err := os.Create(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer dst.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if _, err := io.Copy(dst, f.Body); err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						s.metadata[f.ID] = metadata
 | 
				
			||||||
 | 
						if err := s.writeMetadata(); err != nil {
 | 
				
			||||||
 | 
							delete(s.metadata, f.ID)
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *FSFileStore) Get(id string) (*File, error) {
 | 
				
			||||||
 | 
						metadata, ok := s.metadata[id]
 | 
				
			||||||
 | 
						if !ok {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("no such item")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						path := filepath.Join(s.dir, id)
 | 
				
			||||||
 | 
						f, err := os.Open(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						metadata.Body = f
 | 
				
			||||||
 | 
						return metadata, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *FSFileStore) Delete(id string) error {
 | 
				
			||||||
 | 
						path := filepath.Join(s.dir, id)
 | 
				
			||||||
 | 
						if err := os.Remove(path); err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						delete(s.metadata, id)
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *FSFileStore) List() ([]string, error) {
 | 
				
			||||||
 | 
						var results []string
 | 
				
			||||||
 | 
						for k := range s.metadata {
 | 
				
			||||||
 | 
							results = append(results, k)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return results, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *FSFileStore) writeMetadata() error {
 | 
				
			||||||
 | 
						path := filepath.Join(s.dir, "metadata.json")
 | 
				
			||||||
 | 
						f, err := os.Create(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer f.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						encoder := json.NewEncoder(f)
 | 
				
			||||||
 | 
						if err := encoder.Encode(s.metadata); err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *FSFileStore) readMetadata() error {
 | 
				
			||||||
 | 
						path := filepath.Join(s.dir, "metadata.json")
 | 
				
			||||||
 | 
						f, err := os.Open(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							// TODO: Handle errors better
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer f.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						decoder := json.NewDecoder(f)
 | 
				
			||||||
 | 
						if err := decoder.Decode(&s.metadata); err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										26
									
								
								filestore_fs_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								filestore_fs_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					package gpaste_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"git.t-juice.club/torjus/gpaste"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestFSFileStore(t *testing.T) {
 | 
				
			||||||
 | 
						dir := t.TempDir()
 | 
				
			||||||
 | 
						s, err := gpaste.NewFSFileStore(dir)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("Error creating store: %s", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						RunFilestoreTest(s, t)
 | 
				
			||||||
 | 
						persistentDir := t.TempDir()
 | 
				
			||||||
 | 
						newFunc := func() gpaste.FileStore {
 | 
				
			||||||
 | 
							s, err := gpaste.NewFSFileStore(persistentDir)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error creating store: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return s
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						RunPersistentFilestoreTest(newFunc, t)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -3,9 +3,12 @@ package gpaste_test
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.t-juice.club/torjus/gpaste"
 | 
						"git.t-juice.club/torjus/gpaste"
 | 
				
			||||||
 | 
						"github.com/google/go-cmp/cmp"
 | 
				
			||||||
	"github.com/google/uuid"
 | 
						"github.com/google/uuid"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -74,3 +77,81 @@ func RunFilestoreTest(s gpaste.FileStore, t *testing.T) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func RunPersistentFilestoreTest(newStoreFunc func() gpaste.FileStore, t *testing.T) {
 | 
				
			||||||
 | 
						s := newStoreFunc()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						files := []struct {
 | 
				
			||||||
 | 
							File         *gpaste.File
 | 
				
			||||||
 | 
							ExpectedData string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								File: &gpaste.File{
 | 
				
			||||||
 | 
									ID:               uuid.NewString(),
 | 
				
			||||||
 | 
									OriginalFilename: "testfile.txt",
 | 
				
			||||||
 | 
									MaxViews:         5,
 | 
				
			||||||
 | 
									ExpiresOn:        time.Now().Add(10 * time.Minute),
 | 
				
			||||||
 | 
									Body:             io.NopCloser(strings.NewReader("cocks!")),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								ExpectedData: "cocks!",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								File: &gpaste.File{
 | 
				
			||||||
 | 
									ID:               uuid.NewString(),
 | 
				
			||||||
 | 
									OriginalFilename: "testfile2.txt",
 | 
				
			||||||
 | 
									MaxViews:         5,
 | 
				
			||||||
 | 
									ExpiresOn:        time.Now().Add(10 * time.Minute),
 | 
				
			||||||
 | 
									Body:             io.NopCloser(strings.NewReader("derps!")),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								ExpectedData: "derps!",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, f := range files {
 | 
				
			||||||
 | 
							err := s.Store(f.File)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error storing file: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, f := range files {
 | 
				
			||||||
 | 
							retrieved, err := s.Get(f.File.ID)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Unable to retrieve file: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
 | 
				
			||||||
 | 
							if !cmp.Equal(retrieved, f.File, ignoreBody) {
 | 
				
			||||||
 | 
								t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							buf := new(strings.Builder)
 | 
				
			||||||
 | 
							if _, err := io.Copy(buf, retrieved.Body); err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error reading from body: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							retrieved.Body.Close()
 | 
				
			||||||
 | 
							if buf.String() != f.ExpectedData {
 | 
				
			||||||
 | 
								t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Reopen store, and fetch again
 | 
				
			||||||
 | 
						s = newStoreFunc()
 | 
				
			||||||
 | 
						for _, f := range files {
 | 
				
			||||||
 | 
							retrieved, err := s.Get(f.File.ID)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Unable to retrieve file: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
 | 
				
			||||||
 | 
							if !cmp.Equal(retrieved, f.File, ignoreBody) {
 | 
				
			||||||
 | 
								t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							buf := new(strings.Builder)
 | 
				
			||||||
 | 
							if _, err := io.Copy(buf, retrieved.Body); err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error reading from body: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							retrieved.Body.Close()
 | 
				
			||||||
 | 
							if buf.String() != f.ExpectedData {
 | 
				
			||||||
 | 
								t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -7,6 +7,7 @@ require github.com/google/uuid v1.3.0
 | 
				
			|||||||
require github.com/go-chi/chi/v5 v5.0.7
 | 
					require github.com/go-chi/chi/v5 v5.0.7
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
 | 
						github.com/google/go-cmp v0.5.6
 | 
				
			||||||
	github.com/pelletier/go-toml v1.9.4
 | 
						github.com/pelletier/go-toml v1.9.4
 | 
				
			||||||
	github.com/urfave/cli/v2 v2.3.0
 | 
						github.com/urfave/cli/v2 v2.3.0
 | 
				
			||||||
	go.uber.org/zap v1.20.0
 | 
						go.uber.org/zap v1.20.0
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
 | 
				
			|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
					github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
				
			||||||
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
 | 
					github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
 | 
				
			||||||
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
 | 
					github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
 | 
				
			||||||
 | 
					github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
 | 
				
			||||||
 | 
					github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 | 
				
			||||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
 | 
					github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
 | 
				
			||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 | 
					github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 | 
				
			||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 | 
					github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 | 
				
			||||||
@@ -65,6 +67,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
 | 
				
			|||||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
 | 
					golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
 | 
				
			||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
					golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
				
			||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
					golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
				
			||||||
 | 
					golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
				
			||||||
 | 
					golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
 | 
				
			||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
					golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
				
			||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
					gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
				
			||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
					gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										38
									
								
								http.go
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								http.go
									
									
									
									
									
								
							@@ -7,25 +7,31 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/go-chi/chi/v5"
 | 
						"github.com/go-chi/chi/v5"
 | 
				
			||||||
 | 
						"github.com/go-chi/chi/v5/middleware"
 | 
				
			||||||
	"github.com/google/uuid"
 | 
						"github.com/google/uuid"
 | 
				
			||||||
	"go.uber.org/zap"
 | 
						"go.uber.org/zap"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HTTPServer struct {
 | 
					type HTTPServer struct {
 | 
				
			||||||
	store  FileStore
 | 
						store        FileStore
 | 
				
			||||||
	config *ServerConfig
 | 
						config       *ServerConfig
 | 
				
			||||||
	Logger *zap.SugaredLogger
 | 
						Logger       *zap.SugaredLogger
 | 
				
			||||||
 | 
						AccessLogger *zap.SugaredLogger
 | 
				
			||||||
	http.Server
 | 
						http.Server
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewHTTPServer(cfg *ServerConfig) *HTTPServer {
 | 
					func NewHTTPServer(cfg *ServerConfig) *HTTPServer {
 | 
				
			||||||
	srv := &HTTPServer{
 | 
						srv := &HTTPServer{
 | 
				
			||||||
		config: cfg,
 | 
							config:       cfg,
 | 
				
			||||||
		Logger: zap.NewNop().Sugar(),
 | 
							Logger:       zap.NewNop().Sugar(),
 | 
				
			||||||
 | 
							AccessLogger: zap.NewNop().Sugar(),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	srv.store = NewMemoryFileStore()
 | 
						srv.store = NewMemoryFileStore()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	r := chi.NewRouter()
 | 
						r := chi.NewRouter()
 | 
				
			||||||
 | 
						r.Use(middleware.RealIP)
 | 
				
			||||||
 | 
						r.Use(middleware.RequestID)
 | 
				
			||||||
 | 
						r.Use(srv.MiddlewareAccessLogger)
 | 
				
			||||||
	r.Get("/", srv.HandlerIndex)
 | 
						r.Get("/", srv.HandlerIndex)
 | 
				
			||||||
	r.Post("/api/file", srv.HandlerAPIFilePost)
 | 
						r.Post("/api/file", srv.HandlerAPIFilePost)
 | 
				
			||||||
	r.Get("/api/file/{id}", srv.HandlerAPIFileGet)
 | 
						r.Get("/api/file/{id}", srv.HandlerAPIFileGet)
 | 
				
			||||||
@@ -43,6 +49,7 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
 | 
				
			|||||||
		ID:   uuid.Must(uuid.NewRandom()).String(),
 | 
							ID:   uuid.Must(uuid.NewRandom()).String(),
 | 
				
			||||||
		Body: r.Body,
 | 
							Body: r.Body,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Check if multipart form
 | 
						// Check if multipart form
 | 
				
			||||||
	ct := r.Header.Get("Content-Type")
 | 
						ct := r.Header.Get("Content-Type")
 | 
				
			||||||
@@ -53,10 +60,10 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
 | 
				
			|||||||
	err := s.store.Store(f)
 | 
						err := s.store.Store(f)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		w.WriteHeader(http.StatusInternalServerError)
 | 
							w.WriteHeader(http.StatusInternalServerError)
 | 
				
			||||||
		s.Logger.Warnw("Error storing file.", "erorr", err, "id", f.ID, "remote_addr", r.RemoteAddr)
 | 
							s.Logger.Warnw("Error storing file.", "req_id", reqID, "error", err, "id", f.ID, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	s.Logger.Infow("Stored file.", "id", f.ID, "remote_addr", r.RemoteAddr)
 | 
						s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
	var resp = struct {
 | 
						var resp = struct {
 | 
				
			||||||
		Message string `json:"message"`
 | 
							Message string `json:"message"`
 | 
				
			||||||
		ID      string `json:"id"`
 | 
							ID      string `json:"id"`
 | 
				
			||||||
@@ -69,7 +76,7 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
 | 
				
			|||||||
	w.WriteHeader(http.StatusAccepted)
 | 
						w.WriteHeader(http.StatusAccepted)
 | 
				
			||||||
	encoder := json.NewEncoder(w)
 | 
						encoder := json.NewEncoder(w)
 | 
				
			||||||
	if err := encoder.Encode(&resp); err != nil {
 | 
						if err := encoder.Encode(&resp); err != nil {
 | 
				
			||||||
		s.Logger.Warnw("Error encoding response to client.", "error", err, "remote_addr", r.RemoteAddr)
 | 
							s.Logger.Warnw("Error encoding response to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -89,12 +96,13 @@ func (s *HTTPServer) HandlerAPIFileGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	w.WriteHeader(http.StatusOK)
 | 
						w.WriteHeader(http.StatusOK)
 | 
				
			||||||
	if _, err := io.Copy(w, f.Body); err != nil {
 | 
						if _, err := io.Copy(w, f.Body); err != nil {
 | 
				
			||||||
		s.Logger.Warnw("Error writing file to client.", "error", err, "remote_addr", r.RemoteAddr)
 | 
							reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
 | 
							s.Logger.Warnw("Error writing file to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
 | 
					func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	s.Logger.Debugw("Processing multipart form.")
 | 
						reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
	type resp struct {
 | 
						type resp struct {
 | 
				
			||||||
		Message string `json:"message"`
 | 
							Message string `json:"message"`
 | 
				
			||||||
		ID      string `json:"id"`
 | 
							ID      string `json:"id"`
 | 
				
			||||||
@@ -104,12 +112,12 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
 | 
				
			|||||||
	var responses []resp
 | 
						var responses []resp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil {
 | 
						if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil {
 | 
				
			||||||
		s.Logger.Warnw("Error parsing multipart form.", "err", err)
 | 
							s.Logger.Warnw("Error parsing multipart form.", "req_id", reqID, "err", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for k := range r.MultipartForm.File {
 | 
						for k := range r.MultipartForm.File {
 | 
				
			||||||
		ff, fh, err := r.FormFile(k)
 | 
							ff, fh, err := r.FormFile(k)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			s.Logger.Warnw("Error reading file from multipart form.", "error", err)
 | 
								s.Logger.Warnw("Error reading file from multipart form.", "req_id", reqID, "error", err)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		f := &File{
 | 
							f := &File{
 | 
				
			||||||
@@ -120,10 +128,10 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		if err := s.store.Store(f); err != nil {
 | 
							if err := s.store.Store(f); err != nil {
 | 
				
			||||||
			w.WriteHeader(http.StatusInternalServerError)
 | 
								w.WriteHeader(http.StatusInternalServerError)
 | 
				
			||||||
			s.Logger.Warnw("Error storing file.", "erorr", err, "id", f.ID, "remote_addr", r.RemoteAddr)
 | 
								s.Logger.Warnw("Error storing file.", "req_id", reqID, "error", err, "id", f.ID, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		s.Logger.Infow("Stored file.", "id", f.ID, "filename", f.OriginalFilename, "remote_addr", r.RemoteAddr)
 | 
							s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "filename", f.OriginalFilename, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		responses = append(responses, resp{Message: "OK", ID: f.ID, URL: "TODO"})
 | 
							responses = append(responses, resp{Message: "OK", ID: f.ID, URL: "TODO"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -132,6 +140,6 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
 | 
				
			|||||||
	w.WriteHeader(http.StatusAccepted)
 | 
						w.WriteHeader(http.StatusAccepted)
 | 
				
			||||||
	encoder := json.NewEncoder(w)
 | 
						encoder := json.NewEncoder(w)
 | 
				
			||||||
	if err := encoder.Encode(&responses); err != nil {
 | 
						if err := encoder.Encode(&responses); err != nil {
 | 
				
			||||||
		s.Logger.Warnw("Error encoding response to client.", "error", err, "remote_addr", r.RemoteAddr)
 | 
							s.Logger.Warnw("Error encoding response to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										99
									
								
								http_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								http_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,99 @@
 | 
				
			|||||||
 | 
					package gpaste_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"mime/multipart"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"net/http/httptest"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"git.t-juice.club/torjus/gpaste"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHandlers(t *testing.T) {
 | 
				
			||||||
 | 
						cfg := &gpaste.ServerConfig{
 | 
				
			||||||
 | 
							Store: &gpaste.ServerStoreConfig{
 | 
				
			||||||
 | 
								Type: "memory",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							URL: "http://localhost:8080",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						hs := gpaste.NewHTTPServer(cfg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("HandlerIndex", func(t *testing.T) {
 | 
				
			||||||
 | 
							rr := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req := httptest.NewRequest(http.MethodGet, "/", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							hs.Handler.ServeHTTP(rr, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if status := rr.Code; status != http.StatusOK {
 | 
				
			||||||
 | 
								t.Errorf("Returned unexpected status")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							expectedBody := "index"
 | 
				
			||||||
 | 
							if body := rr.Body.String(); body != expectedBody {
 | 
				
			||||||
 | 
								t.Errorf("Body does not match expected. Got %s want %s", body, expectedBody)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						t.Run("HandlerAPIFilePost", func(t *testing.T) {
 | 
				
			||||||
 | 
							rr := httptest.NewRecorder()
 | 
				
			||||||
 | 
							buf := &bytes.Buffer{}
 | 
				
			||||||
 | 
							mw := multipart.NewWriter(buf)
 | 
				
			||||||
 | 
							fw, err := mw.CreateFormFile("test", "test.txt")
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Unable to create form file: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							expectedData := "Test OMEGALUL PLS."
 | 
				
			||||||
 | 
							if _, err := io.WriteString(fw, expectedData); err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Unable to write body to buffer: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mw.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							req := httptest.NewRequest(http.MethodPost, "/api/file", buf)
 | 
				
			||||||
 | 
							req.Header.Add("Content-Type", mw.FormDataContentType())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							hs.Handler.ServeHTTP(rr, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if status := rr.Code; status != http.StatusAccepted {
 | 
				
			||||||
 | 
								t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var expectedResp []struct {
 | 
				
			||||||
 | 
								Message string `json:"message"`
 | 
				
			||||||
 | 
								ID      string `json:"id"`
 | 
				
			||||||
 | 
								URL     string `json:"url"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							decoder := json.NewDecoder(rr.Result().Body)
 | 
				
			||||||
 | 
							if err := decoder.Decode(&expectedResp); err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("error decoding response: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if l := len(expectedResp); l != 1 {
 | 
				
			||||||
 | 
								t.Errorf("Response has wrong length. Got %d want %d", l, 1)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							uploadID := expectedResp[0].ID
 | 
				
			||||||
 | 
							if uploadID == "" {
 | 
				
			||||||
 | 
								t.Errorf("Response has empty id")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							t.Run("HandlerAPIFileGet", func(t *testing.T) {
 | 
				
			||||||
 | 
								rr := httptest.NewRecorder()
 | 
				
			||||||
 | 
								url := fmt.Sprintf("/api/file/%s", uploadID)
 | 
				
			||||||
 | 
								req := httptest.NewRequest(http.MethodGet, url, nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								hs.Handler.ServeHTTP(rr, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if status := rr.Code; status != http.StatusOK {
 | 
				
			||||||
 | 
									t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
 | 
				
			||||||
 | 
									t.Logf(url)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if body := rr.Body.String(); body != expectedData {
 | 
				
			||||||
 | 
									t.Errorf("Returned body does not match expected.")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										30
									
								
								middleware.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								middleware.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
				
			|||||||
 | 
					package gpaste
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/go-chi/chi/v5/middleware"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *HTTPServer) MiddlewareAccessLogger(next http.Handler) http.Handler {
 | 
				
			||||||
 | 
						fn := func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
							ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
 | 
				
			||||||
 | 
							t1 := time.Now()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							defer func() {
 | 
				
			||||||
 | 
								s.AccessLogger.Infow(r.Method,
 | 
				
			||||||
 | 
									"path", r.URL.Path,
 | 
				
			||||||
 | 
									"status", ww.Status(),
 | 
				
			||||||
 | 
									"written", ww.BytesWritten(),
 | 
				
			||||||
 | 
									"remote_addr", r.RemoteAddr,
 | 
				
			||||||
 | 
									"processing_time_ms", time.Since(t1).Milliseconds(),
 | 
				
			||||||
 | 
									"req_id", reqID)
 | 
				
			||||||
 | 
							}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							next.ServeHTTP(ww, r)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return http.HandlerFunc(fn)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user