63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"go.sour.is/xt/internal/otel"
|
|
)
|
|
|
|
const iAmTheWatcher = "I am the Watcher. I am your guide through this vast new twtiverse."
|
|
|
|
func httpServer(ctx context.Context, app *appState) error {
|
|
ctx, span := otel.Span(ctx)
|
|
defer span.End()
|
|
|
|
span.AddEvent("start http server")
|
|
|
|
db, err := app.DB(ctx)
|
|
if err != nil {
|
|
span.RecordError(fmt.Errorf("%w: missing db", err))
|
|
return err
|
|
}
|
|
|
|
api := API{
|
|
app: app,
|
|
db: db,
|
|
hostname: app.args.Hostname,
|
|
}
|
|
|
|
html := HTML{
|
|
app: app,
|
|
db: db,
|
|
hostname: app.args.Hostname,
|
|
}
|
|
|
|
http.HandleFunc("/", html.home)
|
|
http.HandleFunc("/health", html.healthcheck)
|
|
|
|
http.HandleFunc("/api/plain", api.plain)
|
|
http.HandleFunc("/api/plain/conv/{hash}", api.conv)
|
|
http.HandleFunc("/api/plain/mentions", api.mentions)
|
|
http.HandleFunc("/api/plain/twt", api.twt)
|
|
http.HandleFunc("/api/plain/tweets", api.twt)
|
|
http.HandleFunc("/api/plain/users", api.users)
|
|
http.HandleFunc("/api/plain/queue", api.queue)
|
|
|
|
srv := &http.Server{
|
|
Addr: app.args.Listen,
|
|
Handler: http.DefaultServeMux,
|
|
}
|
|
|
|
app.AddCancel(srv.Shutdown)
|
|
err = srv.ListenAndServe()
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
|
span.RecordError(err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|