Setting up stub auth service

This commit is contained in:
2024-08-28 12:55:03 +02:00
parent 2a64b5761e
commit c0a58ff6b6
5 changed files with 342 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package main
import (
"authentication/data"
"database/sql"
"fmt"
"log"
"net/http"
)
const webPort = "4000"
type Config struct {
DB *sql.DB
Models data.Models
}
func main() {
// TODO connect to DB
// set up config
app := Config{}
log.Printf("Starting authentication service on port %s\n", webPort)
// define http server
srv := &http.Server{
Addr: fmt.Sprintf(":%s", webPort),
Handler: app.routes(),
}
err := srv.ListenAndServe()
if err != nil {
log.Panic(err)
}
}

View File

@ -0,0 +1,27 @@
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
)
func (app *Config) routes() http.Handler {
mux := chi.NewRouter()
// specify who is allowed to connect
mux.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
}))
mux.Use(middleware.Heartbeat("/ping"))
return mux
}