subdomains enabled, implemented go-routines, connected to portfolio, etc. heading into some major refactoring of the codebase, documentation, and installation/update scripts to enable rapid development.

This commit is contained in:
Josh Ashton
2024-11-01 14:49:14 -06:00
committed by Violet Ash
parent 7a3f895782
commit 54271e2323
11 changed files with 599 additions and 745 deletions
@@ -0,0 +1,42 @@
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
// Serve an API using mux.Router().Host({domain}).Subrouter().
// Provide the router, domain, an array of endpoints and functions, and
// the port you would like the API accessible to.
func serveApi(
router *mux.Router,
domain string,
endpoint []string,
function []func(http.ResponseWriter, *http.Request),
port int,
) {
// TODO: Add error handling
apiR := router.Host(domain).Subrouter()
for i := 0; i < len(endpoint); i++ {
apiR.HandleFunc(endpoint[i], function[i])
}
apiSrv := &http.Server{
Addr: "0.0.0.0:" + string(port),
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: apiR,
}
go func() {
if err := apiSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Println(err)
}
}()
log.Println("API server is running on port " + string(port))
}