generated from templates/go-generic
Dan Anglin
7d0336d3db
Add the metadata endpoint so that clients can get the server's authorization and token endpoints. Changes: - Add a config object to allow administrators to configure the binding address, binding port and domain name. - Add a target in the magefiles and a step in the CI workflow to run go vet. - Add the Dockerfile. - Replace the --address flag with the --config flag. - Add the metadata endpoint to the router to return a JSON document containing the URLs to the metadata and token endpoints.
41 lines
824 B
Go
41 lines
824 B
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/config"
|
|
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/info"
|
|
)
|
|
|
|
type Server struct {
|
|
httpServer *http.Server
|
|
}
|
|
|
|
func NewServer(cfg config.Config) *Server {
|
|
address := fmt.Sprintf("%s:%d", cfg.BindAddress, cfg.Port)
|
|
|
|
httpServer := http.Server{
|
|
Addr: address,
|
|
ReadHeaderTimeout: 1 * time.Second,
|
|
Handler: newMux(cfg),
|
|
}
|
|
|
|
server := Server{
|
|
httpServer: &httpServer,
|
|
}
|
|
|
|
return &server
|
|
}
|
|
|
|
func (s *Server) Serve() error {
|
|
slog.Info(info.ApplicationName+" is now ready to serve web requests", "address", s.httpServer.Addr)
|
|
|
|
if err := s.httpServer.ListenAndServe(); err != nil {
|
|
return fmt.Errorf("error running the server: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|