Setting up stub auth service
This commit is contained in:
		
							
								
								
									
										36
									
								
								authentication-service/cmd/api/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								authentication-service/cmd/api/main.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								authentication-service/cmd/api/routes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								authentication-service/cmd/api/routes.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user