Add ability to put pastes via route

master
Nick Krichevsky 2019-03-10 00:08:59 -05:00
parent 24923ae1e4
commit ef4699ed8b
4 changed files with 52 additions and 10 deletions

34
web/paste.go Normal file
View File

@ -0,0 +1,34 @@
package web
import (
"net/http"
)
const (
titleFormKey = "title"
bodyFormKey = "body"
)
func (server *Webserver) makePaste(w http.ResponseWriter, req *http.Request) {
title := req.PostFormValue(titleFormKey)
body := req.PostFormValue(bodyFormKey)
if len(title) == 0 || len(body) == 0 {
w.WriteHeader(400)
return
}
paster, err := server.pasteService.CreatePaste(title)
if err != nil {
w.WriteHeader(500)
// TODO: log error
return
}
defer paster.Close()
_, err = paster.Write([]byte(body))
if err != nil {
w.WriteHeader(500)
// TODO: log error
return
}
}

View File

@ -4,12 +4,14 @@ import (
"net/http"
"github.com/go-chi/chi"
"github.com/ollien/updown/handler"
)
// Webserver is the main websever
type Webserver struct {
httpServer *http.Server
router chi.Router
httpServer *http.Server
router chi.Router
pasteService *handler.PasteService
}
// NewWebserver creates a new webserver and sets up all routes
@ -30,4 +32,5 @@ func (server *Webserver) setupRoutes() {
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Coming soon..."))
})
router.Post("/paste", server.makePaste)
}

View File

@ -8,6 +8,7 @@ package web
import (
"github.com/go-chi/chi"
"github.com/ollien/updown/config"
"github.com/ollien/updown/handler"
"net/http"
"strconv"
)
@ -18,7 +19,8 @@ func initWebserver() *Webserver {
configConfig := config.Get()
router := provideRouter()
server := provideHTTPServer(configConfig, router)
webserver := provideWebserver(server, router)
pasteService := handler.NewPasteService()
webserver := provideWebserver(server, router, pasteService)
return webserver
}
@ -31,10 +33,11 @@ func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server
}
}
func provideWebserver(httpServer *http.Server, router chi.Router) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *handler.PasteService) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,
httpServer: httpServer,
router: router,
pasteService: pasteService,
}
return &webserver

View File

@ -9,11 +9,12 @@ import (
"github.com/go-chi/chi"
"github.com/google/wire"
"github.com/ollien/updown/config"
"github.com/ollien/updown/handler"
)
// initWebserver is generated by wire. Please see wire_gen.go for the generated implementation
func initWebserver() *Webserver {
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get)
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, handler.NewPasteService)
return &Webserver{}
}
@ -24,10 +25,11 @@ func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server
}
}
func provideWebserver(httpServer *http.Server, router chi.Router) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *handler.PasteService) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,
httpServer: httpServer,
router: router,
pasteService: pasteService,
}
return &webserver