Eric Bower
·
20 Sep 24
api.go
1package pubsub
2
3import (
4 "fmt"
5 "net/http"
6 "os"
7
8 "github.com/picosh/pico/db/postgres"
9 "github.com/picosh/pico/shared"
10)
11
12func serveFile(file string, contentType string) http.HandlerFunc {
13 return func(w http.ResponseWriter, r *http.Request) {
14 logger := shared.GetLogger(r)
15 cfg := shared.GetCfg(r)
16
17 contents, err := os.ReadFile(cfg.StaticPath(fmt.Sprintf("public/%s", file)))
18 if err != nil {
19 logger.Error("could not read statis file", "err", err.Error())
20 http.Error(w, "file not found", 404)
21 }
22 w.Header().Add("Content-Type", contentType)
23
24 _, err = w.Write(contents)
25 if err != nil {
26 logger.Error("could not write static file", "err", err.Error())
27 http.Error(w, "server error", 500)
28 }
29 }
30}
31
32func createStaticRoutes() []shared.Route {
33 return []shared.Route{
34 shared.NewRoute("GET", "/main.css", serveFile("main.css", "text/css")),
35 shared.NewRoute("GET", "/smol.css", serveFile("smol.css", "text/css")),
36 shared.NewRoute("GET", "/syntax.css", serveFile("syntax.css", "text/css")),
37 shared.NewRoute("GET", "/card.png", serveFile("card.png", "image/png")),
38 shared.NewRoute("GET", "/favicon-16x16.png", serveFile("favicon-16x16.png", "image/png")),
39 shared.NewRoute("GET", "/favicon-32x32.png", serveFile("favicon-32x32.png", "image/png")),
40 shared.NewRoute("GET", "/apple-touch-icon.png", serveFile("apple-touch-icon.png", "image/png")),
41 shared.NewRoute("GET", "/favicon.ico", serveFile("favicon.ico", "image/x-icon")),
42 shared.NewRoute("GET", "/robots.txt", serveFile("robots.txt", "text/plain")),
43 shared.NewRoute("GET", "/send-output.gif", serveFile("send-output.gif", "image/gif")),
44 shared.NewRoute("GET", "/send-file.gif", serveFile("send-file.gif", "image/gif")),
45 shared.NewRoute("GET", "/multi-sub.gif", serveFile("multi-sub.gif", "image/gif")),
46 shared.NewRoute("GET", "/multi-pub.gif", serveFile("multi-pub.gif", "image/gif")),
47 shared.NewRoute("GET", "/multi-pub-sub.gif", serveFile("multi-pub-sub.gif", "image/gif")),
48 shared.NewRoute("GET", "/pipe-chat.gif", serveFile("pipe-chat.gif", "image/gif")),
49 shared.NewRoute("GET", "/pipe-cmd-output.gif", serveFile("pipe-cmd-output.gif", "image/gif")),
50 shared.NewRoute("GET", "/pipe-reverse-shell.gif", serveFile("pipe-reverse-shell.gif", "image/gif")),
51 }
52}
53
54func createMainRoutes(staticRoutes []shared.Route) []shared.Route {
55 routes := []shared.Route{
56 shared.NewRoute("GET", "/", shared.CreatePageHandler("html/marketing.page.tmpl")),
57 shared.NewRoute("GET", "/check", shared.CheckHandler),
58 }
59
60 routes = append(
61 routes,
62 staticRoutes...,
63 )
64
65 return routes
66}
67
68func StartApiServer() {
69 cfg := NewConfigSite()
70 db := postgres.NewDB(cfg.DbURL, cfg.Logger)
71 defer db.Close()
72 logger := cfg.Logger
73
74 staticRoutes := createStaticRoutes()
75
76 if cfg.Debug {
77 staticRoutes = shared.CreatePProfRoutes(staticRoutes)
78 }
79
80 mainRoutes := createMainRoutes(staticRoutes)
81 subdomainRoutes := staticRoutes
82
83 apiConfig := &shared.ApiConfig{
84 Cfg: cfg,
85 Dbpool: db,
86 }
87 handler := shared.CreateServe(mainRoutes, subdomainRoutes, apiConfig)
88 router := http.HandlerFunc(handler)
89
90 portStr := fmt.Sprintf(":%s", cfg.Port)
91 logger.Info(
92 "Starting server on port",
93 "port", cfg.Port,
94 "domain", cfg.Domain,
95 )
96
97 logger.Error("listen", "err", http.ListenAndServe(portStr, router).Error())
98}