Creating Makefile. Adding .idea files. Updating main.go : Configuration of the application and the server. Creating our first handler for the endpoint /v1/healthcheck with healtcheck.go file.
This commit is contained in:
10
.idea/.gitignore
generated
vendored
Normal file
10
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Environment-dependent path to Maven home directory
|
||||||
|
/mavenHomeManager.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
10
.idea/greenlight.iml
generated
Normal file
10
.idea/greenlight.iml
generated
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/greenlight.iml" filepath="$PROJECT_DIR$/.idea/greenlight.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
23
cmd/api/healthcheck.go
Normal file
23
cmd/api/healthcheck.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
Declare a handler which writes a plain-text response with information about the
|
||||||
|
application status, operating environment and version
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
Tips: The important thing to point out here is that healthcheckHandler is implemented as a method
|
||||||
|
on our application struct. This is an effective and idiomatic way to make dependencies available
|
||||||
|
to our handlers without resorting to global variables or closures - any dependency that the
|
||||||
|
healthcheckHandler needs can simply be included as a field in the application struct when we
|
||||||
|
initialize it in main()
|
||||||
|
*/
|
||||||
|
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, "status: available")
|
||||||
|
fmt.Fprintf(w, "environment: %s\n", app.config.env)
|
||||||
|
fmt.Fprintf(w, "version: %s\n", version)
|
||||||
|
}
|
||||||
89
cmd/api/main.go
Normal file
89
cmd/api/main.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
Declare a string containing the application version number.
|
||||||
|
Later we'll generate this automatically at build time, but for now we'll just store the
|
||||||
|
version number as a hard-coded global constant.
|
||||||
|
*/
|
||||||
|
const version = "1.0.0"
|
||||||
|
|
||||||
|
/*
|
||||||
|
Define a config struct to hold all the configuration settings for our application.
|
||||||
|
For now, the only configuration settings will be the network port that we want the server to listen on,
|
||||||
|
and the name of the current operating environment for the application (development, staging, production, etc.)
|
||||||
|
We'll read in the configuration settings from command-line flags when the application starts.
|
||||||
|
*/
|
||||||
|
type config struct {
|
||||||
|
port int
|
||||||
|
env string
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Define an application struct to hold the dependencies for our HTTP handlers, helpers,
|
||||||
|
and middleware.
|
||||||
|
*/
|
||||||
|
type application struct {
|
||||||
|
config config
|
||||||
|
logger *slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Declare an instance of the config struct.
|
||||||
|
var cfg config
|
||||||
|
|
||||||
|
/*
|
||||||
|
Read the value of the port and env command-line flags into the config struct.
|
||||||
|
We default to using the port number 4000 and the environment "development" if no corresponding flags are provided.
|
||||||
|
*/
|
||||||
|
flag.IntVar(&cfg.port, "port", 4000, "API server port")
|
||||||
|
flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// Initialize a new structured logger which writes log entries to standard out stream.
|
||||||
|
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||||
|
|
||||||
|
// Declare an instance of the application struct, containing the config struct and the logger.
|
||||||
|
app := &application{
|
||||||
|
config: cfg,
|
||||||
|
logger: logger,
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Declare a new servemux and add a /v1/healthcheck route which dispatches requests
|
||||||
|
to the healthcheckHandler method.
|
||||||
|
*/
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/v1/healthcheck", app.healthcheckHandler)
|
||||||
|
|
||||||
|
/*
|
||||||
|
Declare a HTTP server which listens on the port provided in the config struct,
|
||||||
|
uses the servemux we created above as the handler, has some sensible timeout
|
||||||
|
settings and writes any log messages to the structured logger at Error level.
|
||||||
|
*/
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: fmt.Sprintf(":%d", cfg.port),
|
||||||
|
Handler: mux,
|
||||||
|
IdleTimeout: time.Minute,
|
||||||
|
ReadTimeout: 5 * time.Second,
|
||||||
|
WriteTimeout: 10 * time.Second,
|
||||||
|
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the HTTP server.
|
||||||
|
logger.Info("starting server", "addr", srv.Addr, "env", cfg.env)
|
||||||
|
|
||||||
|
err := srv.ListenAndServe()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user