-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7966077
commit 6edd87c
Showing
14 changed files
with
395 additions
and
162 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package api | ||
|
||
import ( | ||
"assistant/config" | ||
"assistant/logging" | ||
"assistant/types" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/gorilla/mux" | ||
"github.com/rs/zerolog" | ||
"net" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
func Start(config *config.ProjectConfig, slitherOutput *types.SlitherOutput) { | ||
port := fmt.Sprint(":", config.Port) | ||
|
||
if port == ":" { | ||
port = ":8080" // Default port | ||
} | ||
|
||
// Create sub-logger for api module | ||
logger := logging.NewLogger(zerolog.InfoLevel) | ||
logger.AddWriter(os.Stdout, logging.UNSTRUCTURED, true) | ||
|
||
// Create a new router | ||
router := mux.NewRouter() | ||
|
||
// Attach middleware | ||
attachMiddleware(router) | ||
|
||
// Serve the contracts on a subrouter | ||
router.HandleFunc("/contracts", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
err := json.NewEncoder(w).Encode(slitherOutput) | ||
if err != nil { | ||
logger.Error("Failed to encode contracts: ", err) | ||
return | ||
} | ||
}) | ||
|
||
var listener net.Listener | ||
var err error | ||
|
||
for i := 0; i < 10; i++ { | ||
listener, err = net.Listen("tcp", port) | ||
if err == nil { | ||
break | ||
} | ||
|
||
logger.Info("Server failed to start on port ", port[1:]) | ||
port = incrementPort(port) | ||
} | ||
|
||
// Stop further execution if the server failed to start | ||
if listener == nil { | ||
logger.Error("Failed to start server: ", err) | ||
return | ||
} | ||
|
||
logger.Info("Server started on port ", port[1:]) | ||
|
||
// Create a channel to receive interrupt signals | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) | ||
|
||
// Start the server in a separate goroutine | ||
serverErrorChan := make(chan error, 1) | ||
go func() { | ||
serverErrorChan <- http.Serve(listener, router) | ||
}() | ||
|
||
// Gracefully shutdown the server if a server error is encountered | ||
select { | ||
case <-sigChan: | ||
logger.Info("Shutting down server...") | ||
err := listener.Close() | ||
if err != nil { | ||
logger.Error("Failed to shut down server: ", err) | ||
return | ||
} | ||
case err := <-serverErrorChan: | ||
logger.Error("Server error: ", err) | ||
} | ||
} | ||
|
||
func incrementPort(port string) string { | ||
var portNum int | ||
|
||
_, err := fmt.Sscanf(port, ":%d", &portNum) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return fmt.Sprintf(":%d", portNum+1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gorilla/mux" | ||
"net/http" | ||
) | ||
|
||
func setHeaders(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Set default headers | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
// Handle CORS headers | ||
w.Header().Set("Access-Control-Allow-Origin", "*") | ||
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") | ||
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func attachMiddleware(router *mux.Router) { | ||
router.Use(setHeaders) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.