implement info endpoint

This commit is contained in:
Lukas Schulte Pelkum 2023-06-08 19:55:43 +02:00
parent dc16506932
commit 6260f20fc4
No known key found for this signature in database
GPG Key ID: AB3985CECFAFC962
4 changed files with 17 additions and 3 deletions

View File

@ -70,6 +70,10 @@ func main() {
// Start the web server
log.Info().Str("address", cfg.WebAddress).Msg("Starting the web server...")
var adminTokens []string
if cfg.ModificationTokenMaster != "" {
adminTokens = []string{cfg.ModificationTokenMaster}
}
webServer := &web.Server{
Address: cfg.WebAddress,
Storage: driver,
@ -80,7 +84,7 @@ func main() {
ModificationTokensEnabled: cfg.ModificationTokens,
ModificationTokenLength: cfg.ModificationTokenLength,
ModificationTokenCharset: cfg.ModificationTokenCharacters,
AdminTokens: []string{cfg.ModificationTokenMaster},
AdminTokens: adminTokens,
}
go func() {
if err := webServer.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {

View File

@ -6,6 +6,7 @@ const devEnvironmentName = "dev"
var (
Environment = devEnvironmentName
Version = "dev"
)
func IsProdEnvironment() bool {

View File

@ -59,7 +59,7 @@ func frontendHandler(notFoundHandler http.HandlerFunc) http.HandlerFunc {
return
}
writer.Header().Set("Content-Type", mime.TypeByExtension(fileInfo.Name()))
writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileInfo.Name())))
writer.Header().Set("Content-Length", strconv.Itoa(len(content)))
_, _ = writer.Write(content)
}

View File

@ -3,6 +3,7 @@ package web
import (
"context"
"github.com/go-chi/chi/v5"
"github.com/lus/pasty/internal/meta"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/storage"
"net/http"
@ -61,7 +62,15 @@ func (server *Server) Start() error {
router.With(server.v2MiddlewareInjectPaste).Get("/api/v2/pastes/{paste_id}", server.v2EndpointGetPaste)
router.Post("/api/v2/pastes", server.v2EndpointCreatePaste)
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Patch("/api/v2/pastes/{paste_id}", server.v2EndpointModifyPaste)
router.Delete("/api/v2/pastes/{paste_id}", server.v2EndpointDeletePaste)
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Delete("/api/v2/pastes/{paste_id}", server.v2EndpointDeletePaste)
router.Get("/api/v2/info", func(writer http.ResponseWriter, request *http.Request) {
writeJSONOrErr(writer, http.StatusOK, map[string]any{
"version": meta.Version,
"modificationTokens": server.ModificationTokensEnabled,
"reports": false, // TODO: Return report state
"pasteLifetime": -1, // TODO: Return paste lifetime
})
})
// Start the HTTP server
server.httpServer = &http.Server{