Files
openfsd/web/config.go
Reese Norris 335409c4b4 Add ConfigRepository and enhance server configuration management
1. Database Enhancements (db/repositories.go):
   - Added ConfigRepository interface and implementations for PostgreSQL and SQLite
   - Updated Repositories struct to include ConfigRepository
   - Modified NewRepositories to initialize both UserRepo and ConfigRepo

2. FSD Server Improvements:
   - Removed hardcoded jwtSecret, now retrieved from ConfigRepository (fsd/conn.go, web/auth.go)
   - Added dynamic welcome message retrieval from ConfigRepository (fsd/conn.go)
   - Optimized METAR buffer size from 4096 to 512 bytes (fsd/metar.go)
   - Reduced minimum fields for DeleteATC and DeletePilot packets (fsd/packet.go)
   - Improved Haversine distance calculation with constants (fsd/postoffice.go)
   - Added thread-safety documentation for sendError (fsd/client.go)

3. Server Configuration (fsd/server.go):
   - Added NewDefaultServer to initialize server with environment-based config
   - Implemented automatic database migration and default admin user creation
   - Added configurable METAR worker count
   - Improved logging with slog and environment-based debug level

4. Web Interface Enhancements:
   - Added user and config editor frontend routes (web/frontend.go, web/routes.go)
   - Improved JWT handling by retrieving secret from ConfigRepository (web/auth.go)
   - Enhanced user management API endpoints (web/user.go)
   - Updated dashboard to display CID and conditional admin links (web/templates/dashboard.html)
   - Embedded templates using go:embed (web/templates.go)

5. Frontend JavaScript Improvements:
   - Added networkRatingFromInt helper for readable ratings (web/static/js/openfsd/dashboard.js)
   - Improved API request handling with auth/no-auth variants (web/static/js/openfsd/api.js)

6. Miscellaneous:
   - Added sethvargo/go-envconfig dependency for environment variable parsing
   - Fixed parseVisRange to use 64-bit float parsing (fsd/util.go)
   - Added strPtr utility function (fsd/util.go, web/main.go)
   - Improved SVG logo rendering in layout (web/templates/layout.html)
2025-05-16 22:27:26 -07:00

108 lines
2.7 KiB
Go

package main
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/renorris/openfsd/db"
"github.com/renorris/openfsd/fsd"
"net/http"
)
type KeyValuePair struct {
Key string `json:"key" binding:"required"`
Value string `json:"value" binding:"required"`
}
func (s *Server) handleGetConfig(c *gin.Context) {
claims := getJwtContext(c)
if claims.NetworkRating < fsd.NetworkRatingAdministator {
writeAPIV1Response(c, http.StatusForbidden, &genericAPIV1Forbidden)
return
}
var configKeys = []string{
db.ConfigWelcomeMessage,
}
type ResponseBody struct {
KeyValuePairs []KeyValuePair `json:"key_value_pairs" binding:"required"`
}
resBody := ResponseBody{
KeyValuePairs: make([]KeyValuePair, 0, len(configKeys)),
}
for i := range configKeys {
key := configKeys[i]
val, err := s.dbRepo.ConfigRepo.Get(key)
if err != nil {
if !errors.Is(err, db.ErrConfigKeyNotFound) {
res := newAPIV1Failure("Error reading key/value from persistent storage")
writeAPIV1Response(c, http.StatusInternalServerError, &res)
return
}
continue
}
resBody.KeyValuePairs = append(resBody.KeyValuePairs,
KeyValuePair{
Key: key,
Value: val,
},
)
}
res := newAPIV1Success(&resBody)
writeAPIV1Response(c, http.StatusOK, &res)
}
func (s *Server) handleUpdateConfig(c *gin.Context) {
claims := getJwtContext(c)
if claims.NetworkRating < fsd.NetworkRatingAdministator {
writeAPIV1Response(c, http.StatusForbidden, &genericAPIV1Forbidden)
return
}
type RequestBody struct {
KeyValuePairs []KeyValuePair `json:"key_value_pairs" binding:"required"`
}
var reqBody RequestBody
if ok := bindJSONOrAbort(c, &reqBody); !ok {
return
}
for i := range reqBody.KeyValuePairs {
kv := reqBody.KeyValuePairs[i]
if err := s.dbRepo.ConfigRepo.Set(kv.Key, kv.Value); err != nil {
res := newAPIV1Failure("Error writing key/value into persistent storage")
writeAPIV1Response(c, http.StatusInternalServerError, &res)
return
}
}
res := newAPIV1Success(nil)
writeAPIV1Response(c, http.StatusOK, &res)
}
func (s *Server) handleResetSecretKey(c *gin.Context) {
claims := getJwtContext(c)
if claims.NetworkRating < fsd.NetworkRatingAdministator {
writeAPIV1Response(c, http.StatusForbidden, &genericAPIV1Forbidden)
return
}
secretKey, err := db.GenerateJwtSecretKey()
if err != nil {
writeAPIV1Response(c, http.StatusInternalServerError, &genericAPIV1InternalServerError)
return
}
if err = s.dbRepo.ConfigRepo.Set(db.ConfigJwtSecretKey, string(secretKey[:])); err != nil {
writeAPIV1Response(c, http.StatusInternalServerError, &genericAPIV1InternalServerError)
return
}
res := newAPIV1Success(nil)
writeAPIV1Response(c, http.StatusOK, &res)
}