En cours de développement

master
losyme 11 months ago
parent 356d926628
commit 6773db4839
  1. 15
      .editorconfig
  2. 22
      LICENSE
  3. 10
      README.md
  4. 25
      Taskfile.yml
  5. 13
      go.mod
  6. 8
      go.sum
  7. 122
      server/config.go
  8. 78
      server/server.go

@ -0,0 +1,15 @@
root = true
[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.go]
indent_style = tab
[*.md]
trim_trailing_whitespace = false

@ -1,9 +1,19 @@
MIT License
MIT License Copyright (c) 2021 losyme
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ -1,3 +1,11 @@
# kong
AFAIRE
A FAIRE.
## Licence
MIT.
---
Copyright (c) 2021 `losyme`.

@ -0,0 +1,25 @@
## https://taskfile.dev
##---------------------
version: '3'
tasks:
update:
cmds:
- go mod tidy
lint:
cmds:
- golangci-lint run
test:
cmds:
- go test -cover -v ./...
default:
cmds:
- task: lint
- task: test
git:
cmds:
- git add .
- git commit -m "En cours de développement"
- git push

@ -0,0 +1,13 @@
module forge.chapril.org/losyme/kong
go 1.17
require (
forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40
forge.chapril.org/losyme/util v0.0.0-20211025142031-fd89d32d86fe
)
require (
forge.chapril.org/losyme/buffer v0.0.0-20211003203540-771701f5a518 // indirect
forge.chapril.org/losyme/kvfmt v0.0.0-20211003204256-34ac32764dae // indirect
)

@ -0,0 +1,8 @@
forge.chapril.org/losyme/buffer v0.0.0-20211003203540-771701f5a518 h1:B7wu0DWUwpt0Mw/VTuWQZikhV/VD6kc2iJWbIY+jgEM=
forge.chapril.org/losyme/buffer v0.0.0-20211003203540-771701f5a518/go.mod h1:4Yl585hmF3bWZfuwGF2Ym8ErykJZiF9yawlmK7szIkc=
forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40 h1:EoVVFHoIsWRlecHmXP4kx3ALchz5DAfNwso3gcl/Jbk=
forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40/go.mod h1:saYfIRBW9zjiCik+/+5ZaajMEnTPBNI00lF9EnO+2kM=
forge.chapril.org/losyme/kvfmt v0.0.0-20211003204256-34ac32764dae h1:RdYWXKy+OQNJueb4Qqqo7sL4OY3/TpfUvmoIoH8ua9E=
forge.chapril.org/losyme/kvfmt v0.0.0-20211003204256-34ac32764dae/go.mod h1:wnVjwnCO2vFLLZ1EgFuFws1n8XLluufsGsdozGYSA7c=
forge.chapril.org/losyme/util v0.0.0-20211025142031-fd89d32d86fe h1:xjMUKHxtG8jz57JyOxgNLlRaPLJeQvMV9K3HNlhKDB8=
forge.chapril.org/losyme/util v0.0.0-20211025142031-fd89d32d86fe/go.mod h1:lKYvkuBOTU3E2HuQZDh4ZSA4u3VcFopQbdPUKaHGHEk=

@ -0,0 +1,122 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package server
import (
_tls "crypto/tls"
"crypto/x509"
"os"
"time"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/util"
)
type TLS struct {
Host string
Cert string
Key string
AuthType int
CA string
}
func (tls *TLS) validate() error {
if ok, err := util.FileExists(tls.Cert); err != nil {
return err
} else if !ok {
return errors.New("[eckmuhl] this file doesn't exist", "file", tls.Cert) ///////////////////////////////////////
}
if ok, err := util.FileExists(tls.Key); err != nil {
return err
} else if !ok {
return errors.New("[eckmuhl] this file doesn't exist", "file", tls.Key) ////////////////////////////////////////
}
if tls.AuthType < 0 || tls.AuthType > 4 {
return errors.New("[eckmuhl] the authentication type is not valid", "type", tls.AuthType) //////////////////////
}
if tls.AuthType == 0 {
return nil
}
if ok, err := util.FileExists(tls.CA); err != nil {
return err
} else if !ok {
return errors.New("[eckmuhl] this file doesn't exist", "file", tls.CA) /////////////////////////////////////////
}
return nil
}
func (tls *TLS) config() (*_tls.Config, error) {
if tls == nil {
return nil, nil
}
var certPool *x509.CertPool
authType := _tls.NoClientCert
if tls.AuthType != 0 {
buf, err := os.ReadFile(tls.CA)
if err != nil {
return nil, errors.WithMessage(err, "[eckmuhl] unable to read this file", "file", tls.CA) //////////////////
}
certPool = x509.NewCertPool()
certPool.AppendCertsFromPEM(buf)
switch tls.AuthType {
case 1:
authType = _tls.RequestClientCert
case 2:
authType = _tls.RequireAnyClientCert
case 3:
authType = _tls.VerifyClientCertIfGiven
case 4:
authType = _tls.RequireAndVerifyClientCert
}
}
cfg := &_tls.Config{
ServerName: tls.Host,
ClientAuth: authType,
ClientCAs: certPool,
MinVersion: _tls.VersionTLS12,
}
return cfg, nil
}
type Config struct {
Port int
TLS *TLS
IdleTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func (c *Config) validate() error {
if c.Port == 0 {
if c.TLS == nil {
c.Port = 80
} else {
c.Port = 443
}
}
if c.TLS == nil {
return nil
}
return c.TLS.validate()
}
/*
######################################################################################################## @(°_°)@ #######
*/

@ -0,0 +1,78 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### server ####### Copyright (c) 2021 losyme ################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package server
import (
"context"
"fmt"
"log"
"net/http"
)
type Server struct {
config *Config
server *http.Server
}
func New(cfg *Config, handler http.Handler, logger *log.Logger) (*Server, error) {
if cfg == nil {
cfg = &Config{}
}
if err := cfg.validate(); err != nil {
return nil, err
}
tlsConfig, err := cfg.TLS.config()
if err != nil {
return nil, err
}
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: handler,
ErrorLog: logger,
IdleTimeout: cfg.IdleTimeout,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
TLSConfig: tlsConfig,
}
server := &Server{
config: cfg,
server: httpServer,
}
return server, nil
}
func (s *Server) ListenAndServe() error {
tls := s.config.TLS
var err error
if tls != nil {
err = s.server.ListenAndServeTLS(tls.Cert, tls.Key)
} else {
err = s.server.ListenAndServe()
}
if err == http.ErrServerClosed {
return nil
}
return err
}
func (s *Server) Shutdown(ctx context.Context) error {
s.server.SetKeepAlivesEnabled(false)
return s.server.Shutdown(ctx)
}
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save