2025-02-15 16:12:42 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-03-27 16:35:05 -06:00
|
|
|
"context"
|
2025-02-24 17:28:09 -07:00
|
|
|
"errors"
|
2025-02-15 16:12:42 -07:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2025-03-13 22:36:24 -06:00
|
|
|
|
2025-02-24 17:28:09 -07:00
|
|
|
"go.sour.is/xt/internal/otel"
|
2025-02-15 16:12:42 -07:00
|
|
|
)
|
|
|
|
|
|
2025-03-28 07:21:34 -06:00
|
|
|
const iAmTheWatcher = "I am the Watcher. I am your guide through this vast new twtiverse."
|
2025-03-30 12:12:10 -06:00
|
|
|
|
2025-03-27 16:35:05 -06:00
|
|
|
func httpServer(ctx context.Context, app *appState) error {
|
|
|
|
|
ctx, span := otel.Span(ctx)
|
2025-03-13 22:36:24 -06:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
span.AddEvent("start http server")
|
2025-02-15 16:12:42 -07:00
|
|
|
|
2025-03-13 22:36:24 -06:00
|
|
|
db, err := app.DB(ctx)
|
2025-02-15 16:12:42 -07:00
|
|
|
if err != nil {
|
2025-03-13 22:36:24 -06:00
|
|
|
span.RecordError(fmt.Errorf("%w: missing db", err))
|
2025-02-24 17:28:09 -07:00
|
|
|
return err
|
2025-02-15 16:12:42 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-31 15:23:40 -06:00
|
|
|
api := API{
|
|
|
|
|
app: app,
|
|
|
|
|
db: db,
|
|
|
|
|
hostname: app.args.Hostname,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-31 17:39:24 -06:00
|
|
|
html := HTML{
|
2025-04-07 13:11:09 -06:00
|
|
|
app: app,
|
|
|
|
|
db: db,
|
2025-03-31 17:39:24 -06:00
|
|
|
hostname: app.args.Hostname,
|
|
|
|
|
}
|
2025-03-13 22:36:24 -06:00
|
|
|
|
2025-03-31 17:39:24 -06:00
|
|
|
http.HandleFunc("/health", html.healthcheck)
|
2025-02-15 16:12:42 -07:00
|
|
|
|
2025-04-02 11:55:56 -06:00
|
|
|
http.HandleFunc("/", html.home)
|
|
|
|
|
http.HandleFunc("/conv/{hash}", html.conv)
|
|
|
|
|
|
2025-03-31 15:23:40 -06:00
|
|
|
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)
|
2025-02-15 16:12:42 -07:00
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
|
Addr: app.args.Listen,
|
|
|
|
|
Handler: http.DefaultServeMux,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-27 16:35:05 -06:00
|
|
|
app.AddCancel(srv.Shutdown)
|
2025-02-15 16:12:42 -07:00
|
|
|
err = srv.ListenAndServe()
|
2025-02-24 17:28:09 -07:00
|
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
2025-03-13 22:36:24 -06:00
|
|
|
span.RecordError(err)
|
2025-02-24 17:28:09 -07:00
|
|
|
return err
|
2025-02-15 16:12:42 -07:00
|
|
|
}
|
2025-02-24 17:28:09 -07:00
|
|
|
|
|
|
|
|
return nil
|
2025-02-15 16:12:42 -07:00
|
|
|
}
|