add go fiber example
This commit is contained in:
19
go-fiber-simple/cmd/api/healthcheck.go
Normal file
19
go-fiber-simple/cmd/api/healthcheck.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]string{
|
||||
"status": "ok",
|
||||
"version": version,
|
||||
}
|
||||
|
||||
// No need to add acess control origin headers. On other routes, that may be necessary
|
||||
err := app.writeJSON(w, http.StatusOK, data, nil)
|
||||
if err != nil {
|
||||
app.logger.Error(err.Error())
|
||||
http.Error(w, "server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
28
go-fiber-simple/cmd/api/helpers.go
Normal file
28
go-fiber-simple/cmd/api/helpers.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// The writeJSON() method is a generic helper for writing JSON to a response
|
||||
func (app *application) writeJSON(w http.ResponseWriter, sCode int, data any, headers http.Header) error {
|
||||
marshalledJson, err := json.Marshal(data)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Valid json requires newline
|
||||
marshalledJson = append(marshalledJson, '\n')
|
||||
|
||||
for key, value := range headers {
|
||||
w.Header()[key] = value
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(sCode)
|
||||
w.Write(marshalledJson)
|
||||
|
||||
return nil
|
||||
}
|
||||
63
go-fiber-simple/cmd/api/main.go
Normal file
63
go-fiber-simple/cmd/api/main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const version = "0.0.1"
|
||||
|
||||
type config struct {
|
||||
port int
|
||||
}
|
||||
|
||||
type application struct {
|
||||
config config
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var cfg config
|
||||
|
||||
// Try to read environment variable for port (given by railway). Otherwise use default
|
||||
port := os.Getenv("PORT")
|
||||
intPort, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
intPort = 4000
|
||||
}
|
||||
|
||||
// Set the port to run the API on
|
||||
cfg.port = intPort
|
||||
|
||||
// create the logger
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
|
||||
// create the application
|
||||
app := &application{
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
// create the server
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", cfg.port),
|
||||
Handler: app.routes(),
|
||||
IdleTimeout: 45 * time.Second,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
|
||||
}
|
||||
|
||||
logger.Info("server started", "addr", srv.Addr)
|
||||
|
||||
// Start the server
|
||||
err = srv.ListenAndServe()
|
||||
logger.Error(err.Error())
|
||||
os.Exit(1)
|
||||
|
||||
}
|
||||
16
go-fiber-simple/cmd/api/routes.go
Normal file
16
go-fiber-simple/cmd/api/routes.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func (app *application) routes() *httprouter.Router {
|
||||
router := httprouter.New()
|
||||
|
||||
// Define the available routes
|
||||
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
||||
|
||||
return router
|
||||
}
|
||||
Reference in New Issue
Block a user