github.com/PDOK/gokoala@v0.50.6/internal/ogc/common/core/main.go (about)

     1  package core
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/PDOK/gokoala/internal/engine"
     7  )
     8  
     9  const (
    10  	templatesDir       = "internal/ogc/common/core/templates/"
    11  	rootPath           = "/"
    12  	apiPath            = "/api"
    13  	alternativeAPIPath = "/openapi.json"
    14  	conformancePath    = "/conformance"
    15  )
    16  
    17  type CommonCore struct {
    18  	engine *engine.Engine
    19  }
    20  
    21  func NewCommonCore(e *engine.Engine) *CommonCore {
    22  	conformanceBreadcrumbs := []engine.Breadcrumb{
    23  		{
    24  			Name: "Conformance",
    25  			Path: "conformance",
    26  		},
    27  	}
    28  	apiBreadcrumbs := []engine.Breadcrumb{
    29  		{
    30  			Name: "OpenAPI specification",
    31  			Path: "api",
    32  		},
    33  	}
    34  
    35  	e.RenderTemplates(rootPath,
    36  		nil,
    37  		engine.NewTemplateKey(templatesDir+"landing-page.go.json"),
    38  		engine.NewTemplateKey(templatesDir+"landing-page.go.html"))
    39  	e.RenderTemplates(rootPath,
    40  		apiBreadcrumbs,
    41  		engine.NewTemplateKey(templatesDir+"api.go.html"))
    42  	e.RenderTemplates(conformancePath,
    43  		conformanceBreadcrumbs,
    44  		engine.NewTemplateKey(templatesDir+"conformance.go.json"),
    45  		engine.NewTemplateKey(templatesDir+"conformance.go.html"))
    46  	core := &CommonCore{
    47  		engine: e,
    48  	}
    49  
    50  	e.Router.Get(rootPath, core.LandingPage())
    51  	e.Router.Get(apiPath, core.API())
    52  	// implements https://gitdocumentatie.logius.nl/publicatie/api/adr/#api-17
    53  	e.Router.Get(alternativeAPIPath, func(w http.ResponseWriter, r *http.Request) { core.apiAsJSON(w, r) })
    54  	e.Router.Get(conformancePath, core.Conformance())
    55  	e.Router.Handle("/*", http.FileServer(http.Dir("assets")))
    56  
    57  	return core
    58  }
    59  
    60  func (c *CommonCore) LandingPage() http.HandlerFunc {
    61  	return func(w http.ResponseWriter, r *http.Request) {
    62  		key := engine.NewTemplateKeyWithLanguage(templatesDir+"landing-page.go."+c.engine.CN.NegotiateFormat(r), c.engine.CN.NegotiateLanguage(w, r))
    63  		c.engine.ServePage(w, r, key)
    64  	}
    65  }
    66  
    67  func (c *CommonCore) API() http.HandlerFunc {
    68  	return func(w http.ResponseWriter, r *http.Request) {
    69  		format := c.engine.CN.NegotiateFormat(r)
    70  		if format == engine.FormatHTML {
    71  			c.apiAsHTML(w, r)
    72  			return
    73  		} else if format == engine.FormatJSON {
    74  			c.apiAsJSON(w, r)
    75  			return
    76  		}
    77  		engine.RenderProblem(engine.ProblemNotFound, w)
    78  	}
    79  }
    80  
    81  func (c *CommonCore) apiAsHTML(w http.ResponseWriter, r *http.Request) {
    82  	key := engine.NewTemplateKeyWithLanguage(templatesDir+"api.go.html", c.engine.CN.NegotiateLanguage(w, r))
    83  	c.engine.ServePage(w, r, key)
    84  }
    85  
    86  func (c *CommonCore) apiAsJSON(w http.ResponseWriter, r *http.Request) {
    87  	c.engine.ServeResponse(w, r, true, true, engine.MediaTypeOpenAPI, c.engine.OpenAPI.SpecJSON)
    88  }
    89  
    90  func (c *CommonCore) Conformance() http.HandlerFunc {
    91  	return func(w http.ResponseWriter, r *http.Request) {
    92  		key := engine.NewTemplateKeyWithLanguage(templatesDir+"conformance.go."+c.engine.CN.NegotiateFormat(r), c.engine.CN.NegotiateLanguage(w, r))
    93  		c.engine.ServePage(w, r, key)
    94  	}
    95  }