initial
This commit is contained in:
commit
38deaabd49
|
@ -0,0 +1,13 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
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", version)
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const version = "1.0.0"
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
port int
|
||||||
|
env string
|
||||||
|
}
|
||||||
|
|
||||||
|
type application struct {
|
||||||
|
config config
|
||||||
|
logger *log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var cfg config
|
||||||
|
|
||||||
|
flag.IntVar(&cfg.port, "port", 4000, "API Server Port")
|
||||||
|
flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||||
|
|
||||||
|
app := &application{
|
||||||
|
config: cfg,
|
||||||
|
logger: logger,
|
||||||
|
}
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: fmt.Sprintf(":%d", cfg.port),
|
||||||
|
Handler: app.routes(),
|
||||||
|
IdleTimeout: time.Minute,
|
||||||
|
ReadTimeout: 5 * time.Second,
|
||||||
|
WriteTimeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Printf("starting %s server on %s", cfg.env, srv.Addr)
|
||||||
|
err := srv.ListenAndServe()
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintln(w, "create a new movie")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
params := httprouter.ParamsFromContext(r.Context())
|
||||||
|
|
||||||
|
id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "show the details of movie %d\n", id)
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (app *application) routes() *httprouter.Router {
|
||||||
|
router := httprouter.New()
|
||||||
|
|
||||||
|
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthCheckHandler)
|
||||||
|
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
|
||||||
|
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
module greenlight.ergz
|
||||||
|
|
||||||
|
go 1.20
|
||||||
|
|
||||||
|
require github.com/julienschmidt/httprouter v1.3.0 // indirect
|
Loading…
Reference in New Issue